Added merging of pass.json props in this[propsSymbol] with an overwrite warning

This commit is contained in:
Alexander Cerutti
2021-09-29 23:55:46 +02:00
parent b0b0a9555f
commit 1fe4355c26
2 changed files with 47 additions and 6 deletions

View File

@@ -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);
}
}

View File

@@ -183,7 +183,9 @@ export const OverridablePassProps = Joi.object<OverridablePassProps>({
),
}).with("webServiceURL", "authenticationToken");
export const PassProps = Joi.object({
export const PassProps = Joi.object<
OverridablePassProps & PassKindsProps & PassPropsFromMethods
>({
...OverridablePassProps,
...PassKindsProps,
...PassPropsFromMethods,