From e4fc8feda88760f0864389be89836589da897c6c Mon Sep 17 00:00:00 2001 From: Alexander Cerutti Date: Wed, 9 Nov 2022 19:30:04 +0100 Subject: [PATCH 1/4] Added FieldWithRow interface and Joi Schema --- src/schemas/Field.ts | 10 ++++++++++ src/schemas/PassFields.ts | 12 +++--------- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/schemas/Field.ts b/src/schemas/Field.ts index 3f1b746..1c837c5 100644 --- a/src/schemas/Field.ts +++ b/src/schemas/Field.ts @@ -22,6 +22,10 @@ export interface Field { numberStyle?: string; } +export interface FieldWithRow extends Field { + row?: 0 | 1; +} + export const Field = Joi.object().keys({ attributedValue: Joi.alternatives( Joi.string().allow(""), @@ -72,3 +76,9 @@ export const Field = Joi.object().keys({ otherwise: Joi.string().forbidden(), }), }); + +export const FieldWithRow = Field.concat( + Joi.object().keys({ + row: Joi.number().min(0).max(1), + }), +); diff --git a/src/schemas/PassFields.ts b/src/schemas/PassFields.ts index 96cd81d..41eef7d 100644 --- a/src/schemas/PassFields.ts +++ b/src/schemas/PassFields.ts @@ -1,5 +1,5 @@ import Joi from "joi"; -import { Field } from "./Field"; +import { Field, FieldWithRow } from "./Field"; export type TransitType = | "PKTransitTypeAir" @@ -13,7 +13,7 @@ export const TransitType = Joi.string().regex( ); export interface PassFields { - auxiliaryFields: (Field & { row?: number })[]; + auxiliaryFields: FieldWithRow[]; backFields: Field[]; headerFields: Field[]; primaryFields: Field[]; @@ -22,13 +22,7 @@ export interface PassFields { } export const PassFields = Joi.object().keys({ - auxiliaryFields: Joi.array().items( - Joi.object() - .keys({ - row: Joi.number().max(1).min(0), - }) - .concat(Field), - ), + auxiliaryFields: Joi.array().items(FieldWithRow), backFields: Joi.array().items(Field), headerFields: Joi.array().items(Field), primaryFields: Joi.array().items(Field), From 68c9b1588ef59a29f95d2188d1866076c2354716 Mon Sep 17 00:00:00 2001 From: Alexander Cerutti Date: Wed, 9 Nov 2022 19:31:44 +0100 Subject: [PATCH 2/4] Added fieldSchema property to FieldsArray --- src/FieldsArray.ts | 5 ++++- src/PKPass.ts | 32 ++++++++++++++++++++++++++------ 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/src/FieldsArray.ts b/src/FieldsArray.ts index f02df0f..00e07b0 100644 --- a/src/FieldsArray.ts +++ b/src/FieldsArray.ts @@ -10,6 +10,7 @@ import * as Messages from "./messages"; const passInstanceSymbol = Symbol("passInstance"); const sharedKeysPoolSymbol = Symbol("keysPool"); +const fieldSchemaSymbol = Symbol("fieldSchema"); export default class FieldsArray extends Array { private [passInstanceSymbol]: InstanceType; @@ -18,9 +19,11 @@ export default class FieldsArray extends Array { constructor( passInstance: InstanceType, keysPool: Set, + fieldSchema: typeof Schemas.Field | typeof Schemas.FieldWithRow, ...args: Schemas.Field[] ) { super(...args); + this[fieldSchemaSymbol] = fieldSchema; this[passInstanceSymbol] = passInstance; this[sharedKeysPoolSymbol] = keysPool; } @@ -75,7 +78,7 @@ function registerWithValidation( try { Schemas.assertValidity( - Schemas.Field, + instance[fieldSchemaSymbol], field, Messages.FIELDS.INVALID, ); diff --git a/src/PKPass.ts b/src/PKPass.ts index e74f0ba..58e02ba 100644 --- a/src/PKPass.ts +++ b/src/PKPass.ts @@ -353,12 +353,32 @@ export default class PKPass extends Bundle { const sharedKeysPool = new Set(); this[passTypeSymbol] = type; - this[propsSymbol][this[passTypeSymbol]] = { - headerFields /******/: new FieldsArray(this, sharedKeysPool), - primaryFields /*****/: new FieldsArray(this, sharedKeysPool), - secondaryFields /***/: new FieldsArray(this, sharedKeysPool), - auxiliaryFields /***/: new FieldsArray(this, sharedKeysPool), - backFields /********/: new FieldsArray(this, sharedKeysPool), + this[propsSymbol][type] = { + headerFields /******/: new FieldsArray( + this, + sharedKeysPool, + Schemas.Field, + ), + primaryFields /*****/: new FieldsArray( + this, + sharedKeysPool, + Schemas.Field, + ), + secondaryFields /***/: new FieldsArray( + this, + sharedKeysPool, + Schemas.Field, + ), + auxiliaryFields /***/: new FieldsArray( + this, + sharedKeysPool, + type === "eventTicket" ? Schemas.FieldWithRow : Schemas.Field, + ), + backFields /********/: new FieldsArray( + this, + sharedKeysPool, + Schemas.Field, + ), transitType: undefined, }; } From ab70e61f81311df1c9776669b9c43de2174f72f7 Mon Sep 17 00:00:00 2001 From: Alexander Cerutti Date: Fri, 11 Nov 2022 23:39:17 +0100 Subject: [PATCH 3/4] Changed auxiliaryFields getter return type --- src/PKPass.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/PKPass.ts b/src/PKPass.ts index 58e02ba..e1347f4 100644 --- a/src/PKPass.ts +++ b/src/PKPass.ts @@ -287,9 +287,14 @@ export default class PKPass extends Bundle { * if no valid pass.json has been parsed yet * or, anyway, if a valid type has not been * set yet. + * + * For Typescript users: this signature allows + * in any case to add the 'row' field, but on + * runtime they are only allowed on "eventTicket" + * passes. */ - public get auxiliaryFields(): Schemas.Field[] { + public get auxiliaryFields(): Schemas.FieldWithRow[] { return this[propsSymbol][this.type].auxiliaryFields; } From 036e4753c4c38c81952dd4afec9459ba0e16e634 Mon Sep 17 00:00:00 2001 From: Alexander Cerutti Date: Sat, 12 Nov 2022 00:20:56 +0100 Subject: [PATCH 4/4] Added auxiliaryFields rows in examplePass model --- examples/models/examplePass.pass/pass.json | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/examples/models/examplePass.pass/pass.json b/examples/models/examplePass.pass/pass.json index 44026d9..cdb2916 100644 --- a/examples/models/examplePass.pass/pass.json +++ b/examples/models/examplePass.pass/pass.json @@ -41,6 +41,20 @@ "label": "LOCATION", "value": "Moscone West" } + ], + "auxiliaryFields": [ + { + "key": "Foo", + "value": "foo2", + "label": "Foo(l)", + "row": 0 + }, + { + "key": "Foo2", + "value": "foo3", + "label": "Foo(l)2", + "row": 1 + } ] } }