z-schema 7.0.0 → 7.0.6
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.
- package/README.md +7 -11
- package/bin/z-schema +0 -0
- package/cjs/index.d.ts +192 -117
- package/cjs/index.js +949 -998
- package/{src/FormatValidators.ts → dist/format-validators.js} +97 -65
- package/dist/index.js +1 -1
- package/dist/json-schema.js +40 -0
- package/dist/{JsonValidation.js → json-validation.js} +75 -69
- package/dist/{Report.js → report.js} +35 -45
- package/dist/schema-cache.js +109 -0
- package/dist/schema-compiler.js +255 -0
- package/dist/{SchemaValidation.js → schema-validator.js} +153 -149
- package/dist/types/{Errors.d.ts → errors.d.ts} +2 -0
- package/dist/types/format-validators.d.ts +10 -0
- package/dist/types/index.d.ts +10 -1
- package/dist/types/json-schema.d.ts +50 -0
- package/dist/types/json-validation.d.ts +7 -0
- package/dist/types/{Report.d.ts → report.d.ts} +22 -23
- package/dist/types/schema-cache.d.ts +17 -0
- package/dist/types/schema-compiler.d.ts +16 -0
- package/dist/types/schema-validator.d.ts +10 -0
- package/dist/types/utils/array.d.ts +2 -0
- package/dist/types/utils/clone.d.ts +2 -0
- package/dist/types/utils/json.d.ts +7 -0
- package/dist/types/utils/symbols.d.ts +2 -0
- package/dist/types/utils/unicode.d.ts +14 -0
- package/dist/types/utils/uri.d.ts +4 -0
- package/dist/types/utils/what-is.d.ts +3 -0
- package/dist/types/z-schema.d.ts +75 -0
- package/dist/utils/array.js +27 -0
- package/dist/utils/clone.js +61 -0
- package/dist/utils/json.js +59 -0
- package/dist/utils/symbols.js +2 -0
- package/dist/utils/unicode.js +45 -0
- package/dist/utils/uri.js +15 -0
- package/dist/utils/what-is.js +29 -0
- package/dist/{ZSchema.js → z-schema.js} +66 -77
- package/package.json +8 -4
- package/src/{Errors.ts → errors.ts} +4 -0
- package/src/format-validators.ts +191 -0
- package/src/index.ts +12 -1
- package/src/json-schema.ts +97 -0
- package/src/{JsonValidation.ts → json-validation.ts} +137 -127
- package/src/{Report.ts → report.ts} +60 -70
- package/src/schema-cache.ts +122 -0
- package/src/schema-compiler.ts +300 -0
- package/src/{SchemaValidation.ts → schema-validator.ts} +213 -215
- package/src/utils/array.ts +29 -0
- package/src/utils/clone.ts +63 -0
- package/src/utils/json.ts +74 -0
- package/src/utils/symbols.ts +3 -0
- package/src/utils/unicode.ts +43 -0
- package/src/utils/uri.ts +18 -0
- package/src/utils/what-is.ts +46 -0
- package/src/{ZSchema.ts → z-schema.ts} +108 -113
- package/umd/ZSchema.js +949 -998
- package/umd/ZSchema.min.js +1 -1
- package/dist/FormatValidators.js +0 -136
- package/dist/SchemaCache.js +0 -173
- package/dist/SchemaCompilation.js +0 -259
- package/dist/Utils.js +0 -266
- package/dist/types/FormatValidators.d.ts +0 -12
- package/dist/types/JsonValidation.d.ts +0 -37
- package/dist/types/SchemaCache.d.ts +0 -26
- package/dist/types/SchemaCompilation.d.ts +0 -1
- package/dist/types/SchemaValidation.d.ts +0 -6
- package/dist/types/Utils.d.ts +0 -64
- package/dist/types/ZSchema.d.ts +0 -97
- package/src/SchemaCache.ts +0 -189
- package/src/SchemaCompilation.ts +0 -293
- package/src/Utils.ts +0 -286
- /package/dist/{Errors.js → errors.js} +0 -0
- /package/dist/schemas/{hyper-schema.json → draft-04-hyper-schema.json} +0 -0
- /package/dist/schemas/{schema.json → draft-04-schema.json} +0 -0
- /package/src/schemas/{hyper-schema.json → draft-04-hyper-schema.json} +0 -0
- /package/src/schemas/{schema.json → draft-04-schema.json} +0 -0
|
@@ -1,4 +1,6 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ErrorCode, ErrorParam } from './errors.js';
|
|
2
|
+
import { ValidateCallback, ZSchemaOptions } from './z-schema.js';
|
|
3
|
+
import { JsonSchema, JsonSchemaInternal } from './json-schema.js';
|
|
2
4
|
export interface SchemaError extends Error {
|
|
3
5
|
/**
|
|
4
6
|
* Implements the Error.name contract. The value is always "z-schema validation error".
|
|
@@ -29,12 +31,12 @@ export interface SchemaErrorDetail {
|
|
|
29
31
|
* Format parameters that can be used to format a custom error message.
|
|
30
32
|
* Example: ["string","array"]
|
|
31
33
|
*/
|
|
32
|
-
params:
|
|
34
|
+
params: ErrorParam[];
|
|
33
35
|
/**
|
|
34
36
|
* A JSON path indicating the location of the error.
|
|
35
37
|
* Example: "#/projects/1"
|
|
36
38
|
*/
|
|
37
|
-
path: string | string
|
|
39
|
+
path: string | Array<string | number>;
|
|
38
40
|
/**
|
|
39
41
|
* The schema rule description, which is included for certain errors where
|
|
40
42
|
* this information is useful (e.g. to describe a constraint).
|
|
@@ -58,30 +60,27 @@ type TaskFnArgs = Parameters<TaskFn>;
|
|
|
58
60
|
type TaskProcessFn = (result: ReturnType<TaskFn>) => void;
|
|
59
61
|
type AsyncTask = [TaskFn, TaskFnArgs, TaskProcessFn];
|
|
60
62
|
export declare class Report {
|
|
63
|
+
asyncTasks: AsyncTask[];
|
|
64
|
+
commonErrorMessage?: string;
|
|
61
65
|
errors: SchemaErrorDetail[];
|
|
66
|
+
json?: unknown;
|
|
67
|
+
path: Array<number | string>;
|
|
68
|
+
rootSchema?: JsonSchemaInternal;
|
|
62
69
|
parentReport?: Report;
|
|
63
70
|
options: ZSchemaOptions;
|
|
64
71
|
reportOptions: ReportOptions;
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
id?: string;
|
|
69
|
-
};
|
|
70
|
-
commonErrorMessage?: string;
|
|
71
|
-
json?: unknown;
|
|
72
|
-
constructor(parentOrOptions: any, reportOptions?: any);
|
|
72
|
+
constructor(zschemaOptions: ZSchemaOptions);
|
|
73
|
+
constructor(parentReport: Report);
|
|
74
|
+
constructor(parentReport: Report, reportOptions: ReportOptions);
|
|
73
75
|
isValid(): boolean;
|
|
74
|
-
addAsyncTask(fn:
|
|
75
|
-
getAncestor(id:
|
|
76
|
-
processAsyncTasks(timeout:
|
|
77
|
-
getPath(returnPathAsString
|
|
78
|
-
getSchemaId():
|
|
79
|
-
hasError(
|
|
80
|
-
addError(
|
|
81
|
-
getJson():
|
|
82
|
-
addCustomError(errorCode: string, errorMessage: string, params
|
|
83
|
-
title?: string;
|
|
84
|
-
description?: string;
|
|
85
|
-
}): void;
|
|
76
|
+
addAsyncTask<FV, FN extends (...args: any[]) => FV>(fn: FN, args: Parameters<FN>, asyncTaskResultProcessFn: (result: ReturnType<FN>) => void): void;
|
|
77
|
+
getAncestor(id: string): Report | undefined;
|
|
78
|
+
processAsyncTasks(timeout: number | undefined, callback: ValidateCallback): void;
|
|
79
|
+
getPath(returnPathAsString?: boolean): string | (string | number)[];
|
|
80
|
+
getSchemaId(): string | undefined;
|
|
81
|
+
hasError(errCode: string, errParams: Array<any>): boolean;
|
|
82
|
+
addError(errCode: ErrorCode, errParams?: ErrorParam[], subReports?: Report | Report[], schema?: JsonSchema): void;
|
|
83
|
+
getJson(): unknown;
|
|
84
|
+
addCustomError(errorCode: string, errorMessage: string, params?: ErrorParam[], subReports?: Report | Report[], schema?: JsonSchema): void;
|
|
86
85
|
}
|
|
87
86
|
export {};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import type { ZSchema } from './z-schema.js';
|
|
2
|
+
import { Report } from './report.js';
|
|
3
|
+
import { JsonSchemaInternal } from './json-schema.js';
|
|
4
|
+
export type SchemaCacheStorage = Record<string, JsonSchemaInternal>;
|
|
5
|
+
export type ReferenceSchemaCacheStorage = Array<[JsonSchemaInternal, JsonSchemaInternal]>;
|
|
6
|
+
export declare class SchemaCache {
|
|
7
|
+
private validator;
|
|
8
|
+
cache: SchemaCacheStorage;
|
|
9
|
+
referenceCache: ReferenceSchemaCacheStorage;
|
|
10
|
+
constructor(validator: ZSchema);
|
|
11
|
+
cacheSchemaByUri(uri: string, schema: JsonSchemaInternal): void;
|
|
12
|
+
removeFromCacheByUri(uri: string): void;
|
|
13
|
+
checkCacheForUri(uri: string): boolean;
|
|
14
|
+
getSchema(report: Report, refOrSchema: string | JsonSchemaInternal): JsonSchemaInternal | undefined;
|
|
15
|
+
getSchemaByUri(report: Report, uri: string, root?: JsonSchemaInternal): JsonSchemaInternal | undefined;
|
|
16
|
+
getSchemaByReference(report: Report, schema: JsonSchemaInternal): JsonSchemaInternal;
|
|
17
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { ZSchema } from './z-schema.js';
|
|
2
|
+
import { JsonSchemaInternal } from './json-schema.js';
|
|
3
|
+
import { Report } from './report.js';
|
|
4
|
+
export interface Reference {
|
|
5
|
+
ref: string;
|
|
6
|
+
key: '$ref' | '$schema';
|
|
7
|
+
obj: JsonSchemaInternal;
|
|
8
|
+
path: Array<string | number>;
|
|
9
|
+
}
|
|
10
|
+
export declare class SchemaCompiler {
|
|
11
|
+
private validator;
|
|
12
|
+
constructor(validator: ZSchema);
|
|
13
|
+
compileSchema(report: Report, schema: JsonSchemaInternal): boolean;
|
|
14
|
+
compileArrayOfSchemas(report: Report, arr: JsonSchemaInternal[]): boolean;
|
|
15
|
+
compileArrayOfSchemasLoop(mainReport: Report, arr: JsonSchemaInternal[]): number;
|
|
16
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { ZSchema } from './z-schema.js';
|
|
2
|
+
import { Report } from './report.js';
|
|
3
|
+
import { JsonSchemaInternal } from './json-schema.js';
|
|
4
|
+
export declare class SchemaValidator {
|
|
5
|
+
private validator;
|
|
6
|
+
constructor(validator: ZSchema);
|
|
7
|
+
get options(): import("./z-schema.js").ZSchemaOptions;
|
|
8
|
+
validateArrayOfSchemas(report: Report, arr: JsonSchemaInternal[]): boolean;
|
|
9
|
+
validateSchema(report: Report, schema: JsonSchemaInternal | JsonSchemaInternal[]): boolean;
|
|
10
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
interface AreEqualOptions {
|
|
2
|
+
caseInsensitiveComparison?: boolean;
|
|
3
|
+
}
|
|
4
|
+
export declare const areEqual: (json1: unknown, json2: unknown, options?: AreEqualOptions) => boolean;
|
|
5
|
+
export declare const decodeJSONPointer: (str: string) => string;
|
|
6
|
+
export declare const sortedKeys: (obj: Record<string, unknown>) => string[];
|
|
7
|
+
export {};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Creates an array containing the numeric code points of each Unicode
|
|
3
|
+
* character in the string. While JavaScript uses UCS-2 internally,
|
|
4
|
+
* this function will convert a pair of surrogate halves (each of which
|
|
5
|
+
* UCS-2 exposes as separate characters) into a single code point,
|
|
6
|
+
* matching UTF-16.
|
|
7
|
+
* @see `punycode.ucs2.encode`
|
|
8
|
+
* @see <https://mathiasbynens.be/notes/javascript-encoding>
|
|
9
|
+
* @memberOf punycode.ucs2
|
|
10
|
+
* @name decode
|
|
11
|
+
* @param {String} string The Unicode input string (UCS-2).
|
|
12
|
+
* @returns {Array} The new array of code points.
|
|
13
|
+
*/
|
|
14
|
+
export declare function ucs2decode(string: string): number[];
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export type WHAT_IS = 'undefined' | 'null' | 'object' | 'array' | 'integer' | 'number' | 'string' | 'not-a-number' | 'unknown-number' | 'bigint' | 'boolean' | 'symbol' | 'function';
|
|
2
|
+
export declare const whatIs: (what: unknown) => WHAT_IS;
|
|
3
|
+
export declare function isObject(value: unknown): value is Record<any, any>;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import { Report, SchemaError, SchemaErrorDetail } from './report.js';
|
|
2
|
+
import { FormatValidatorFn } from './format-validators.js';
|
|
3
|
+
import { SchemaCache } from './schema-cache.js';
|
|
4
|
+
import { SchemaCompiler } from './schema-compiler.js';
|
|
5
|
+
import { SchemaValidator } from './schema-validator.js';
|
|
6
|
+
import type { Errors } from './errors.js';
|
|
7
|
+
import type { JsonSchema, JsonSchemaInternal } from './json-schema.js';
|
|
8
|
+
export interface ZSchemaOptions {
|
|
9
|
+
asyncTimeout?: number;
|
|
10
|
+
forceAdditional?: boolean;
|
|
11
|
+
assumeAdditional?: boolean | string[];
|
|
12
|
+
enumCaseInsensitiveComparison?: boolean;
|
|
13
|
+
forceItems?: boolean;
|
|
14
|
+
forceMinItems?: boolean;
|
|
15
|
+
forceMaxItems?: boolean;
|
|
16
|
+
forceMinLength?: boolean;
|
|
17
|
+
forceMaxLength?: boolean;
|
|
18
|
+
forceProperties?: boolean;
|
|
19
|
+
ignoreUnresolvableReferences?: boolean;
|
|
20
|
+
noExtraKeywords?: boolean;
|
|
21
|
+
noTypeless?: boolean;
|
|
22
|
+
noEmptyStrings?: boolean;
|
|
23
|
+
noEmptyArrays?: boolean;
|
|
24
|
+
strictUris?: boolean;
|
|
25
|
+
strictMode?: boolean;
|
|
26
|
+
reportPathAsArray?: boolean;
|
|
27
|
+
breakOnFirstError?: boolean;
|
|
28
|
+
pedanticCheck?: boolean;
|
|
29
|
+
ignoreUnknownFormats?: boolean;
|
|
30
|
+
customValidator?: (report: Report, schema: unknown, json: unknown) => void;
|
|
31
|
+
}
|
|
32
|
+
export interface ValidateOptions {
|
|
33
|
+
schemaPath?: string;
|
|
34
|
+
includeErrors?: Array<keyof typeof Errors>;
|
|
35
|
+
}
|
|
36
|
+
export type ValidateCallback = (e: Error | SchemaErrorDetail[] | null, valid: boolean) => void;
|
|
37
|
+
type SchemaReader = (uri: string) => JsonSchema;
|
|
38
|
+
export declare class ZSchema {
|
|
39
|
+
static registerFormat(name: string, validatorFunction: FormatValidatorFn): void;
|
|
40
|
+
static unregisterFormat(name: string): void;
|
|
41
|
+
static getRegisteredFormats(): string[];
|
|
42
|
+
static getDefaultOptions(): ZSchemaOptions;
|
|
43
|
+
lastReport: Report | undefined;
|
|
44
|
+
scache: SchemaCache;
|
|
45
|
+
sc: SchemaCompiler;
|
|
46
|
+
sv: SchemaValidator;
|
|
47
|
+
validateOptions: ValidateOptions;
|
|
48
|
+
options: ZSchemaOptions;
|
|
49
|
+
constructor(options?: ZSchemaOptions);
|
|
50
|
+
validateSchema(schema: JsonSchemaInternal): boolean;
|
|
51
|
+
validate(json: unknown, schema: JsonSchema, options?: ValidateOptions, callback?: ValidateCallback): void;
|
|
52
|
+
validate(json: unknown, schema: JsonSchema, callback?: ValidateCallback): void;
|
|
53
|
+
validate(json: unknown, schema: JsonSchema): boolean;
|
|
54
|
+
/**
|
|
55
|
+
* Returns an Error object for the most recent failed validation, or null if the validation was successful.
|
|
56
|
+
*/
|
|
57
|
+
getLastError(): SchemaError | null;
|
|
58
|
+
/**
|
|
59
|
+
* Returns the error details for the most recent validation, or undefined if the validation was successful.
|
|
60
|
+
* This is the same list as the SchemaError.details property.
|
|
61
|
+
*/
|
|
62
|
+
getLastErrors(): SchemaErrorDetail[] | null;
|
|
63
|
+
setRemoteReference(uri: string, schema: string | JsonSchema, validationOptions: ZSchemaOptions): void;
|
|
64
|
+
compileSchema(schema: JsonSchema): boolean;
|
|
65
|
+
getMissingReferences(arr?: SchemaErrorDetail[]): string[];
|
|
66
|
+
getMissingRemoteReferences(): string[];
|
|
67
|
+
getResolvedSchema(schema: JsonSchemaInternal): JsonSchema;
|
|
68
|
+
static schemaReader: SchemaReader;
|
|
69
|
+
setSchemaReader(schemaReader: SchemaReader): void;
|
|
70
|
+
getSchemaReader(): SchemaReader;
|
|
71
|
+
static setSchemaReader(schemaReader: SchemaReader): void;
|
|
72
|
+
static schemaSymbol: symbol;
|
|
73
|
+
static jsonSymbol: symbol;
|
|
74
|
+
}
|
|
75
|
+
export {};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { areEqual } from './json.js';
|
|
2
|
+
export const isUniqueArray = (arr, indexes) => {
|
|
3
|
+
let i;
|
|
4
|
+
let j;
|
|
5
|
+
const l = arr.length;
|
|
6
|
+
for (i = 0; i < l; i++) {
|
|
7
|
+
for (j = i + 1; j < l; j++) {
|
|
8
|
+
if (areEqual(arr[i], arr[j])) {
|
|
9
|
+
if (indexes) {
|
|
10
|
+
indexes.push(i, j);
|
|
11
|
+
}
|
|
12
|
+
return false;
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
return true;
|
|
17
|
+
};
|
|
18
|
+
export const difference = (bigSet, subSet) => {
|
|
19
|
+
const arr = [];
|
|
20
|
+
let idx = bigSet.length;
|
|
21
|
+
while (idx--) {
|
|
22
|
+
if (subSet.indexOf(bigSet[idx]) === -1) {
|
|
23
|
+
arr.push(bigSet[idx]);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return arr;
|
|
27
|
+
};
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
export const shallowClone = (src) => {
|
|
2
|
+
if (src == null || typeof src !== 'object') {
|
|
3
|
+
return src;
|
|
4
|
+
}
|
|
5
|
+
let res;
|
|
6
|
+
let idx;
|
|
7
|
+
if (Array.isArray(src)) {
|
|
8
|
+
res = [];
|
|
9
|
+
idx = src.length;
|
|
10
|
+
while (idx--) {
|
|
11
|
+
res[idx] = src[idx];
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
else {
|
|
15
|
+
res = {};
|
|
16
|
+
const keys = Object.keys(src);
|
|
17
|
+
idx = keys.length;
|
|
18
|
+
while (idx--) {
|
|
19
|
+
const key = keys[idx];
|
|
20
|
+
res[key] = src[key];
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return res;
|
|
24
|
+
};
|
|
25
|
+
export const deepClone = (src) => {
|
|
26
|
+
let vidx = 0;
|
|
27
|
+
const visited = new Map();
|
|
28
|
+
const cloned = [];
|
|
29
|
+
const cloneDeepInner = (src) => {
|
|
30
|
+
if (typeof src !== 'object' || src === null) {
|
|
31
|
+
return src;
|
|
32
|
+
}
|
|
33
|
+
let res;
|
|
34
|
+
let idx;
|
|
35
|
+
const cidx = visited.get(src);
|
|
36
|
+
if (cidx !== undefined) {
|
|
37
|
+
return cloned[cidx];
|
|
38
|
+
}
|
|
39
|
+
visited.set(src, vidx++);
|
|
40
|
+
if (Array.isArray(src)) {
|
|
41
|
+
res = [];
|
|
42
|
+
cloned.push(res);
|
|
43
|
+
idx = src.length;
|
|
44
|
+
while (idx--) {
|
|
45
|
+
res[idx] = cloneDeepInner(src[idx]);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
else {
|
|
49
|
+
res = {};
|
|
50
|
+
cloned.push(res);
|
|
51
|
+
const keys = Object.keys(src);
|
|
52
|
+
idx = keys.length;
|
|
53
|
+
while (idx--) {
|
|
54
|
+
const key = keys[idx];
|
|
55
|
+
res[key] = cloneDeepInner(src[key]);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
return res;
|
|
59
|
+
};
|
|
60
|
+
return cloneDeepInner(src);
|
|
61
|
+
};
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { isObject } from './what-is.js';
|
|
2
|
+
export const areEqual = (json1, json2, options) => {
|
|
3
|
+
options = options || {};
|
|
4
|
+
const caseInsensitiveComparison = options.caseInsensitiveComparison || false;
|
|
5
|
+
// http://json-schema.org/latest/json-schema-core.html#rfc.section.3.6
|
|
6
|
+
// Two JSON values are said to be equal if and only if:
|
|
7
|
+
// both are nulls; or
|
|
8
|
+
// both are booleans, and have the same value; or
|
|
9
|
+
// both are strings, and have the same value; or
|
|
10
|
+
// both are numbers, and have the same mathematical value; or
|
|
11
|
+
if (json1 === json2) {
|
|
12
|
+
return true;
|
|
13
|
+
}
|
|
14
|
+
if (caseInsensitiveComparison === true &&
|
|
15
|
+
typeof json1 === 'string' &&
|
|
16
|
+
typeof json2 === 'string' &&
|
|
17
|
+
json1.toUpperCase() === json2.toUpperCase()) {
|
|
18
|
+
return true;
|
|
19
|
+
}
|
|
20
|
+
let i, len;
|
|
21
|
+
// both are arrays, and:
|
|
22
|
+
if (Array.isArray(json1) && Array.isArray(json2)) {
|
|
23
|
+
// have the same number of items; and
|
|
24
|
+
if (json1.length !== json2.length) {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
// items at the same index are equal according to this definition; or
|
|
28
|
+
len = json1.length;
|
|
29
|
+
for (i = 0; i < len; i++) {
|
|
30
|
+
if (!areEqual(json1[i], json2[i], { caseInsensitiveComparison: caseInsensitiveComparison })) {
|
|
31
|
+
return false;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
// both are objects, and:
|
|
37
|
+
if (isObject(json1) && isObject(json2)) {
|
|
38
|
+
// have the same set of property names; and
|
|
39
|
+
const keys1 = sortedKeys(json1);
|
|
40
|
+
const keys2 = sortedKeys(json2);
|
|
41
|
+
if (!areEqual(keys1, keys2, { caseInsensitiveComparison: caseInsensitiveComparison })) {
|
|
42
|
+
return false;
|
|
43
|
+
}
|
|
44
|
+
// values for a same property name are equal according to this definition.
|
|
45
|
+
len = keys1.length;
|
|
46
|
+
for (i = 0; i < len; i++) {
|
|
47
|
+
if (!areEqual(json1[keys1[i]], json2[keys1[i]], { caseInsensitiveComparison: caseInsensitiveComparison })) {
|
|
48
|
+
return false;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
return false;
|
|
54
|
+
};
|
|
55
|
+
export const decodeJSONPointer = (str) => {
|
|
56
|
+
// http://tools.ietf.org/html/draft-ietf-appsawg-json-pointer-07#section-3
|
|
57
|
+
return decodeURIComponent(str).replace(/~[0-1]/g, (x) => (x === '~1' ? '/' : '~'));
|
|
58
|
+
};
|
|
59
|
+
export const sortedKeys = (obj) => Object.keys(obj).sort();
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/*
|
|
2
|
+
following function comes from punycode.js library
|
|
3
|
+
see: https://github.com/bestiejs/punycode.js
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Creates an array containing the numeric code points of each Unicode
|
|
7
|
+
* character in the string. While JavaScript uses UCS-2 internally,
|
|
8
|
+
* this function will convert a pair of surrogate halves (each of which
|
|
9
|
+
* UCS-2 exposes as separate characters) into a single code point,
|
|
10
|
+
* matching UTF-16.
|
|
11
|
+
* @see `punycode.ucs2.encode`
|
|
12
|
+
* @see <https://mathiasbynens.be/notes/javascript-encoding>
|
|
13
|
+
* @memberOf punycode.ucs2
|
|
14
|
+
* @name decode
|
|
15
|
+
* @param {String} string The Unicode input string (UCS-2).
|
|
16
|
+
* @returns {Array} The new array of code points.
|
|
17
|
+
*/
|
|
18
|
+
export function ucs2decode(string) {
|
|
19
|
+
const output = [];
|
|
20
|
+
let counter = 0;
|
|
21
|
+
const length = string.length;
|
|
22
|
+
let value;
|
|
23
|
+
let extra;
|
|
24
|
+
while (counter < length) {
|
|
25
|
+
value = string.charCodeAt(counter++);
|
|
26
|
+
if (value >= 0xd800 && value <= 0xdbff && counter < length) {
|
|
27
|
+
// high surrogate, and there is a next character
|
|
28
|
+
extra = string.charCodeAt(counter++);
|
|
29
|
+
if ((extra & 0xfc00) == 0xdc00) {
|
|
30
|
+
// low surrogate
|
|
31
|
+
output.push(((value & 0x3ff) << 10) + (extra & 0x3ff) + 0x10000);
|
|
32
|
+
}
|
|
33
|
+
else {
|
|
34
|
+
// unmatched surrogate; only append this code unit, in case the next
|
|
35
|
+
// code unit is the high surrogate of a surrogate pair
|
|
36
|
+
output.push(value);
|
|
37
|
+
counter--;
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
output.push(value);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
return output;
|
|
45
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export const getQueryPath = (uri) => {
|
|
2
|
+
const io = uri.indexOf('#');
|
|
3
|
+
const res = io === -1 ? undefined : uri.slice(io + 1);
|
|
4
|
+
// WARN: do not slice slash, #/ means take root and go down from it
|
|
5
|
+
// if (res && res[0] === "/") { res = res.slice(1); }
|
|
6
|
+
return res;
|
|
7
|
+
};
|
|
8
|
+
export const getRemotePath = (uri) => {
|
|
9
|
+
const io = uri.indexOf('#');
|
|
10
|
+
return io === -1 ? uri : uri.slice(0, io);
|
|
11
|
+
};
|
|
12
|
+
export const isAbsoluteUri = (uri) => /^https?:\/\//.test(uri);
|
|
13
|
+
export const isRelativeUri = (uri) =>
|
|
14
|
+
// relative URIs that end with a hash sign, issue #56
|
|
15
|
+
/.+#/.test(uri);
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export const whatIs = (what) => {
|
|
2
|
+
if (typeof what === 'object') {
|
|
3
|
+
if (what === null) {
|
|
4
|
+
return 'null';
|
|
5
|
+
}
|
|
6
|
+
if (Array.isArray(what)) {
|
|
7
|
+
return 'array';
|
|
8
|
+
}
|
|
9
|
+
return 'object'; // typeof what === 'object' && what === Object(what) && !Array.isArray(what);
|
|
10
|
+
}
|
|
11
|
+
if (typeof what === 'number') {
|
|
12
|
+
if (Number.isFinite(what)) {
|
|
13
|
+
if (what % 1 === 0) {
|
|
14
|
+
return 'integer';
|
|
15
|
+
}
|
|
16
|
+
else {
|
|
17
|
+
return 'number';
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
if (Number.isNaN(what)) {
|
|
21
|
+
return 'not-a-number';
|
|
22
|
+
}
|
|
23
|
+
return 'unknown-number';
|
|
24
|
+
}
|
|
25
|
+
return typeof what; // undefined, boolean, string, function
|
|
26
|
+
};
|
|
27
|
+
export function isObject(value) {
|
|
28
|
+
return whatIs(value) === 'object';
|
|
29
|
+
}
|