Added checks in PKPass methods to prevent changing contents if pass is frozen

This commit is contained in:
Alexander Cerutti
2021-10-23 00:27:27 +02:00
parent 3b69446873
commit 86d09b9509

View File

@@ -170,11 +170,14 @@ export default class PKPass extends Bundle {
*/ */
public set certificates(certs: Schemas.CertificatesSchema) { public set certificates(certs: Schemas.CertificatesSchema) {
Utils.assertUnfrozen(this);
Schemas.assertValidity( Schemas.assertValidity(
Schemas.CertificatesSchema, Schemas.CertificatesSchema,
certs, certs,
Messages.CERTIFICATES.INVALID, Messages.CERTIFICATES.INVALID,
); );
this[certificatesSymbol] = certs; this[certificatesSymbol] = certs;
} }
@@ -196,6 +199,8 @@ export default class PKPass extends Bundle {
*/ */
public set transitType(value: Schemas.TransitType) { public set transitType(value: Schemas.TransitType) {
Utils.assertUnfrozen(this);
if (this.type !== "boardingPass") { if (this.type !== "boardingPass") {
throw new TypeError(Messages.TRANSIT_TYPE.UNEXPECTED_PASS_TYPE); throw new TypeError(Messages.TRANSIT_TYPE.UNEXPECTED_PASS_TYPE);
} }
@@ -287,6 +292,8 @@ export default class PKPass extends Bundle {
*/ */
public set type(type: Schemas.PassTypesProps) { public set type(type: Schemas.PassTypesProps) {
Utils.assertUnfrozen(this);
Schemas.assertValidity( Schemas.assertValidity(
Schemas.PassType, Schemas.PassType,
type, type,
@@ -687,6 +694,8 @@ export default class PKPass extends Bundle {
lang: string, lang: string,
translations: { [key: string]: string } | null, translations: { [key: string]: string } | null,
) { ) {
Utils.assertUnfrozen(this);
if (typeof lang !== "string") { if (typeof lang !== "string") {
throw new TypeError( throw new TypeError(
formatMessage(Messages.LANGUAGES.INVALID_LANG, typeof lang), formatMessage(Messages.LANGUAGES.INVALID_LANG, typeof lang),
@@ -734,6 +743,8 @@ export default class PKPass extends Bundle {
*/ */
public setExpirationDate(date: Date | null) { public setExpirationDate(date: Date | null) {
Utils.assertUnfrozen(this);
if (date === null) { if (date === null) {
delete this[propsSymbol]["expirationDate"]; delete this[propsSymbol]["expirationDate"];
return; return;
@@ -770,6 +781,8 @@ export default class PKPass extends Bundle {
public setBeacons(beacons: null): void; public setBeacons(beacons: null): void;
public setBeacons(...beacons: Schemas.Beacon[]): void; public setBeacons(...beacons: Schemas.Beacon[]): void;
public setBeacons(...beacons: (Schemas.Beacon | null)[]) { public setBeacons(...beacons: (Schemas.Beacon | null)[]) {
Utils.assertUnfrozen(this);
if (beacons[0] === null) { if (beacons[0] === null) {
delete this[propsSymbol]["beacons"]; delete this[propsSymbol]["beacons"];
return; return;
@@ -804,6 +817,8 @@ export default class PKPass extends Bundle {
public setLocations(locations: null): void; public setLocations(locations: null): void;
public setLocations(...locations: Schemas.Location[]): void; public setLocations(...locations: Schemas.Location[]): void;
public setLocations(...locations: (Schemas.Location | null)[]): void { public setLocations(...locations: (Schemas.Location | null)[]): void {
Utils.assertUnfrozen(this);
if (locations[0] === null) { if (locations[0] === null) {
delete this[propsSymbol]["locations"]; delete this[propsSymbol]["locations"];
return; return;
@@ -823,6 +838,8 @@ export default class PKPass extends Bundle {
*/ */
public setRelevantDate(date: Date): void { public setRelevantDate(date: Date): void {
Utils.assertUnfrozen(this);
if (date === null) { if (date === null) {
delete this[propsSymbol]["relevantDate"]; delete this[propsSymbol]["relevantDate"];
return; return;
@@ -852,6 +869,8 @@ export default class PKPass extends Bundle {
public setBarcodes(message: string): void; public setBarcodes(message: string): void;
public setBarcodes(...barcodes: Schemas.Barcode[]): void; public setBarcodes(...barcodes: Schemas.Barcode[]): void;
public setBarcodes(...barcodes: (Schemas.Barcode | string | null)[]): void { public setBarcodes(...barcodes: (Schemas.Barcode | string | null)[]): void {
Utils.assertUnfrozen(this);
if (!barcodes.length) { if (!barcodes.length) {
return; return;
} }
@@ -904,6 +923,8 @@ export default class PKPass extends Bundle {
*/ */
public setNFC(nfc: Schemas.NFC | null): void { public setNFC(nfc: Schemas.NFC | null): void {
Utils.assertUnfrozen(this);
if (nfc === null) { if (nfc === null) {
delete this[propsSymbol]["nfc"]; delete this[propsSymbol]["nfc"];
return; return;