@cfworker/json-schema 4.0.1 → 4.0.3

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.
Files changed (50) hide show
  1. package/dist/commonjs/package.json +3 -0
  2. package/dist/esm/deep-compare-strict.d.ts +1 -0
  3. package/dist/esm/dereference.d.ts +7 -0
  4. package/dist/esm/format.d.ts +2 -0
  5. package/dist/esm/index.d.ts +8 -0
  6. package/dist/esm/package.json +3 -0
  7. package/dist/esm/pointer.d.ts +2 -0
  8. package/dist/esm/types.d.ts +72 -0
  9. package/dist/esm/ucs2-length.d.ts +1 -0
  10. package/dist/esm/validate.d.ts +3 -0
  11. package/dist/esm/validator.d.ts +10 -0
  12. package/package.json +33 -27
  13. package/src/deep-compare-strict.d.ts +1 -0
  14. package/src/deep-compare-strict.js +39 -0
  15. package/src/dereference.d.ts +12 -0
  16. package/src/dereference.js +148 -0
  17. package/src/format.d.ts +2 -0
  18. package/src/format.js +137 -0
  19. package/src/index.d.ts +8 -0
  20. package/src/index.js +8 -0
  21. package/src/pointer.d.ts +2 -0
  22. package/src/pointer.js +6 -0
  23. package/src/types.d.ts +79 -0
  24. package/src/types.js +6 -0
  25. package/src/ucs2-length.d.ts +1 -0
  26. package/src/ucs2-length.js +17 -0
  27. package/src/validate.d.ts +13 -0
  28. package/src/validate.js +1099 -0
  29. package/src/validator.d.ts +14 -0
  30. package/src/validator.js +29 -0
  31. package/dist/tsconfig.cjs.tsbuildinfo +0 -1
  32. package/dist/tsconfig.tsbuildinfo +0 -1
  33. /package/dist/{types → commonjs}/deep-compare-strict.d.ts +0 -0
  34. /package/dist/{cjs → commonjs}/deep-compare-strict.js +0 -0
  35. /package/dist/{types → commonjs}/dereference.d.ts +0 -0
  36. /package/dist/{cjs → commonjs}/dereference.js +0 -0
  37. /package/dist/{types → commonjs}/format.d.ts +0 -0
  38. /package/dist/{cjs → commonjs}/format.js +0 -0
  39. /package/dist/{types → commonjs}/index.d.ts +0 -0
  40. /package/dist/{cjs → commonjs}/index.js +0 -0
  41. /package/dist/{types → commonjs}/pointer.d.ts +0 -0
  42. /package/dist/{cjs → commonjs}/pointer.js +0 -0
  43. /package/dist/{types → commonjs}/types.d.ts +0 -0
  44. /package/dist/{cjs → commonjs}/types.js +0 -0
  45. /package/dist/{types → commonjs}/ucs2-length.d.ts +0 -0
  46. /package/dist/{cjs → commonjs}/ucs2-length.js +0 -0
  47. /package/dist/{types → commonjs}/validate.d.ts +0 -0
  48. /package/dist/{cjs → commonjs}/validate.js +0 -0
  49. /package/dist/{types → commonjs}/validator.d.ts +0 -0
  50. /package/dist/{cjs → commonjs}/validator.js +0 -0
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "commonjs"
3
+ }
@@ -0,0 +1 @@
1
+ export declare function deepCompareStrict(a: any, b: any): boolean;
@@ -0,0 +1,7 @@
1
+ import { Schema } from './types.js';
2
+ export declare const schemaKeyword: Record<string, boolean>;
3
+ export declare const schemaArrayKeyword: Record<string, boolean>;
4
+ export declare const schemaMapKeyword: Record<string, boolean>;
5
+ export declare const ignoredKeyword: Record<string, boolean>;
6
+ export declare let initialBaseURI: URL;
7
+ export declare function dereference(schema: Schema | boolean, lookup?: Record<string, Schema | boolean>, baseURI?: URL, basePointer?: string): Record<string, Schema | boolean>;
@@ -0,0 +1,2 @@
1
+ export declare const fullFormat: Record<string, (s: string) => boolean>;
2
+ export declare const fastFormat: Record<string, (s: string) => boolean>;
@@ -0,0 +1,8 @@
1
+ export * from './deep-compare-strict.js';
2
+ export * from './dereference.js';
3
+ export * from './format.js';
4
+ export * from './pointer.js';
5
+ export * from './types.js';
6
+ export * from './ucs2-length.js';
7
+ export * from './validate.js';
8
+ export * from './validator.js';
@@ -0,0 +1,3 @@
1
+ {
2
+ "type": "module"
3
+ }
@@ -0,0 +1,2 @@
1
+ export declare function encodePointer(p: string): string;
2
+ export declare function escapePointer(p: string): string;
@@ -0,0 +1,72 @@
1
+ export type SchemaDraft = '4' | '7' | '2019-09' | '2020-12';
2
+ export declare const enum OutputFormat {
3
+ Flag = 1,
4
+ Basic = 2,
5
+ Detailed = 4
6
+ }
7
+ export type InstanceType = 'array' | 'boolean' | 'integer' | 'null' | 'number' | 'object' | 'string';
8
+ export interface Schema {
9
+ $id?: string;
10
+ $anchor?: string;
11
+ $recursiveAnchor?: boolean;
12
+ $ref?: string;
13
+ $recursiveRef?: '#';
14
+ $schema?: string;
15
+ $comment?: string;
16
+ $defs?: any;
17
+ $vocabulary?: Record<string, boolean>;
18
+ type?: InstanceType | InstanceType[];
19
+ const?: any;
20
+ enum?: any[];
21
+ required?: string[];
22
+ not?: Schema;
23
+ anyOf?: Schema[];
24
+ allOf?: Schema[];
25
+ oneOf?: Schema[];
26
+ if?: Schema;
27
+ then?: Schema;
28
+ else?: Schema;
29
+ format?: string;
30
+ properties?: Record<string | number, Schema | boolean>;
31
+ patternProperties?: Record<string, Schema | boolean>;
32
+ additionalProperties?: Schema | boolean;
33
+ unevaluatedProperties?: Schema | boolean;
34
+ minProperties?: number;
35
+ maxProperties?: number;
36
+ propertyNames?: Schema;
37
+ dependentRequired?: Record<string, string[]>;
38
+ dependentSchemas?: Record<string, Schema>;
39
+ dependencies?: Record<string, Schema | string[]>;
40
+ prefixItems?: Array<Schema | boolean>;
41
+ items?: Schema | boolean | Array<Schema | boolean>;
42
+ additionalItems?: Schema | boolean;
43
+ unevaluatedItems?: Schema | boolean;
44
+ contains?: Schema | boolean;
45
+ minContains?: number;
46
+ maxContains?: number;
47
+ minItems?: number;
48
+ maxItems?: number;
49
+ uniqueItems?: boolean;
50
+ minimum?: number;
51
+ maximum?: number;
52
+ exclusiveMinimum?: number | boolean;
53
+ exclusiveMaximum?: number | boolean;
54
+ multipleOf?: number;
55
+ minLength?: number;
56
+ maxLength?: number;
57
+ pattern?: string;
58
+ __absolute_ref__?: string;
59
+ __absolute_recursive_ref__?: string;
60
+ __absolute_uri__?: string;
61
+ [key: string]: any;
62
+ }
63
+ export interface OutputUnit {
64
+ keyword: string;
65
+ keywordLocation: string;
66
+ instanceLocation: string;
67
+ error: string;
68
+ }
69
+ export interface ValidationResult {
70
+ valid: boolean;
71
+ errors: OutputUnit[];
72
+ }
@@ -0,0 +1 @@
1
+ export declare function ucs2length(s: string): number;
@@ -0,0 +1,3 @@
1
+ import { Schema, SchemaDraft, ValidationResult } from './types.js';
2
+ export type Evaluated = Record<string | number, boolean>;
3
+ export declare function validate(instance: any, schema: Schema | boolean, draft?: SchemaDraft, lookup?: Record<string, Schema | boolean>, shortCircuit?: boolean, recursiveAnchor?: Schema | null, instanceLocation?: string, schemaLocation?: string, evaluated?: Evaluated): ValidationResult;
@@ -0,0 +1,10 @@
1
+ import { Schema, SchemaDraft, ValidationResult } from './types.js';
2
+ export declare class Validator {
3
+ private readonly schema;
4
+ private readonly draft;
5
+ private readonly shortCircuit;
6
+ private readonly lookup;
7
+ constructor(schema: Schema | boolean, draft?: SchemaDraft, shortCircuit?: boolean);
8
+ validate(instance: any): ValidationResult;
9
+ addSchema(schema: Schema, id?: string): void;
10
+ }
package/package.json CHANGED
@@ -1,8 +1,7 @@
1
1
  {
2
2
  "name": "@cfworker/json-schema",
3
- "version": "4.0.1",
3
+ "version": "4.0.3",
4
4
  "description": "A JSON schema validator that will run on Cloudflare workers. Supports drafts 4, 7, 2019-09, and 2020-12.",
5
- "type": "module",
6
5
  "keywords": [
7
6
  "json-schema",
8
7
  "jsonschema",
@@ -13,50 +12,57 @@
13
12
  "workers",
14
13
  "service-worker"
15
14
  ],
15
+ "repository": "https://github.com/cfworker/cfworker",
16
+ "author": "Jeremy Danyow <jdanyow@gmail.com>",
17
+ "homepage": "https://github.com/cfworker/cfworker/tree/master/packages/json-schema/README.md",
18
+ "license": "MIT",
19
+ "publishConfig": {
20
+ "access": "public"
21
+ },
22
+ "type": "module",
16
23
  "sideEffects": false,
17
- "main": "dist/cjs/index.js",
18
- "module": "dist/esm/index.js",
19
- "source": "src/index.ts",
20
- "types": "dist/types/index.d.ts",
24
+ "main": "./dist/commonjs/index.js",
25
+ "types": "./dist/commonjs/index.d.ts",
26
+ "module": "./dist/esm/index.js",
21
27
  "exports": {
28
+ "./package.json": "./package.json",
22
29
  ".": {
23
30
  "import": {
24
- "default": "./dist/esm/index.js",
25
- "types": "./dist/types/index.d.ts"
31
+ "types": "./dist/esm/index.d.ts",
32
+ "default": "./dist/esm/index.js"
26
33
  },
27
34
  "require": {
28
- "default": "./dist/cjs/index.js",
29
- "types": "./dist/types/index.d.ts"
35
+ "types": "./dist/commonjs/index.d.ts",
36
+ "default": "./dist/commonjs/index.js"
30
37
  }
31
38
  }
32
39
  },
33
40
  "files": [
34
- "dist/**/*",
35
- "src/**/*",
36
- "README.md",
37
- "package.json"
41
+ "dist",
42
+ "src",
43
+ "README.md"
38
44
  ],
39
- "repository": "https://github.com/cfworker/cfworker",
40
- "author": "Jeremy Danyow <jdanyow@gmail.com>",
41
- "homepage": "https://github.com/cfworker/cfworker/tree/master/packages/json-schema/README.md",
42
- "license": "MIT",
43
- "publishConfig": {
44
- "access": "public"
45
- },
46
45
  "scripts": {
47
- "build": "tsc --build & tsc --build tsconfig.cjs.json",
48
- "clean": "tsc --build --clean && tsc --build tsconfig.cjs.json --clean",
46
+ "prepare": "tshy",
47
+ "build": "tsc --build",
48
+ "clean": "tsc --build --clean",
49
49
  "pretest": "node --experimental-import-meta-resolve suite-gen.mjs && esbuild test/test.ts --target=esnext --bundle --format=esm --conditions=worker,browser --outdir=dist-test --ignore-annotations",
50
50
  "test": "node ../../test.mjs"
51
51
  },
52
52
  "devDependencies": {
53
53
  "@types/chai": "^5.0.1",
54
- "@types/mocha": "^10.0.9",
54
+ "@types/mocha": "^10.0.10",
55
55
  "chai": "^5.1.2",
56
56
  "json-schema-test-suite": "git+https://github.com/json-schema-org/JSON-Schema-Test-Suite#76b529f",
57
57
  "esbuild": "^0.24.0",
58
- "mocha": "^10.8.2",
59
- "typescript": "^5.6.3",
60
- "wrangler": "^3.87.0"
58
+ "mocha": "^11.0.1",
59
+ "typescript": "^5.7.2",
60
+ "wrangler": "^3.95.0"
61
+ },
62
+ "tshy": {
63
+ "exports": {
64
+ "./package.json": "./package.json",
65
+ ".": "./src/index.ts"
66
+ }
61
67
  }
62
68
  }
