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 + } ] } } 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..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; } @@ -353,12 +358,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, }; } 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),