From 1fe4355c2657fa5a627a350bbda291aacd213bb3 Mon Sep 17 00:00:00 2001 From: Alexander Cerutti Date: Wed, 29 Sep 2021 23:55:46 +0200 Subject: [PATCH] Added merging of pass.json props in this[propsSymbol] with an overwrite warning --- src/PKPass.ts | 49 +++++++++++++++++++++++++++++++++++++++----- src/schemas/index.ts | 4 +++- 2 files changed, 47 insertions(+), 6 deletions(-) diff --git a/src/PKPass.ts b/src/PKPass.ts index f68cb87..f4eb720 100644 --- a/src/PKPass.ts +++ b/src/PKPass.ts @@ -409,6 +409,37 @@ export default class PKPass extends Bundle { const type = possibleTypes.find((type) => Boolean(data[type])); + const { + boardingPass, + coupon, + storeCard, + generic, + eventTicket, + ...otherPassData + } = data; + + /** + * Validating the rest of the data and + * importing all the props. They are going + * to overwrite props setted by user but + * we can't do much about. + */ + + const validation = Schemas.getValidated( + otherPassData, + Schemas.PassProps, + ); + + if (validation) { + if (Object.keys(this[propsSymbol]).length) { + console.warn( + "The imported pass.json's properties will be joined with the current setted props. You might lose some data.", + ); + } + + Object.assign(this[propsSymbol], validation); + } + if (!type) { if (!this[passTypeSymbol]) { console.warn( @@ -422,11 +453,19 @@ export default class PKPass extends Bundle { } else { this.type = type; - this.headerFields.push(...data[type]?.headerFields); - this.primaryFields.push(...data[type]?.primaryFields); - this.secondaryFields.push(...data[type]?.secondaryFields); - this.auxiliaryFields.push(...data[type]?.auxiliaryFields); - this.backFields.push(...data[type]?.backFields); + const { + headerFields = [], + primaryFields = [], + secondaryFields = [], + auxiliaryFields = [], + backFields = [], + } = data[type]; + + this.headerFields.push(...headerFields); + this.primaryFields.push(...primaryFields); + this.secondaryFields.push(...secondaryFields); + this.auxiliaryFields.push(...auxiliaryFields); + this.backFields.push(...backFields); } } diff --git a/src/schemas/index.ts b/src/schemas/index.ts index 6986ca5..712a50c 100644 --- a/src/schemas/index.ts +++ b/src/schemas/index.ts @@ -183,7 +183,9 @@ export const OverridablePassProps = Joi.object({ ), }).with("webServiceURL", "authenticationToken"); -export const PassProps = Joi.object({ +export const PassProps = Joi.object< + OverridablePassProps & PassKindsProps & PassPropsFromMethods +>({ ...OverridablePassProps, ...PassKindsProps, ...PassPropsFromMethods,