@@ -0,0 +1 @@
1
+ export declare function deepCompareStrict(a: any, b: any): boolean;
@@ -0,0 +1,39 @@
1
+ export function deepCompareStrict(a, b) {
2
+ const typeofa = typeof a;
3
+ if (typeofa !== typeof b) {
4
+ return false;
5
+ }
6
+ if (Array.isArray(a)) {
7
+ if (!Array.isArray(b)) {
8
+ return false;
9
+ }
10
+ const length = a.length;
11
+ if (length !== b.length) {
12
+ return false;
13
+ }
14
+ for (let i = 0; i < length; i++) {
15
+ if (!deepCompareStrict(a[i], b[i])) {
16
+ return false;
17
+ }
18
+ }
19
+ return true;
20
+ }
21
+ if (typeofa === 'object') {
22
+ if (!a || !b) {
23
+ return a === b;
24
+ }
25
+ const aKeys = Object.keys(a);
26
+ const bKeys = Object.keys(b);
27
+ const length = aKeys.length;
28
+ if (length !== bKeys.length) {
29
+ return false;
30
+ }
31
+ for (const k of aKeys) {
32
+ if (!deepCompareStrict(a[k], b[k])) {
33
+ return false;
34
+ }
35
+ }
36
+ return true;
37
+ }
38
+ return a === b;
39
+ }
@@ -0,0 +1,12 @@
1
+ import { Schema } from './types.js';
2
+ export declare const schemaKeyword: Record<string, boolean>;
3
+ export declare const schemaArrayKeyword: Record<string, boolean>;
4
+ export declare const schemaMapKeyword: Record<string, boolean>;
5
+ export declare const ignoredKeyword: Record<string, boolean>;
6
+ export declare let initialBaseURI: URL;
7
+ export declare function dereference(
8
+ schema: Schema | boolean,
9
+ lookup?: Record<string, Schema | boolean>,
10
+ baseURI?: URL,
11
+ basePointer?: string
12
+ ): Record<string, Schema | boolean>;
@@ -0,0 +1,148 @@
1
+ import { encodePointer } from './pointer.js';
2
+ export const schemaKeyword = {
3
+ additionalItems: true,
4
+ unevaluatedItems: true,
5
+ items: true,
6
+ contains: true,
7
+ additionalProperties: true,
8
+ unevaluatedProperties: true,
9
+ propertyNames: true,
10
+ not: true,
11
+ if: true,
12
+ then: true,
13
+ else: true
14
+ };
15
+ export const schemaArrayKeyword = {
16
+ prefixItems: true,
17
+ items: true,
18
+ allOf: true,
19
+ anyOf: true,
20
+ oneOf: true
21
+ };
22
+ export const schemaMapKeyword = {
23
+ $defs: true,
24
+ definitions: true,
25
+ properties: true,
26
+ patternProperties: true,
27
+ dependentSchemas: true
28
+ };
29
+ export const ignoredKeyword = {
30
+ id: true,
31
+ $id: true,
32
+ $ref: true,
33
+ $schema: true,
34
+ $anchor: true,
35
+ $vocabulary: true,
36
+ $comment: true,
37
+ default: true,
38
+ enum: true,
39
+ const: true,
40
+ required: true,
41
+ type: true,
42
+ maximum: true,
43
+ minimum: true,
44
+ exclusiveMaximum: true,
45
+ exclusiveMinimum: true,
46
+ multipleOf: true,
47
+ maxLength: true,
48
+ minLength: true,
49
+ pattern: true,
50
+ format: true,
51
+ maxItems: true,
52
+ minItems: true,
53
+ uniqueItems: true,
54
+ maxProperties: true,
55
+ minProperties: true
56
+ };
57
+ export let initialBaseURI =
58
+ typeof self !== 'undefined' &&
59
+ self.location &&
60
+ self.location.origin !== 'null'
61
+ ? new URL(self.location.origin + self.location.pathname + location.search)
62
+ : new URL('https://github.com/cfworker');
63
+ export function dereference(
64
+ schema,
65
+ lookup = Object.create(null),
66
+ baseURI = initialBaseURI,
67
+ basePointer = ''
68
+ ) {
69
+ if (schema && typeof schema === 'object' && !Array.isArray(schema)) {
70
+ const id = schema.$id || schema.id;
71
+ if (id) {
72
+ const url = new URL(id, baseURI.href);
73
+ if (url.hash.length > 1) {
74
+ lookup[url.href] = schema;
75
+ } else {
76
+ url.hash = '';
77
+ if (basePointer === '') {
78
+ baseURI = url;
79
+ } else {
80
+ dereference(schema, lookup, baseURI);
81
+ }
82
+ }
83
+ }
84
+ } else if (schema !== true && schema !== false) {
85
+ return lookup;
86
+ }
87
+ const schemaURI = baseURI.href + (basePointer ? '#' + basePointer : '');
88
+ if (lookup[schemaURI] !== undefined) {
89
+ throw new Error(`Duplicate schema URI "${schemaURI}".`);
90
+ }
91
+ lookup[schemaURI] = schema;
92
+ if (schema === true || schema === false) {
93
+ return lookup;
94
+ }
95
+ if (schema.__absolute_uri__ === undefined) {
96
+ Object.defineProperty(schema, '__absolute_uri__', {
97
+ enumerable: false,
98
+ value: schemaURI
99
+ });
100
+ }
101
+ if (schema.$ref && schema.__absolute_ref__ === undefined) {
102
+ const url = new URL(schema.$ref, baseURI.href);
103
+ url.hash = url.hash;
104
+ Object.defineProperty(schema, '__absolute_ref__', {
105
+ enumerable: false,
106
+ value: url.href
107
+ });
108
+ }
109
+ if (schema.$recursiveRef && schema.__absolute_recursive_ref__ === undefined) {
110
+ const url = new URL(schema.$recursiveRef, baseURI.href);
111
+ url.hash = url.hash;
112
+ Object.defineProperty(schema, '__absolute_recursive_ref__', {
113
+ enumerable: false,
114
+ value: url.href
115
+ });
116
+ }
117
+ if (schema.$anchor) {
118
+ const url = new URL('#' + schema.$anchor, baseURI.href);
119
+ lookup[url.href] = schema;
120
+ }
121
+ for (let key in schema) {
122
+ if (ignoredKeyword[key]) {
123
+ continue;
124
+ }
125
+ const keyBase = `${basePointer}/${encodePointer(key)}`;
126
+ const subSchema = schema[key];
127
+ if (Array.isArray(subSchema)) {
128
+ if (schemaArrayKeyword[key]) {
129
+ const length = subSchema.length;
130
+ for (let i = 0; i < length; i++) {
131
+ dereference(subSchema[i], lookup, baseURI, `${keyBase}/${i}`);
132
+ }
133
+ }
134
+ } else if (schemaMapKeyword[key]) {
135
+ for (let subKey in subSchema) {
136
+ dereference(
137
+ subSchema[subKey],
138
+ lookup,
139
+ baseURI,
140
+ `${keyBase}/${encodePointer(subKey)}`
141
+ );
142
+ }
143
+ } else {
144
+ dereference(subSchema, lookup, baseURI, keyBase);
145
+ }
146
+ }
147
+ return lookup;
148
+ }
@@ -0,0 +1,2 @@
1
+ export declare const fullFormat: Record<string, (s: string) => boolean>;
2
+ export declare const fastFormat: Record<string, (s: string) => boolean>;
package/src/format.js ADDED
@@ -0,0 +1,137 @@
1
+ const DATE = /^(\d\d\d\d)-(\d\d)-(\d\d)$/;
2
+ const DAYS = [0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31];
3
+ const TIME = /^(\d\d):(\d\d):(\d\d)(\.\d+)?(z|[+-]\d\d(?::?\d\d)?)?$/i;
4
+ const HOSTNAME =
5
+ /^(?=.{1,253}\.?$)[a-z0-9](?:[a-z0-9-]{0,61}[a-z0-9])?(?:\.[a-z0-9](?:[-0-9a-z]{0,61}[0-9a-z])?)*\.?$/i;
6
+ const URIREF =
7
+ /^(?:[a-z][a-z0-9+\-.]*:)?(?:\/?\/(?:(?:[a-z0-9\-._~!$&'()*+,;=:]|%[0-9a-f]{2})*@)?(?:\[(?:(?:(?:(?:[0-9a-f]{1,4}:){6}|::(?:[0-9a-f]{1,4}:){5}|(?:[0-9a-f]{1,4})?::(?:[0-9a-f]{1,4}:){4}|(?:(?:[0-9a-f]{1,4}:){0,1}[0-9a-f]{1,4})?::(?:[0-9a-f]{1,4}:){3}|(?:(?:[0-9a-f]{1,4}:){0,2}[0-9a-f]{1,4})?::(?:[0-9a-f]{1,4}:){2}|(?:(?:[0-9a-f]{1,4}:){0,3}[0-9a-f]{1,4})?::[0-9a-f]{1,4}:|(?:(?:[0-9a-f]{1,4}:){0,4}[0-9a-f]{1,4})?::)(?:[0-9a-f]{1,4}:[0-9a-f]{1,4}|(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?))|(?:(?:[0-9a-f]{1,4}:){0,5}[0-9a-f]{1,4})?::[0-9a-f]{1,4}|(?:(?:[0-9a-f]{1,4}:){0,6}[0-9a-f]{1,4})?::)|[Vv][0-9a-f]+\.[a-z0-9\-._~!$&'()*+,;=:]+)\]|(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)|(?:[a-z0-9\-._~!$&'"()*+,;=]|%[0-9a-f]{2})*)(?::\d*)?(?:\/(?:[a-z0-9\-._~!$&'"()*+,;=:@]|%[0-9a-f]{2})*)*|\/(?:(?:[a-z0-9\-._~!$&'"()*+,;=:@]|%[0-9a-f]{2})+(?:\/(?:[a-z0-9\-._~!$&'"()*+,;=:@]|%[0-9a-f]{2})*)*)?|(?:[a-z0-9\-._~!$&'"()*+,;=:@]|%[0-9a-f]{2})+(?:\/(?:[a-z0-9\-._~!$&'"()*+,;=:@]|%[0-9a-f]{2})*)*)?(?:\?(?:[a-z0-9\-._~!$&'"()*+,;=:@/?]|%[0-9a-f]{2})*)?(?:#(?:[a-z0-9\-._~!$&'"()*+,;=:@/?]|%[0-9a-f]{2})*)?$/i;
8
+ const URITEMPLATE =
9
+ /^(?:(?:[^\x00-\x20"'<>%\\^`{|}]|%[0-9a-f]{2})|\{[+#./;?&=,!@|]?(?:[a-z0-9_]|%[0-9a-f]{2})+(?::[1-9][0-9]{0,3}|\*)?(?:,(?:[a-z0-9_]|%[0-9a-f]{2})+(?::[1-9][0-9]{0,3}|\*)?)*\})*$/i;
10
+ const URL_ =
11
+ /^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?@)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u{00a1}-\u{ffff}0-9]+-?)*[a-z\u{00a1}-\u{ffff}0-9]+)(?:\.(?:[a-z\u{00a1}-\u{ffff}0-9]+-?)*[a-z\u{00a1}-\u{ffff}0-9]+)*(?:\.(?:[a-z\u{00a1}-\u{ffff}]{2,})))(?::\d{2,5})?(?:\/[^\s]*)?$/iu;
12
+ const UUID = /^(?:urn:uuid:)?[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/i;
13
+ const JSON_POINTER = /^(?:\/(?:[^~/]|~0|~1)*)*$/;
14
+ const JSON_POINTER_URI_FRAGMENT =
15
+ /^#(?:\/(?:[a-z0-9_\-.!$&'()*+,;:=@]|%[0-9a-f]{2}|~0|~1)*)*$/i;
16
+ const RELATIVE_JSON_POINTER =
17
+ /^(?:0|[1-9][0-9]*)(?:#|(?:\/(?:[^~/]|~0|~1)*)*)$/;
18
+ const FASTDATE = /^\d\d\d\d-[0-1]\d-[0-3]\d$/;
19
+ const FASTTIME =
20
+ /^(?:[0-2]\d:[0-5]\d:[0-5]\d|23:59:60)(?:\.\d+)?(?:z|[+-]\d\d(?::?\d\d)?)?$/i;
21
+ const FASTDATETIME =
22
+ /^\d\d\d\d-[0-1]\d-[0-3]\d[t\s](?:[0-2]\d:[0-5]\d:[0-5]\d|23:59:60)(?:\.\d+)?(?:z|[+-]\d\d(?::?\d\d)?)$/i;
23
+ const FASTURIREFERENCE =
24
+ /^(?:(?:[a-z][a-z0-9+-.]*:)?\/?\/)?(?:[^\\\s#][^\s#]*)?(?:#[^\\\s]*)?$/i;
25
+ const EMAIL = input => {
26
+ if (input[0] === '"') return false;
27
+ const [name, host, ...rest] = input.split('@');
28
+ if (
29
+ !name ||
30
+ !host ||
31
+ rest.length !== 0 ||
32
+ name.length > 64 ||
33
+ host.length > 253
34
+ )
35
+ return false;
36
+ if (name[0] === '.' || name.endsWith('.') || name.includes('..'))
37
+ return false;
38
+ if (
39
+ !/^[a-z0-9.-]+$/i.test(host) ||
40
+ !/^[a-z0-9.!#$%&'*+/=?^_`{|}~-]+$/i.test(name)
41
+ )
42
+ return false;
43
+ return host
44
+ .split('.')
45
+ .every(part => /^[a-z0-9]([a-z0-9-]{0,61}[a-z0-9])?$/i.test(part));
46
+ };
47
+ const IPV4 =
48
+ /^(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)$/;
49
+ const IPV6 =
50
+ /^((([0-9a-f]{1,4}:){7}([0-9a-f]{1,4}|:))|(([0-9a-f]{1,4}:){6}(:[0-9a-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9a-f]{1,4}:){5}(((:[0-9a-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9a-f]{1,4}:){4}(((:[0-9a-f]{1,4}){1,3})|((:[0-9a-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-f]{1,4}:){3}(((:[0-9a-f]{1,4}){1,4})|((:[0-9a-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-f]{1,4}:){2}(((:[0-9a-f]{1,4}){1,5})|((:[0-9a-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9a-f]{1,4}:){1}(((:[0-9a-f]{1,4}){1,6})|((:[0-9a-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9a-f]{1,4}){1,7})|((:[0-9a-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))$/i;
51
+ const DURATION = input =>
52
+ input.length > 1 &&
53
+ input.length < 80 &&
54
+ (/^P\d+([.,]\d+)?W$/.test(input) ||
55
+ (/^P[\dYMDTHS]*(\d[.,]\d+)?[YMDHS]$/.test(input) &&
56
+ /^P([.,\d]+Y)?([.,\d]+M)?([.,\d]+D)?(T([.,\d]+H)?([.,\d]+M)?([.,\d]+S)?)?$/.test(
57
+ input
58
+ )));
59
+ function bind(r) {
60
+ return r.test.bind(r);
61
+ }
62
+ export const fullFormat = {
63
+ date,
64
+ time: time.bind(undefined, false),
65
+ 'date-time': date_time,
66
+ duration: DURATION,
67
+ uri,
68
+ 'uri-reference': bind(URIREF),
69
+ 'uri-template': bind(URITEMPLATE),
70
+ url: bind(URL_),
71
+ email: EMAIL,
72
+ hostname: bind(HOSTNAME),
73
+ ipv4: bind(IPV4),
74
+ ipv6: bind(IPV6),
75
+ regex: regex,
76
+ uuid: bind(UUID),
77
+ 'json-pointer': bind(JSON_POINTER),
78
+ 'json-pointer-uri-fragment': bind(JSON_POINTER_URI_FRAGMENT),
79
+ 'relative-json-pointer': bind(RELATIVE_JSON_POINTER)
80
+ };
81
+ export const fastFormat = {
82
+ ...fullFormat,
83
+ date: bind(FASTDATE),
84
+ time: bind(FASTTIME),
85
+ 'date-time': bind(FASTDATETIME),
86
+ 'uri-reference': bind(FASTURIREFERENCE)
87
+ };
88
+ function isLeapYear(year) {
89
+ return year % 4 === 0 && (year % 100 !== 0 || year % 400 === 0);
90
+ }
91
+ function date(str) {
92
+ const matches = str.match(DATE);
93
+ if (!matches) return false;
94
+ const year = +matches[1];
95
+ const month = +matches[2];
96
+ const day = +matches[3];
97
+ return (
98
+ month >= 1 &&
99
+ month <= 12 &&
100
+ day >= 1 &&
101
+ day <= (month == 2 && isLeapYear(year) ? 29 : DAYS[month])
102
+ );
103
+ }
104
+ function time(full, str) {
105
+ const matches = str.match(TIME);
106
+ if (!matches) return false;
107
+ const hour = +matches[1];
108
+ const minute = +matches[2];
109
+ const second = +matches[3];
110
+ const timeZone = !!matches[5];
111
+ return (
112
+ ((hour <= 23 && minute <= 59 && second <= 59) ||
113
+ (hour == 23 && minute == 59 && second == 60)) &&
114
+ (!full || timeZone)
115
+ );
116
+ }
117
+ const DATE_TIME_SEPARATOR = /t|\s/i;
118
+ function date_time(str) {
119
+ const dateTime = str.split(DATE_TIME_SEPARATOR);
120
+ return dateTime.length == 2 && date(dateTime[0]) && time(true, dateTime[1]);
121
+ }
122
+ const NOT_URI_FRAGMENT = /\/|:/;
123
+ const URI_PATTERN =
124
+ /^(?:[a-z][a-z0-9+\-.]*:)(?:\/?\/(?:(?:[a-z0-9\-._~!$&'()*+,;=:]|%[0-9a-f]{2})*@)?(?:\[(?:(?:(?:(?:[0-9a-f]{1,4}:){6}|::(?:[0-9a-f]{1,4}:){5}|(?:[0-9a-f]{1,4})?::(?:[0-9a-f]{1,4}:){4}|(?:(?:[0-9a-f]{1,4}:){0,1}[0-9a-f]{1,4})?::(?:[0-9a-f]{1,4}:){3}|(?:(?:[0-9a-f]{1,4}:){0,2}[0-9a-f]{1,4})?::(?:[0-9a-f]{1,4}:){2}|(?:(?:[0-9a-f]{1,4}:){0,3}[0-9a-f]{1,4})?::[0-9a-f]{1,4}:|(?:(?:[0-9a-f]{1,4}:){0,4}[0-9a-f]{1,4})?::)(?:[0-9a-f]{1,4}:[0-9a-f]{1,4}|(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?))|(?:(?:[0-9a-f]{1,4}:){0,5}[0-9a-f]{1,4})?::[0-9a-f]{1,4}|(?:(?:[0-9a-f]{1,4}:){0,6}[0-9a-f]{1,4})?::)|[Vv][0-9a-f]+\.[a-z0-9\-._~!$&'()*+,;=:]+)\]|(?:(?:25[0-5]|2[0-4]\d|[01]?\d\d?)\.){3}(?:25[0-5]|2[0-4]\d|[01]?\d\d?)|(?:[a-z0-9\-._~!$&'()*+,;=]|%[0-9a-f]{2})*)(?::\d*)?(?:\/(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})*)*|\/(?:(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})+(?:\/(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})*)*)?|(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})+(?:\/(?:[a-z0-9\-._~!$&'()*+,;=:@]|%[0-9a-f]{2})*)*)(?:\?(?:[a-z0-9\-._~!$&'()*+,;=:@/?]|%[0-9a-f]{2})*)?(?:#(?:[a-z0-9\-._~!$&'()*+,;=:@/?]|%[0-9a-f]{2})*)?$/i;
125
+ function uri(str) {
126
+ return NOT_URI_FRAGMENT.test(str) && URI_PATTERN.test(str);
127
+ }
128
+ const Z_ANCHOR = /[^\\]\\Z/;
129
+ function regex(str) {
130
+ if (Z_ANCHOR.test(str)) return false;
131
+ try {
132
+ new RegExp(str, 'u');
133
+ return true;
134
+ } catch (e) {
135
+ return false;
136
+ }
137
+ }
package/src/index.d.ts ADDED
@@ -0,0 +1,8 @@
1
+ export * from './deep-compare-strict.js';
2
+ export * from './dereference.js';
3
+ export * from './format.js';
4
+ export * from './pointer.js';
5
+ export * from './types.js';
6
+ export * from './ucs2-length.js';
7
+ export * from './validate.js';
8
+ export * from './validator.js';
package/src/index.js ADDED
@@ -0,0 +1,8 @@
1
+ export * from './deep-compare-strict.js';
2
+ export * from './dereference.js';
3
+ export * from './format.js';
4
+ export * from './pointer.js';
5
+ export * from './types.js';
6
+ export * from './ucs2-length.js';
7
+ export * from './validate.js';
8
+ export * from './validator.js';
@@ -0,0 +1,2 @@
1
+ export declare function encodePointer(p: string): string;
2
+ export declare function escapePointer(p: string): string;
package/src/pointer.js ADDED
@@ -0,0 +1,6 @@
1
+ export function encodePointer(p) {
2
+ return encodeURI(escapePointer(p));
3
+ }
4
+ export function escapePointer(p) {
5
+ return p.replace(/~/g, '~0').replace(/\//g, '~1');
6
+ }