schema-shield 1.0.5 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1 +1 @@
1
- {"version":3,"file":"array-keywords.d.ts","sourceRoot":"","sources":["../../lib/keywords/array-keywords.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AA+F3C,eAAO,MAAM,aAAa,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,CAkTzD,CAAC"}
1
+ {"version":3,"file":"array-keywords.d.ts","sourceRoot":"","sources":["../../lib/keywords/array-keywords.ts"],"names":[],"mappings":"AAKA,OAAO,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AA+F3C,eAAO,MAAM,aAAa,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,CA8TzD,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"number-keywords.d.ts","sourceRoot":"","sources":["../../lib/keywords/number-keywords.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAG3C,eAAO,MAAM,cAAc,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,CA2F1D,CAAC"}
1
+ {"version":3,"file":"number-keywords.d.ts","sourceRoot":"","sources":["../../lib/keywords/number-keywords.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAG3C,eAAO,MAAM,cAAc,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,CAyG1D,CAAC"}
@@ -1,3 +1,9 @@
1
- import { KeywordFunction } from "../index";
1
+ import type { KeywordFunction, SchemaShield } from "../index";
2
+ interface ApplyPropertyDefaultsFunction {
3
+ (schema: Record<string, any>, data: any, instance: SchemaShield): void;
4
+ }
5
+ export declare const applyPropertyDefaults: ApplyPropertyDefaultsFunction;
6
+ export declare const applyEmptyPropertyDefaults: ApplyPropertyDefaultsFunction;
2
7
  export declare const ObjectKeywords: Record<string, KeywordFunction | false>;
8
+ export {};
3
9
  //# sourceMappingURL=object-keywords.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"object-keywords.d.ts","sourceRoot":"","sources":["../../lib/keywords/object-keywords.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AA8F3C,eAAO,MAAM,cAAc,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,GAAG,KAAK,CA2YlE,CAAC"}
1
+ {"version":3,"file":"object-keywords.d.ts","sourceRoot":"","sources":["../../lib/keywords/object-keywords.ts"],"names":[],"mappings":"AAMA,OAAO,KAAK,EAAE,eAAe,EAAE,YAAY,EAAE,MAAM,UAAU,CAAC;AAW9D,UAAU,6BAA6B;IACrC,CAAC,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,EAAE,IAAI,EAAE,GAAG,EAAE,QAAQ,EAAE,YAAY,GAAG,IAAI,CAAC;CACxE;AAgCD,eAAO,MAAM,qBAAqB,+BAAqC,CAAC;AACxE,eAAO,MAAM,0BAA0B,+BAAoC,CAAC;AAqF5E,eAAO,MAAM,cAAc,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,GAAG,KAAK,CAgWlE,CAAC"}
@@ -1,3 +1,19 @@
1
- import { KeywordFunction } from "../index";
1
+ import type { KeywordFunction, ValidateFunction, ValidateSubschemaFunction } from "../index";
2
+ import type { DefineErrorFunction } from "../utils/main-utils";
3
+ type CombinatorKey = "allOf" | "anyOf" | "oneOf";
4
+ type DefaultMutation = {
5
+ target: Record<string, any>;
6
+ key: string;
7
+ value: any;
8
+ };
9
+ type TransactionHooks = {
10
+ savepoint: () => number;
11
+ rollback: (savepoint: number) => void;
12
+ capture: (savepoint: number) => DefaultMutation[];
13
+ restore: (mutations: DefaultMutation[]) => void;
14
+ };
15
+ export declare function createCombinatorValidator(key: CombinatorKey, schema: any, defineError: DefineErrorFunction, validateSubschema?: ValidateSubschemaFunction, transactions?: TransactionHooks): ValidateFunction;
16
+ export declare function prepareCombinatorEntries(schema: any): void;
2
17
  export declare const OtherKeywords: Record<string, KeywordFunction>;
18
+ export {};
3
19
  //# sourceMappingURL=other-keywords.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"other-keywords.d.ts","sourceRoot":"","sources":["../../lib/keywords/other-keywords.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAE,MAAM,UAAU,CAAC;AAkD3C,eAAO,MAAM,aAAa,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,CA0UzD,CAAC"}
1
+ {"version":3,"file":"other-keywords.d.ts","sourceRoot":"","sources":["../../lib/keywords/other-keywords.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EACV,eAAe,EAEf,gBAAgB,EAChB,yBAAyB,EAC1B,MAAM,UAAU,CAAC;AAClB,OAAO,KAAK,EAAE,mBAAmB,EAAE,MAAM,qBAAqB,CAAC;AAkD/D,KAAK,aAAa,GAAG,OAAO,GAAG,OAAO,GAAG,OAAO,CAAC;AACjD,KAAK,eAAe,GAAG;IAAE,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAC;IAAC,GAAG,EAAE,MAAM,CAAC;IAAC,KAAK,EAAE,GAAG,CAAA;CAAE,CAAC;AAChF,KAAK,gBAAgB,GAAG;IACtB,SAAS,EAAE,MAAM,MAAM,CAAC;IACxB,QAAQ,EAAE,CAAC,SAAS,EAAE,MAAM,KAAK,IAAI,CAAC;IACtC,OAAO,EAAE,CAAC,SAAS,EAAE,MAAM,KAAK,eAAe,EAAE,CAAC;IAClD,OAAO,EAAE,CAAC,SAAS,EAAE,eAAe,EAAE,KAAK,IAAI,CAAC;CACjD,CAAC;AAqFF,wBAAgB,yBAAyB,CACvC,GAAG,EAAE,aAAa,EAClB,MAAM,EAAE,GAAG,EACX,WAAW,EAAE,mBAAmB,EAChC,iBAAiB,CAAC,EAAE,yBAAyB,EAC7C,YAAY,CAAC,EAAE,gBAAgB,GAC9B,gBAAgB,CA0ElB;AAED,wBAAgB,wBAAwB,CAAC,MAAM,EAAE,GAAG,QAUnD;AAED,eAAO,MAAM,aAAa,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,CAqJzD,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"string-keywords.d.ts","sourceRoot":"","sources":["../../lib/keywords/string-keywords.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,eAAe,EAAE,MAAM,UAAU,CAAC;AAM3D,eAAO,MAAM,cAAc,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,CAiK1D,CAAC"}
1
+ {"version":3,"file":"string-keywords.d.ts","sourceRoot":"","sources":["../../lib/keywords/string-keywords.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,eAAe,EAAE,MAAM,UAAU,CAAC;AA+B3D,eAAO,MAAM,cAAc,EAAE,MAAM,CAAC,MAAM,EAAE,eAAe,CAmL1D,CAAC"}
@@ -1 +1 @@
1
- {"version":3,"file":"deep-freeze.d.ts","sourceRoot":"","sources":["../../lib/utils/deep-freeze.ts"],"names":[],"mappings":"AAAA,wBAAgB,UAAU,CACxB,GAAG,EAAE,GAAG,EACR,oBAAoB,GAAE,OAAe,EACrC,IAAI,kBAAgB,GACnB,GAAG,CAkCL;AAED,iBAAS,aAAa,CAAC,KAAK,EAAE,GAAG,GAAG,OAAO,CAO1C;AAED,OAAO,EAAE,aAAa,EAAE,CAAC;AAuBzB,wBAAgB,iBAAiB,CAAC,CAAC,EACjC,GAAG,EAAE,CAAC,EACN,mBAAmB,UAAQ,EAC3B,IAAI,uBAAgB,GACnB,CAAC,CAmIH"}
1
+ {"version":3,"file":"deep-freeze.d.ts","sourceRoot":"","sources":["../../lib/utils/deep-freeze.ts"],"names":[],"mappings":"AAEA,wBAAgB,UAAU,CACxB,GAAG,EAAE,GAAG,EACR,oBAAoB,GAAE,OAAe,EACrC,IAAI,kBAAgB,GACnB,GAAG,CAkCL;AAED,iBAAS,aAAa,CAAC,KAAK,EAAE,GAAG,GAAG,OAAO,CAO1C;AAED,OAAO,EAAE,aAAa,EAAE,CAAC;AAuBzB,wBAAgB,iBAAiB,CAAC,CAAC,EACjC,GAAG,EAAE,CAAC,EACN,mBAAmB,UAAQ,EAC3B,IAAI,uBAAgB,GACnB,CAAC,CAmIH"}
@@ -1,4 +1,6 @@
1
- import { CompiledSchema } from "../index";
1
+ import type { CompiledSchema } from "../index";
2
+ export declare function definePropertyOrThrow<T extends object>(target: T, key: PropertyKey, descriptor: PropertyDescriptor): T;
3
+ export declare function hasOwn(target: any, key: PropertyKey): boolean;
2
4
  interface ErrorTree {
3
5
  message: string;
4
6
  keyword: string;
@@ -9,6 +11,7 @@ interface ErrorTree {
9
11
  cause?: ErrorTree;
10
12
  }
11
13
  export declare class ValidationError extends Error {
14
+ code?: string;
12
15
  message: string;
13
16
  item?: string | number;
14
17
  keyword: string;
@@ -18,9 +21,7 @@ export declare class ValidationError extends Error {
18
21
  data?: any;
19
22
  schema?: CompiledSchema;
20
23
  constructor(message: string);
21
- private _getCause;
22
24
  getCause(): ValidationError;
23
- private _getTree;
24
25
  getTree(): ErrorTree;
25
26
  getPath(): {
26
27
  schemaPath: string;
@@ -28,6 +29,7 @@ export declare class ValidationError extends Error {
28
29
  };
29
30
  }
30
31
  export interface DefineErrorOptions {
32
+ code?: string;
31
33
  item?: any;
32
34
  cause?: ValidationError | true;
33
35
  data?: any;
@@ -36,9 +38,10 @@ export interface DefineErrorFunction {
36
38
  (message: string, options?: DefineErrorOptions): ValidationError | void | true;
37
39
  }
38
40
  export declare function getDefinedErrorFunctionForKey(key: string, schema: CompiledSchema, failFast: boolean): DefineErrorFunction;
41
+ export declare function escapeJsonPointerToken(value: string | number): string;
39
42
  export declare function getUTF16Length(str: any): number;
40
43
  export declare function isCompiledSchema(subSchema: any): subSchema is CompiledSchema;
41
- export declare function getNamedFunction<T>(name: string, fn: T): T;
44
+ export declare function getNamedFunction<T extends object>(name: string, fn: T): T;
42
45
  export declare function resolvePath(root: any, path: string): any;
43
46
  export declare function areCloseEnough(a: number, b: number, epsilon?: number): boolean;
44
47
  export {};
@@ -1 +1 @@
1
- {"version":3,"file":"main-utils.d.ts","sourceRoot":"","sources":["../../lib/utils/main-utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,cAAc,EAAE,MAAM,UAAU,CAAC;AAE1C,UAAU,SAAS;IACjB,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IACvB,UAAU,EAAE,MAAM,CAAC;IACnB,YAAY,EAAE,MAAM,CAAC;IACrB,IAAI,CAAC,EAAE,GAAG,CAAC;IACX,KAAK,CAAC,EAAE,SAAS,CAAC;CACnB;AAED,qBAAa,eAAgB,SAAQ,KAAK;IACxC,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,MAAM,GAAG,MAAM,CAAC;IACvB,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,eAAe,CAAC;IACxB,UAAU,EAAE,MAAM,CAAM;IACxB,YAAY,EAAE,MAAM,CAAM;IAC1B,IAAI,CAAC,EAAE,GAAG,CAAC;IACX,MAAM,CAAC,EAAE,cAAc,CAAC;gBAEZ,OAAO,EAAE,MAAM;IAK3B,OAAO,CAAC,SAAS;IA0BjB,QAAQ,IAAI,eAAe;IAI3B,OAAO,CAAC,QAAQ;IAiBhB,OAAO,IAAI,SAAS;IAKpB,OAAO;;;;CAOR;AAED,MAAM,WAAW,kBAAkB;IACjC,IAAI,CAAC,EAAE,GAAG,CAAC;IACX,KAAK,CAAC,EAAE,eAAe,GAAG,IAAI,CAAC;IAC/B,IAAI,CAAC,EAAE,GAAG,CAAC;CACZ;AAED,MAAM,WAAW,mBAAmB;IAClC,CACE,OAAO,EAAE,MAAM,EACf,OAAO,CAAC,EAAE,kBAAkB,GAC3B,eAAe,GAAG,IAAI,GAAG,IAAI,CAAC;CAClC;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E,CAAC,EAAE,MAAM,EAAE,OAAO,SAAQ,GAAG,OAAO,CAE7E"}
package/lib/formats.ts CHANGED
@@ -11,7 +11,6 @@ const EMAIL_REGEX =
11
11
  /^(?!\.)(?!.*\.$)[a-z0-9!#$%&'*+/=?^_`{|}~-]{1,20}(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]{1,21}){0,2}@[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[a-z0-9-]{0,60}[a-z0-9])?){0,3}$/i;
12
12
  const HOSTNAME_REGEX =
13
13
  /^[a-z0-9][a-z0-9-]{0,62}(?:\.[a-z0-9][a-z0-9-]{0,62})*[a-z0-9]$/i;
14
- const DATE_REGEX = /^(\d{4})-(\d{2})-(\d{2})$/;
15
14
  const TIME_REGEX =
16
15
  /^([01]\d|2[0-3]):([0-5]\d):([0-5]\d)(\.\d+)?(Z|([+-])([01]\d|2[0-3]):([0-5]\d))$/;
17
16
  const URI_REFERENCE_REGEX =
@@ -112,6 +111,27 @@ function isHexCharCode(code: number) {
112
111
  );
113
112
  }
114
113
 
114
+ function hasValidPercentEncoding(data: string) {
115
+ for (let index = 0; index < data.length; index++) {
116
+ const code = data.charCodeAt(index);
117
+ if (code === 92) {
118
+ return false;
119
+ }
120
+ if (code !== 37) {
121
+ continue;
122
+ }
123
+ if (
124
+ index + 2 >= data.length ||
125
+ !isHexCharCode(data.charCodeAt(index + 1)) ||
126
+ !isHexCharCode(data.charCodeAt(index + 2))
127
+ ) {
128
+ return false;
129
+ }
130
+ index += 2;
131
+ }
132
+ return true;
133
+ }
134
+
115
135
  function isValidIpv6(data: string) {
116
136
  const length = data.length;
117
137
  if (length === 0) {
@@ -249,7 +269,7 @@ function isValidJsonPointer(data: string) {
249
269
 
250
270
  function isValidRelativeJsonPointer(data: string) {
251
271
  if (data.length === 0) {
252
- return true;
272
+ return false;
253
273
  }
254
274
 
255
275
  let i = 0;
@@ -265,6 +285,10 @@ function isValidRelativeJsonPointer(data: string) {
265
285
  return false;
266
286
  }
267
287
 
288
+ if (i > 1 && data.charCodeAt(0) === 48) {
289
+ return false;
290
+ }
291
+
268
292
  if (i === data.length) {
269
293
  return true;
270
294
  }
@@ -451,7 +475,7 @@ export const Formats: Record<string, FormatFunction | false> = {
451
475
  return true;
452
476
  },
453
477
  uri(data) {
454
- return URI_REGEX.test(data);
478
+ return URI_REGEX.test(data) && hasValidPercentEncoding(data);
455
479
  },
456
480
  email(data) {
457
481
  return EMAIL_REGEX.test(data);
@@ -468,17 +492,19 @@ export const Formats: Record<string, FormatFunction | false> = {
468
492
  return HOSTNAME_REGEX.test(data);
469
493
  },
470
494
  date(data) {
471
- const match = DATE_REGEX.exec(data);
472
- if (!match) {
495
+ if (
496
+ data.length !== 10 ||
497
+ data.charCodeAt(4) !== 45 ||
498
+ data.charCodeAt(7) !== 45
499
+ ) {
473
500
  return false;
474
501
  }
475
502
 
476
- const [, yearStr, monthStr, dayStr] = match;
477
- const year = Number(yearStr);
478
- const month = Number(monthStr);
479
- const day = Number(dayStr);
503
+ const year = parseFourDigits(data, 0);
504
+ const month = parseTwoDigits(data, 5);
505
+ const day = parseTwoDigits(data, 8);
480
506
 
481
- if (month < 1 || month > 12) {
507
+ if (year < 0 || month < 1 || month > 12) {
482
508
  return false;
483
509
  }
484
510
  if (day < 1) {