From c494ff901223520b959724a34cf0c083ee41d65b Mon Sep 17 00:00:00 2001 From: Alexander Cerutti Date: Fri, 8 Oct 2021 23:27:57 +0200 Subject: [PATCH] Merged buffer json reading and validation under the same function. Now it is used for pass.json and personalization.json --- src/PKPass.ts | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/PKPass.ts b/src/PKPass.ts index b3f34c5..4842025 100644 --- a/src/PKPass.ts +++ b/src/PKPass.ts @@ -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[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); }