Added a new setter and getter for preferredStyleSchemes to throw if type is not an eventTicket and moved PreferredStyleSchemes schemas

This commit is contained in:
Alexander Cerutti
2024-06-15 23:48:33 +02:00
parent 593b64a676
commit 432e380429
4 changed files with 56 additions and 6 deletions

View File

@@ -316,6 +316,44 @@ export default class PKPass extends Bundle {
return this[propsSymbol][this.type].backFields;
}
/**
* Allows accessing to iOS 18 new Event Ticket
* property `preferredStyleSchemes`.
*
* @throws (automatically) if current type is not
* "eventTicket".
*/
public get preferredStyleSchemes(): Schemas.PreferredStyleSchemes {
return this[propsSymbol]["eventTicket"].preferredStyleSchemes;
}
/**
* Allows setting a preferredStyleSchemes property
* for a eventTicket.
*
* @throws if current type is not "eventTicket".
* @param value
*/
public set preferredStyleSchemes(value: Schemas.PreferredStyleSchemes) {
Utils.assertUnfrozen(this);
if (this.type !== "eventTicket") {
throw new TypeError(
Messages.PREFERRED_STYLE_SCHEMES.UNEXPECTED_PASS_TYPE,
);
}
Schemas.assertValidity(
Schemas.PreferredStyleSchemes,
value,
Messages.PREFERRED_STYLE_SCHEMES.INVALID,
);
this[propsSymbol]["eventTicket"].preferredStyleSchemes = value;
}
/**
* Allows setting a pass type.
*

View File

@@ -15,6 +15,13 @@ export const TRANSIT_TYPE = {
"Cannot set transitType because not compliant with Apple specifications. Refer to https://apple.co/3DHuAG4 for more - %s",
} as const;
export const PREFERRED_STYLE_SCHEMES = {
UNEXPECTED_PASS_TYPE:
"Cannot set preferredStyleSchemes on a pass with type different from eventTicket.",
INVALID:
"Cannot set preferredStyleSchemes because not compliant with Apple specifications - %s",
} as const;
export const PASS_TYPE = {
INVALID:
"Cannot set type because not compliant with Apple specifications. Refer to https://apple.co/3aFpSfg for a list of valid props - %s",

View File

@@ -12,6 +12,13 @@ export const TransitType = Joi.string().regex(
/(PKTransitTypeAir|PKTransitTypeBoat|PKTransitTypeBus|PKTransitTypeGeneric|PKTransitTypeTrain)/,
);
export type PreferredStyleSchemes = ("posterEventTicket" | "eventTicket")[];
export const PreferredStyleSchemes = Joi.array().items(
"posterEventTicket",
"eventTicket",
) satisfies Joi.Schema<PreferredStyleSchemes>;
export interface PassFields {
auxiliaryFields: FieldWithRow[];
backFields: Field[];

View File

@@ -15,7 +15,7 @@ import { Barcode } from "./Barcode";
import { Location } from "./Location";
import { Beacon } from "./Beacon";
import { NFC } from "./NFC";
import { PassFields, TransitType } from "./PassFields";
import { PassFields, PreferredStyleSchemes, TransitType } from "./PassFields";
import { Semantics } from "./Semantics";
import { CertificatesSchema } from "./Certificates";
@@ -75,7 +75,7 @@ export interface PassProps {
* so on, are not necessary anymore for the new style,
* as semantics are preferred.
*/
preferredStyleSchemes?: ("posterEventTicket" | "eventTicket")[];
preferredStyleSchemes?: PreferredStyleSchemes;
};
coupon?: PassFields;
generic?: PassFields;
@@ -158,9 +158,7 @@ export const PassKindsProps = Joi.object<PassKindsProps>({
* so on, are not necessary anymore for the new style,
* as semantics are preferred.
*/
preferredStyleSchemes: Joi.array().items(
Joi.string().allow("posterEventTicket", "eventTicket"),
),
preferredStyleSchemes: PreferredStyleSchemes,
}),
),
boardingPass: PassFields,
@@ -240,7 +238,7 @@ export const Template = Joi.object<Template>({
*/
export function assertValidity<T>(
schema: Joi.ObjectSchema<T> | Joi.StringSchema,
schema: Joi.ObjectSchema<T> | Joi.StringSchema | Joi.Schema<T>,
data: T,
customErrorMessage?: string,
): void {