mirror of
https://github.com/marcogll/passkit-generator.git
synced 2026-03-15 15:25:20 +00:00
Merged buffer json reading and validation under the same function. Now it is used for pass.json and personalization.json
This commit is contained in:
@@ -356,7 +356,9 @@ export default class PKPass extends Bundle {
|
||||
return;
|
||||
}
|
||||
|
||||
this[importMetadataSymbol](readPassMetadata(buffer));
|
||||
this[importMetadataSymbol](
|
||||
validateJSONBuffer(buffer, Schemas.PassProps),
|
||||
);
|
||||
|
||||
/**
|
||||
* Adding an empty buffer just for reference
|
||||
@@ -374,12 +376,8 @@ export default class PKPass extends Bundle {
|
||||
* once the pass is getting closed, if needed.
|
||||
*/
|
||||
|
||||
const prsJSON = JSON.parse(
|
||||
buffer.toString(),
|
||||
) as Schemas.Personalization;
|
||||
|
||||
try {
|
||||
Schemas.assertValidity(Schemas.Personalization, prsJSON);
|
||||
validateJSONBuffer(buffer, Schemas.Personalization);
|
||||
} catch (err) {
|
||||
console.warn(
|
||||
"Personalization.json file has been omitted as invalid.",
|
||||
@@ -879,16 +877,17 @@ function freezeRecusive(object: Object) {
|
||||
return Object.freeze(objectCopy);
|
||||
}
|
||||
|
||||
function readPassMetadata(buffer: Buffer) {
|
||||
let contentAsJSON: Schemas.PassProps;
|
||||
function validateJSONBuffer(
|
||||
buffer: Buffer,
|
||||
schema: Parameters<typeof Schemas.validate>[0],
|
||||
) {
|
||||
let contentAsJSON: unknown;
|
||||
|
||||
try {
|
||||
contentAsJSON = JSON.parse(
|
||||
buffer.toString("utf8"),
|
||||
) as Schemas.PassProps;
|
||||
contentAsJSON = JSON.parse(buffer.toString("utf8"));
|
||||
} catch (err) {
|
||||
throw new TypeError("Cannot validat Pass.json: invalid JSON");
|
||||
}
|
||||
|
||||
return Schemas.validate(Schemas.PassProps, contentAsJSON);
|
||||
return Schemas.validate(schema, contentAsJSON);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user