@sinclair/typebox 0.33.3 → 0.33.5
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/build/cjs/compiler/compiler.d.ts +2 -2
- package/build/cjs/compiler/compiler.js +1 -1
- package/build/cjs/system/policy.d.ts +5 -5
- package/build/cjs/system/policy.js +5 -5
- package/build/cjs/value/assert/assert.d.ts +15 -0
- package/build/cjs/value/assert/assert.js +55 -0
- package/build/cjs/value/assert/index.d.ts +1 -0
- package/build/cjs/value/assert/index.js +18 -0
- package/build/cjs/value/cast/cast.js +1 -1
- package/build/cjs/value/clean/clean.js +6 -6
- package/build/cjs/value/clone/clone.js +21 -11
- package/build/cjs/value/convert/convert.d.ts +2 -2
- package/build/cjs/value/convert/convert.js +17 -18
- package/build/cjs/value/create/create.js +5 -1
- package/build/cjs/value/default/default.js +14 -16
- package/build/cjs/value/equal/equal.js +3 -3
- package/build/cjs/value/hash/hash.js +1 -1
- package/build/cjs/value/index.d.ts +2 -0
- package/build/cjs/value/index.js +2 -0
- package/build/cjs/value/mutate/mutate.js +4 -4
- package/build/cjs/value/parse/index.d.ts +1 -0
- package/build/cjs/value/parse/index.js +18 -0
- package/build/cjs/value/parse/parse.d.ts +6 -0
- package/build/cjs/value/parse/parse.js +29 -0
- package/build/cjs/value/transform/decode.js +21 -8
- package/build/cjs/value/transform/encode.js +22 -9
- package/build/cjs/value/transform/has.js +5 -1
- package/build/cjs/value/value/value.d.ts +10 -2
- package/build/cjs/value/value/value.js +38 -26
- package/build/esm/compiler/compiler.d.mts +2 -2
- package/build/esm/compiler/compiler.mjs +1 -1
- package/build/esm/system/policy.d.mts +5 -5
- package/build/esm/system/policy.mjs +5 -5
- package/build/esm/value/assert/assert.d.mts +15 -0
- package/build/esm/value/assert/assert.mjs +49 -0
- package/build/esm/value/assert/index.d.mts +1 -0
- package/build/esm/value/assert/index.mjs +1 -0
- package/build/esm/value/cast/cast.mjs +2 -2
- package/build/esm/value/clean/clean.mjs +7 -7
- package/build/esm/value/clone/clone.mjs +22 -12
- package/build/esm/value/convert/convert.d.mts +2 -2
- package/build/esm/value/convert/convert.mjs +17 -18
- package/build/esm/value/create/create.mjs +5 -1
- package/build/esm/value/default/default.mjs +14 -16
- package/build/esm/value/equal/equal.mjs +4 -4
- package/build/esm/value/hash/hash.mjs +2 -2
- package/build/esm/value/index.d.mts +2 -0
- package/build/esm/value/index.mjs +2 -0
- package/build/esm/value/mutate/mutate.mjs +5 -5
- package/build/esm/value/parse/index.d.mts +1 -0
- package/build/esm/value/parse/index.mjs +1 -0
- package/build/esm/value/parse/parse.d.mts +6 -0
- package/build/esm/value/parse/parse.mjs +25 -0
- package/build/esm/value/transform/decode.mjs +23 -10
- package/build/esm/value/transform/encode.mjs +24 -11
- package/build/esm/value/transform/has.mjs +5 -1
- package/build/esm/value/value/value.d.mts +10 -2
- package/build/esm/value/value/value.mjs +11 -1
- package/package.json +1 -1
- package/readme.md +115 -130
|
@@ -17,9 +17,9 @@ export declare class TypeCheck<T extends TSchema> {
|
|
|
17
17
|
/** Returns true if the value matches the compiled type. */
|
|
18
18
|
Check(value: unknown): value is Static<T>;
|
|
19
19
|
/** Decodes a value or throws if error */
|
|
20
|
-
Decode(value: unknown):
|
|
20
|
+
Decode<R = StaticDecode<T>>(value: unknown): R;
|
|
21
21
|
/** Encodes a value or throws if error */
|
|
22
|
-
Encode(value: unknown):
|
|
22
|
+
Encode<R = StaticEncode<T>>(value: unknown): R;
|
|
23
23
|
}
|
|
24
24
|
export declare class TypeCompilerUnknownTypeError extends TypeBoxError {
|
|
25
25
|
readonly schema: TSchema;
|
|
@@ -48,7 +48,7 @@ class TypeCheck {
|
|
|
48
48
|
Decode(value) {
|
|
49
49
|
if (!this.checkFunc(value))
|
|
50
50
|
throw new index_1.TransformDecodeCheckError(this.schema, value, this.Errors(value).First());
|
|
51
|
-
return this.hasTransform ? (0, index_1.TransformDecode)(this.schema, this.references, value) : value;
|
|
51
|
+
return (this.hasTransform ? (0, index_1.TransformDecode)(this.schema, this.references, value) : value);
|
|
52
52
|
}
|
|
53
53
|
/** Encodes a value or throws if error */
|
|
54
54
|
Encode(value) {
|
|
@@ -16,14 +16,14 @@ export declare namespace TypeSystemPolicy {
|
|
|
16
16
|
let AllowNaN: boolean;
|
|
17
17
|
/** Sets whether `null` should validate for void types. The default is `false` */
|
|
18
18
|
let AllowNullVoid: boolean;
|
|
19
|
-
/**
|
|
19
|
+
/** Checks this value using the ExactOptionalPropertyTypes policy */
|
|
20
20
|
function IsExactOptionalProperty(value: Record<keyof any, unknown>, key: string): boolean;
|
|
21
|
-
/**
|
|
21
|
+
/** Checks this value using the AllowArrayObjects policy */
|
|
22
22
|
function IsObjectLike(value: unknown): value is Record<keyof any, unknown>;
|
|
23
|
-
/**
|
|
23
|
+
/** Checks this value as a record using the AllowArrayObjects policy */
|
|
24
24
|
function IsRecordLike(value: unknown): value is Record<keyof any, unknown>;
|
|
25
|
-
/**
|
|
25
|
+
/** Checks this value using the AllowNaN policy */
|
|
26
26
|
function IsNumberLike(value: unknown): value is number;
|
|
27
|
-
/**
|
|
27
|
+
/** Checks this value using the AllowVoidNull policy */
|
|
28
28
|
function IsVoidLike(value: unknown): value is void;
|
|
29
29
|
}
|
|
@@ -28,28 +28,28 @@ var TypeSystemPolicy;
|
|
|
28
28
|
TypeSystemPolicy.AllowNaN = false;
|
|
29
29
|
/** Sets whether `null` should validate for void types. The default is `false` */
|
|
30
30
|
TypeSystemPolicy.AllowNullVoid = false;
|
|
31
|
-
/**
|
|
31
|
+
/** Checks this value using the ExactOptionalPropertyTypes policy */
|
|
32
32
|
function IsExactOptionalProperty(value, key) {
|
|
33
33
|
return TypeSystemPolicy.ExactOptionalPropertyTypes ? key in value : value[key] !== undefined;
|
|
34
34
|
}
|
|
35
35
|
TypeSystemPolicy.IsExactOptionalProperty = IsExactOptionalProperty;
|
|
36
|
-
/**
|
|
36
|
+
/** Checks this value using the AllowArrayObjects policy */
|
|
37
37
|
function IsObjectLike(value) {
|
|
38
38
|
const isObject = (0, index_1.IsObject)(value);
|
|
39
39
|
return TypeSystemPolicy.AllowArrayObject ? isObject : isObject && !(0, index_1.IsArray)(value);
|
|
40
40
|
}
|
|
41
41
|
TypeSystemPolicy.IsObjectLike = IsObjectLike;
|
|
42
|
-
/**
|
|
42
|
+
/** Checks this value as a record using the AllowArrayObjects policy */
|
|
43
43
|
function IsRecordLike(value) {
|
|
44
44
|
return IsObjectLike(value) && !(value instanceof Date) && !(value instanceof Uint8Array);
|
|
45
45
|
}
|
|
46
46
|
TypeSystemPolicy.IsRecordLike = IsRecordLike;
|
|
47
|
-
/**
|
|
47
|
+
/** Checks this value using the AllowNaN policy */
|
|
48
48
|
function IsNumberLike(value) {
|
|
49
49
|
return TypeSystemPolicy.AllowNaN ? (0, index_1.IsNumber)(value) : Number.isFinite(value);
|
|
50
50
|
}
|
|
51
51
|
TypeSystemPolicy.IsNumberLike = IsNumberLike;
|
|
52
|
-
/**
|
|
52
|
+
/** Checks this value using the AllowVoidNull policy */
|
|
53
53
|
function IsVoidLike(value) {
|
|
54
54
|
const isUndefined = (0, index_1.IsUndefined)(value);
|
|
55
55
|
return TypeSystemPolicy.AllowNullVoid ? isUndefined || value === null : isUndefined;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { ValueErrorIterator, ValueError } from '../../errors/index';
|
|
2
|
+
import { TypeBoxError } from '../../type/error/error';
|
|
3
|
+
import { TSchema } from '../../type/schema/index';
|
|
4
|
+
import { Static } from '../../type/static/index';
|
|
5
|
+
export declare class AssertError extends TypeBoxError {
|
|
6
|
+
#private;
|
|
7
|
+
error: ValueError | undefined;
|
|
8
|
+
constructor(iterator: ValueErrorIterator);
|
|
9
|
+
/** Returns an iterator for each error in this value. */
|
|
10
|
+
Errors(): ValueErrorIterator;
|
|
11
|
+
}
|
|
12
|
+
/** Asserts a value matches the given type or throws an `AssertError` if invalid */
|
|
13
|
+
export declare function Assert<T extends TSchema>(schema: T, references: TSchema[], value: unknown): asserts value is Static<T>;
|
|
14
|
+
/** Asserts a value matches the given type or throws an `AssertError` if invalid */
|
|
15
|
+
export declare function Assert<T extends TSchema>(schema: T, value: unknown): asserts value is Static<T>;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var __classPrivateFieldSet = (this && this.__classPrivateFieldSet) || function (receiver, state, value, kind, f) {
|
|
4
|
+
if (kind === "m") throw new TypeError("Private method is not writable");
|
|
5
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a setter");
|
|
6
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot write private member to an object whose class did not declare it");
|
|
7
|
+
return (kind === "a" ? f.call(receiver, value) : f ? f.value = value : state.set(receiver, value)), value;
|
|
8
|
+
};
|
|
9
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
10
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
11
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
12
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
13
|
+
};
|
|
14
|
+
var _AssertError_instances, _AssertError_iterator, _AssertError_Iterator;
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.AssertError = void 0;
|
|
17
|
+
exports.Assert = Assert;
|
|
18
|
+
const index_1 = require("../../errors/index");
|
|
19
|
+
const error_1 = require("../../type/error/error");
|
|
20
|
+
const check_1 = require("../check/check");
|
|
21
|
+
// ------------------------------------------------------------------
|
|
22
|
+
// AssertError
|
|
23
|
+
// ------------------------------------------------------------------
|
|
24
|
+
class AssertError extends error_1.TypeBoxError {
|
|
25
|
+
constructor(iterator) {
|
|
26
|
+
const error = iterator.First();
|
|
27
|
+
super(error === undefined ? 'Invalid Value' : error.message);
|
|
28
|
+
_AssertError_instances.add(this);
|
|
29
|
+
_AssertError_iterator.set(this, void 0);
|
|
30
|
+
__classPrivateFieldSet(this, _AssertError_iterator, iterator, "f");
|
|
31
|
+
this.error = error;
|
|
32
|
+
}
|
|
33
|
+
/** Returns an iterator for each error in this value. */
|
|
34
|
+
Errors() {
|
|
35
|
+
return new index_1.ValueErrorIterator(__classPrivateFieldGet(this, _AssertError_instances, "m", _AssertError_Iterator).call(this));
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
exports.AssertError = AssertError;
|
|
39
|
+
_AssertError_iterator = new WeakMap(), _AssertError_instances = new WeakSet(), _AssertError_Iterator = function* _AssertError_Iterator() {
|
|
40
|
+
if (this.error)
|
|
41
|
+
yield this.error;
|
|
42
|
+
yield* __classPrivateFieldGet(this, _AssertError_iterator, "f");
|
|
43
|
+
};
|
|
44
|
+
// ------------------------------------------------------------------
|
|
45
|
+
// AssertValue
|
|
46
|
+
// ------------------------------------------------------------------
|
|
47
|
+
function AssertValue(schema, references, value) {
|
|
48
|
+
if ((0, check_1.Check)(schema, references, value))
|
|
49
|
+
return;
|
|
50
|
+
throw new AssertError((0, index_1.Errors)(schema, references, value));
|
|
51
|
+
}
|
|
52
|
+
/** Asserts a value matches the given type or throws an `AssertError` if invalid */
|
|
53
|
+
function Assert(...args) {
|
|
54
|
+
return args.length === 3 ? AssertValue(args[0], args[1], args[2]) : AssertValue(args[0], [], args[1]);
|
|
55
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './assert';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
4
|
+
if (k2 === undefined) k2 = k;
|
|
5
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
6
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
7
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
8
|
+
}
|
|
9
|
+
Object.defineProperty(o, k2, desc);
|
|
10
|
+
}) : (function(o, m, k, k2) {
|
|
11
|
+
if (k2 === undefined) k2 = k;
|
|
12
|
+
o[k2] = m[k];
|
|
13
|
+
}));
|
|
14
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
15
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
16
|
+
};
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
__exportStar(require("./assert"), exports);
|
|
@@ -106,7 +106,7 @@ function FromConstructor(schema, references, value) {
|
|
|
106
106
|
}
|
|
107
107
|
function FromIntersect(schema, references, value) {
|
|
108
108
|
const created = (0, index_4.Create)(schema, references);
|
|
109
|
-
const mapped = (0, index_1.
|
|
109
|
+
const mapped = (0, index_1.IsObject)(created) && (0, index_1.IsObject)(value) ? { ...created, ...value } : value;
|
|
110
110
|
return (0, index_5.Check)(schema, references, mapped) ? mapped : (0, index_4.Create)(schema, references);
|
|
111
111
|
}
|
|
112
112
|
function FromNever(schema, references, value) {
|
|
@@ -16,12 +16,12 @@ const index_6 = require("../guard/index");
|
|
|
16
16
|
// TypeGuard
|
|
17
17
|
// ------------------------------------------------------------------
|
|
18
18
|
// prettier-ignore
|
|
19
|
-
const
|
|
19
|
+
const kind_1 = require("../../type/guard/kind");
|
|
20
20
|
// ------------------------------------------------------------------
|
|
21
21
|
// IsCheckable
|
|
22
22
|
// ------------------------------------------------------------------
|
|
23
23
|
function IsCheckable(schema) {
|
|
24
|
-
return (0,
|
|
24
|
+
return (0, kind_1.IsKind)(schema) && schema[index_5.Kind] !== 'Unsafe';
|
|
25
25
|
}
|
|
26
26
|
// ------------------------------------------------------------------
|
|
27
27
|
// Types
|
|
@@ -35,7 +35,7 @@ function FromIntersect(schema, references, value) {
|
|
|
35
35
|
const unevaluatedProperties = schema.unevaluatedProperties;
|
|
36
36
|
const intersections = schema.allOf.map((schema) => Visit(schema, references, (0, index_3.Clone)(value)));
|
|
37
37
|
const composite = intersections.reduce((acc, value) => ((0, index_6.IsObject)(value) ? { ...acc, ...value } : value), {});
|
|
38
|
-
if (!(0, index_6.IsObject)(value) || !(0, index_6.IsObject)(composite) || !(0,
|
|
38
|
+
if (!(0, index_6.IsObject)(value) || !(0, index_6.IsObject)(composite) || !(0, kind_1.IsKind)(unevaluatedProperties))
|
|
39
39
|
return composite;
|
|
40
40
|
const knownkeys = (0, index_1.KeyOfPropertyKeys)(schema);
|
|
41
41
|
for (const key of Object.getOwnPropertyNames(value)) {
|
|
@@ -52,11 +52,11 @@ function FromObject(schema, references, value) {
|
|
|
52
52
|
return value; // Check IsArray for AllowArrayObject configuration
|
|
53
53
|
const additionalProperties = schema.additionalProperties;
|
|
54
54
|
for (const key of Object.getOwnPropertyNames(value)) {
|
|
55
|
-
if (
|
|
55
|
+
if ((0, index_6.HasPropertyKey)(schema.properties, key)) {
|
|
56
56
|
value[key] = Visit(schema.properties[key], references, value[key]);
|
|
57
57
|
continue;
|
|
58
58
|
}
|
|
59
|
-
if ((0,
|
|
59
|
+
if ((0, kind_1.IsKind)(additionalProperties) && (0, index_2.Check)(additionalProperties, references, value[key])) {
|
|
60
60
|
value[key] = Visit(additionalProperties, references, value[key]);
|
|
61
61
|
continue;
|
|
62
62
|
}
|
|
@@ -76,7 +76,7 @@ function FromRecord(schema, references, value) {
|
|
|
76
76
|
value[key] = Visit(propertySchema, references, value[key]);
|
|
77
77
|
continue;
|
|
78
78
|
}
|
|
79
|
-
if ((0,
|
|
79
|
+
if ((0, kind_1.IsKind)(additionalProperties) && (0, index_2.Check)(additionalProperties, references, value[key])) {
|
|
80
80
|
value[key] = Visit(additionalProperties, references, value[key]);
|
|
81
81
|
continue;
|
|
82
82
|
}
|
|
@@ -9,7 +9,7 @@ const index_1 = require("../guard/index");
|
|
|
9
9
|
// ------------------------------------------------------------------
|
|
10
10
|
// Clonable
|
|
11
11
|
// ------------------------------------------------------------------
|
|
12
|
-
function
|
|
12
|
+
function FromObject(value) {
|
|
13
13
|
const Acc = {};
|
|
14
14
|
for (const key of Object.getOwnPropertyNames(value)) {
|
|
15
15
|
Acc[key] = Clone(value[key]);
|
|
@@ -19,16 +19,22 @@ function ObjectType(value) {
|
|
|
19
19
|
}
|
|
20
20
|
return Acc;
|
|
21
21
|
}
|
|
22
|
-
function
|
|
22
|
+
function FromArray(value) {
|
|
23
23
|
return value.map((element) => Clone(element));
|
|
24
24
|
}
|
|
25
|
-
function
|
|
25
|
+
function FromTypedArray(value) {
|
|
26
26
|
return value.slice();
|
|
27
27
|
}
|
|
28
|
-
function
|
|
28
|
+
function FromMap(value) {
|
|
29
|
+
return new Map(Clone([...value.entries()]));
|
|
30
|
+
}
|
|
31
|
+
function FromSet(value) {
|
|
32
|
+
return new Set(Clone([...value.entries()]));
|
|
33
|
+
}
|
|
34
|
+
function FromDate(value) {
|
|
29
35
|
return new Date(value.toISOString());
|
|
30
36
|
}
|
|
31
|
-
function
|
|
37
|
+
function FromValue(value) {
|
|
32
38
|
return value;
|
|
33
39
|
}
|
|
34
40
|
// ------------------------------------------------------------------
|
|
@@ -37,14 +43,18 @@ function ValueType(value) {
|
|
|
37
43
|
/** Returns a clone of the given value */
|
|
38
44
|
function Clone(value) {
|
|
39
45
|
if ((0, index_1.IsArray)(value))
|
|
40
|
-
return
|
|
46
|
+
return FromArray(value);
|
|
41
47
|
if ((0, index_1.IsDate)(value))
|
|
42
|
-
return
|
|
43
|
-
if ((0, index_1.IsStandardObject)(value))
|
|
44
|
-
return ObjectType(value);
|
|
48
|
+
return FromDate(value);
|
|
45
49
|
if ((0, index_1.IsTypedArray)(value))
|
|
46
|
-
return
|
|
50
|
+
return FromTypedArray(value);
|
|
51
|
+
if ((0, index_1.IsMap)(value))
|
|
52
|
+
return FromMap(value);
|
|
53
|
+
if ((0, index_1.IsSet)(value))
|
|
54
|
+
return FromSet(value);
|
|
55
|
+
if ((0, index_1.IsObject)(value))
|
|
56
|
+
return FromObject(value);
|
|
47
57
|
if ((0, index_1.IsValueType)(value))
|
|
48
|
-
return
|
|
58
|
+
return FromValue(value);
|
|
49
59
|
throw new Error('ValueClone: Unable to clone value');
|
|
50
60
|
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import type { TSchema } from '../../type/schema/index';
|
|
2
|
-
/** Converts any type mismatched values to their target type if a reasonable conversion is possible. */
|
|
2
|
+
/** `[Mutable]` Converts any type mismatched values to their target type if a reasonable conversion is possible. */
|
|
3
3
|
export declare function Convert(schema: TSchema, references: TSchema[], value: unknown): unknown;
|
|
4
|
-
/** Converts any type mismatched values to their target type if a reasonable conversion is possible. */
|
|
4
|
+
/** `[Mutable]` Converts any type mismatched values to their target type if a reasonable conversion is possible. */
|
|
5
5
|
export declare function Convert(schema: TSchema, value: unknown): unknown;
|
|
@@ -60,7 +60,7 @@ function TryConvertLiteral(schema, value) {
|
|
|
60
60
|
return ((0, index_5.IsString)(schema.const) ? TryConvertLiteralString(value, schema.const) :
|
|
61
61
|
(0, index_5.IsNumber)(schema.const) ? TryConvertLiteralNumber(value, schema.const) :
|
|
62
62
|
(0, index_5.IsBoolean)(schema.const) ? TryConvertLiteralBoolean(value, schema.const) :
|
|
63
|
-
|
|
63
|
+
value);
|
|
64
64
|
}
|
|
65
65
|
function TryConvertBoolean(value) {
|
|
66
66
|
return IsValueTrue(value) ? true : IsValueFalse(value) ? false : value;
|
|
@@ -143,16 +143,14 @@ function FromNumber(schema, references, value) {
|
|
|
143
143
|
}
|
|
144
144
|
// prettier-ignore
|
|
145
145
|
function FromObject(schema, references, value) {
|
|
146
|
-
|
|
147
|
-
if (!isConvertable)
|
|
146
|
+
if (!(0, index_5.IsObject)(value))
|
|
148
147
|
return value;
|
|
149
|
-
const
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
: value[key];
|
|
148
|
+
for (const propertyKey of Object.getOwnPropertyNames(schema.properties)) {
|
|
149
|
+
if (!(0, index_5.HasPropertyKey)(value, propertyKey))
|
|
150
|
+
continue;
|
|
151
|
+
value[propertyKey] = Visit(schema.properties[propertyKey], references, value[propertyKey]);
|
|
154
152
|
}
|
|
155
|
-
return
|
|
153
|
+
return value;
|
|
156
154
|
}
|
|
157
155
|
function FromRecord(schema, references, value) {
|
|
158
156
|
const isConvertable = (0, index_5.IsObject)(value);
|
|
@@ -160,11 +158,10 @@ function FromRecord(schema, references, value) {
|
|
|
160
158
|
return value;
|
|
161
159
|
const propertyKey = Object.getOwnPropertyNames(schema.patternProperties)[0];
|
|
162
160
|
const property = schema.patternProperties[propertyKey];
|
|
163
|
-
const result = {};
|
|
164
161
|
for (const [propKey, propValue] of Object.entries(value)) {
|
|
165
|
-
|
|
162
|
+
value[propKey] = Visit(property, references, propValue);
|
|
166
163
|
}
|
|
167
|
-
return
|
|
164
|
+
return value;
|
|
168
165
|
}
|
|
169
166
|
function FromRef(schema, references, value) {
|
|
170
167
|
return Visit((0, index_3.Deref)(schema, references), references, value);
|
|
@@ -194,15 +191,19 @@ function FromUndefined(schema, references, value) {
|
|
|
194
191
|
}
|
|
195
192
|
function FromUnion(schema, references, value) {
|
|
196
193
|
for (const subschema of schema.anyOf) {
|
|
197
|
-
const converted = Visit(subschema, references, value);
|
|
194
|
+
const converted = Visit(subschema, references, (0, index_1.Clone)(value));
|
|
198
195
|
if (!(0, index_2.Check)(subschema, references, converted))
|
|
199
196
|
continue;
|
|
200
197
|
return converted;
|
|
201
198
|
}
|
|
202
199
|
return value;
|
|
203
200
|
}
|
|
201
|
+
function AddReference(references, schema) {
|
|
202
|
+
references.push(schema);
|
|
203
|
+
return references;
|
|
204
|
+
}
|
|
204
205
|
function Visit(schema, references, value) {
|
|
205
|
-
const references_ = (0, index_5.IsString)(schema.$id) ?
|
|
206
|
+
const references_ = (0, index_5.IsString)(schema.$id) ? AddReference(references, schema) : references;
|
|
206
207
|
const schema_ = schema;
|
|
207
208
|
switch (schema[index_4.Kind]) {
|
|
208
209
|
case 'Array':
|
|
@@ -245,10 +246,8 @@ function Visit(schema, references, value) {
|
|
|
245
246
|
return Default(value);
|
|
246
247
|
}
|
|
247
248
|
}
|
|
248
|
-
/** Converts any type mismatched values to their target type if a reasonable conversion is possible. */
|
|
249
|
+
/** `[Mutable]` Converts any type mismatched values to their target type if a reasonable conversion is possible. */
|
|
249
250
|
// prettier-ignore
|
|
250
251
|
function Convert(...args) {
|
|
251
|
-
return args.length === 3
|
|
252
|
-
? Visit(args[0], args[1], args[2])
|
|
253
|
-
: Visit(args[0], [], args[1]);
|
|
252
|
+
return args.length === 3 ? Visit(args[0], args[1], args[2]) : Visit(args[0], [], args[1]);
|
|
254
253
|
}
|
|
@@ -386,8 +386,12 @@ function FromKind(schema, references) {
|
|
|
386
386
|
throw new Error('User defined types must specify a default value');
|
|
387
387
|
}
|
|
388
388
|
}
|
|
389
|
+
function AddReference(references, schema) {
|
|
390
|
+
references.push(schema);
|
|
391
|
+
return references;
|
|
392
|
+
}
|
|
389
393
|
function Visit(schema, references) {
|
|
390
|
-
const references_ = (0, index_1.IsString)(schema.$id) ?
|
|
394
|
+
const references_ = (0, index_1.IsString)(schema.$id) ? AddReference(references, schema) : references;
|
|
391
395
|
const schema_ = schema;
|
|
392
396
|
switch (schema_[index_8.Kind]) {
|
|
393
397
|
case 'Any':
|
|
@@ -13,7 +13,7 @@ const index_5 = require("../guard/index");
|
|
|
13
13
|
// ------------------------------------------------------------------
|
|
14
14
|
// TypeGuard
|
|
15
15
|
// ------------------------------------------------------------------
|
|
16
|
-
const
|
|
16
|
+
const kind_1 = require("../../type/guard/kind");
|
|
17
17
|
// ------------------------------------------------------------------
|
|
18
18
|
// ValueOrDefault
|
|
19
19
|
// ------------------------------------------------------------------
|
|
@@ -21,16 +21,10 @@ function ValueOrDefault(schema, value) {
|
|
|
21
21
|
return value === undefined && 'default' in schema ? (0, index_2.Clone)(schema.default) : value;
|
|
22
22
|
}
|
|
23
23
|
// ------------------------------------------------------------------
|
|
24
|
-
//
|
|
24
|
+
// HasDefaultProperty
|
|
25
25
|
// ------------------------------------------------------------------
|
|
26
|
-
function
|
|
27
|
-
return (0,
|
|
28
|
-
}
|
|
29
|
-
// ------------------------------------------------------------------
|
|
30
|
-
// IsDefaultSchema
|
|
31
|
-
// ------------------------------------------------------------------
|
|
32
|
-
function IsDefaultSchema(value) {
|
|
33
|
-
return (0, type_1.IsSchema)(value) && 'default' in value;
|
|
26
|
+
function HasDefaultProperty(schema) {
|
|
27
|
+
return (0, kind_1.IsKind)(schema) && 'default' in schema;
|
|
34
28
|
}
|
|
35
29
|
// ------------------------------------------------------------------
|
|
36
30
|
// Types
|
|
@@ -59,12 +53,12 @@ function FromObject(schema, references, value) {
|
|
|
59
53
|
const knownPropertyKeys = Object.getOwnPropertyNames(schema.properties);
|
|
60
54
|
// properties
|
|
61
55
|
for (const key of knownPropertyKeys) {
|
|
62
|
-
if (!
|
|
56
|
+
if (!HasDefaultProperty(schema.properties[key]))
|
|
63
57
|
continue;
|
|
64
58
|
defaulted[key] = Visit(schema.properties[key], references, defaulted[key]);
|
|
65
59
|
}
|
|
66
60
|
// return if not additional properties
|
|
67
|
-
if (!
|
|
61
|
+
if (!HasDefaultProperty(additionalPropertiesSchema))
|
|
68
62
|
return defaulted;
|
|
69
63
|
// additional properties
|
|
70
64
|
for (const key of Object.getOwnPropertyNames(defaulted)) {
|
|
@@ -83,12 +77,12 @@ function FromRecord(schema, references, value) {
|
|
|
83
77
|
const knownPropertyKey = new RegExp(propertyKeyPattern);
|
|
84
78
|
// properties
|
|
85
79
|
for (const key of Object.getOwnPropertyNames(defaulted)) {
|
|
86
|
-
if (!(knownPropertyKey.test(key) &&
|
|
80
|
+
if (!(knownPropertyKey.test(key) && HasDefaultProperty(propertySchema)))
|
|
87
81
|
continue;
|
|
88
82
|
defaulted[key] = Visit(propertySchema, references, defaulted[key]);
|
|
89
83
|
}
|
|
90
84
|
// return if not additional properties
|
|
91
|
-
if (!
|
|
85
|
+
if (!HasDefaultProperty(additionalPropertiesSchema))
|
|
92
86
|
return defaulted;
|
|
93
87
|
// additional properties
|
|
94
88
|
for (const key of Object.getOwnPropertyNames(defaulted)) {
|
|
@@ -119,14 +113,18 @@ function FromUnion(schema, references, value) {
|
|
|
119
113
|
const defaulted = ValueOrDefault(schema, value);
|
|
120
114
|
for (const inner of schema.anyOf) {
|
|
121
115
|
const result = Visit(inner, references, defaulted);
|
|
122
|
-
if (
|
|
116
|
+
if ((0, index_1.Check)(inner, result)) {
|
|
123
117
|
return result;
|
|
124
118
|
}
|
|
125
119
|
}
|
|
126
120
|
return defaulted;
|
|
127
121
|
}
|
|
122
|
+
function AddReference(references, schema) {
|
|
123
|
+
references.push(schema);
|
|
124
|
+
return references;
|
|
125
|
+
}
|
|
128
126
|
function Visit(schema, references, value) {
|
|
129
|
-
const references_ = (0, index_5.IsString)(schema.$id) ?
|
|
127
|
+
const references_ = (0, index_5.IsString)(schema.$id) ? AddReference(references, schema) : references;
|
|
130
128
|
const schema_ = schema;
|
|
131
129
|
switch (schema_[index_4.Kind]) {
|
|
132
130
|
case 'Array':
|
|
@@ -7,7 +7,7 @@ const index_1 = require("../guard/index");
|
|
|
7
7
|
// Equality Checks
|
|
8
8
|
// ------------------------------------------------------------------
|
|
9
9
|
function ObjectType(left, right) {
|
|
10
|
-
if (!(0, index_1.
|
|
10
|
+
if (!(0, index_1.IsObject)(right))
|
|
11
11
|
return false;
|
|
12
12
|
const leftKeys = [...Object.keys(left), ...Object.getOwnPropertySymbols(left)];
|
|
13
13
|
const rightKeys = [...Object.keys(right), ...Object.getOwnPropertySymbols(right)];
|
|
@@ -36,14 +36,14 @@ function ValueType(left, right) {
|
|
|
36
36
|
// ------------------------------------------------------------------
|
|
37
37
|
/** Returns true if the left value deep-equals the right */
|
|
38
38
|
function Equal(left, right) {
|
|
39
|
-
if ((0, index_1.IsStandardObject)(left))
|
|
40
|
-
return ObjectType(left, right);
|
|
41
39
|
if ((0, index_1.IsDate)(left))
|
|
42
40
|
return DateType(left, right);
|
|
43
41
|
if ((0, index_1.IsTypedArray)(left))
|
|
44
42
|
return TypedArrayType(left, right);
|
|
45
43
|
if ((0, index_1.IsArray)(left))
|
|
46
44
|
return ArrayType(left, right);
|
|
45
|
+
if ((0, index_1.IsObject)(left))
|
|
46
|
+
return ObjectType(left, right);
|
|
47
47
|
if ((0, index_1.IsValueType)(left))
|
|
48
48
|
return ValueType(left, right);
|
|
49
49
|
throw new Error('ValueEquals: Unable to compare value');
|
|
@@ -125,7 +125,7 @@ function Visit(value) {
|
|
|
125
125
|
return NullType(value);
|
|
126
126
|
if ((0, index_1.IsNumber)(value))
|
|
127
127
|
return NumberType(value);
|
|
128
|
-
if ((0, index_1.
|
|
128
|
+
if ((0, index_1.IsObject)(value))
|
|
129
129
|
return ObjectType(value);
|
|
130
130
|
if ((0, index_1.IsString)(value))
|
|
131
131
|
return StringType(value);
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { ValueError, ValueErrorType, ValueErrorIterator } from '../errors/index';
|
|
2
2
|
export * from './guard/index';
|
|
3
|
+
export * from './assert/index';
|
|
3
4
|
export * from './cast/index';
|
|
4
5
|
export * from './check/index';
|
|
5
6
|
export * from './clean/index';
|
|
@@ -11,6 +12,7 @@ export * from './delta/index';
|
|
|
11
12
|
export * from './equal/index';
|
|
12
13
|
export * from './hash/index';
|
|
13
14
|
export * from './mutate/index';
|
|
15
|
+
export * from './parse/index';
|
|
14
16
|
export * from './pointer/index';
|
|
15
17
|
export * from './transform/index';
|
|
16
18
|
export { Value } from './value/index';
|
package/build/cjs/value/index.js
CHANGED
|
@@ -29,6 +29,7 @@ __exportStar(require("./guard/index"), exports);
|
|
|
29
29
|
// ------------------------------------------------------------------
|
|
30
30
|
// Operators
|
|
31
31
|
// ------------------------------------------------------------------
|
|
32
|
+
__exportStar(require("./assert/index"), exports);
|
|
32
33
|
__exportStar(require("./cast/index"), exports);
|
|
33
34
|
__exportStar(require("./check/index"), exports);
|
|
34
35
|
__exportStar(require("./clean/index"), exports);
|
|
@@ -40,6 +41,7 @@ __exportStar(require("./delta/index"), exports);
|
|
|
40
41
|
__exportStar(require("./equal/index"), exports);
|
|
41
42
|
__exportStar(require("./hash/index"), exports);
|
|
42
43
|
__exportStar(require("./mutate/index"), exports);
|
|
44
|
+
__exportStar(require("./parse/index"), exports);
|
|
43
45
|
__exportStar(require("./pointer/index"), exports);
|
|
44
46
|
__exportStar(require("./transform/index"), exports);
|
|
45
47
|
// ------------------------------------------------------------------
|
|
@@ -17,7 +17,7 @@ class ValueMutateError extends index_4.TypeBoxError {
|
|
|
17
17
|
}
|
|
18
18
|
exports.ValueMutateError = ValueMutateError;
|
|
19
19
|
function ObjectType(root, path, current, next) {
|
|
20
|
-
if (!(0, index_1.
|
|
20
|
+
if (!(0, index_1.IsObject)(current)) {
|
|
21
21
|
index_2.ValuePointer.Set(root, path, (0, index_3.Clone)(next));
|
|
22
22
|
}
|
|
23
23
|
else {
|
|
@@ -69,7 +69,7 @@ function Visit(root, path, current, next) {
|
|
|
69
69
|
return ArrayType(root, path, current, next);
|
|
70
70
|
if ((0, index_1.IsTypedArray)(next))
|
|
71
71
|
return TypedArrayType(root, path, current, next);
|
|
72
|
-
if ((0, index_1.
|
|
72
|
+
if ((0, index_1.IsObject)(next))
|
|
73
73
|
return ObjectType(root, path, current, next);
|
|
74
74
|
if ((0, index_1.IsValueType)(next))
|
|
75
75
|
return ValueType(root, path, current, next);
|
|
@@ -82,8 +82,8 @@ function IsNonMutableValue(value) {
|
|
|
82
82
|
}
|
|
83
83
|
function IsMismatchedValue(current, next) {
|
|
84
84
|
// prettier-ignore
|
|
85
|
-
return (((0, index_1.
|
|
86
|
-
((0, index_1.IsArray)(current) && (0, index_1.
|
|
85
|
+
return (((0, index_1.IsObject)(current) && (0, index_1.IsArray)(next)) ||
|
|
86
|
+
((0, index_1.IsArray)(current) && (0, index_1.IsObject)(next)));
|
|
87
87
|
}
|
|
88
88
|
// ------------------------------------------------------------------
|
|
89
89
|
// Mutate
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from './parse';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
4
|
+
if (k2 === undefined) k2 = k;
|
|
5
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
6
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
7
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
8
|
+
}
|
|
9
|
+
Object.defineProperty(o, k2, desc);
|
|
10
|
+
}) : (function(o, m, k, k2) {
|
|
11
|
+
if (k2 === undefined) k2 = k;
|
|
12
|
+
o[k2] = m[k];
|
|
13
|
+
}));
|
|
14
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
15
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
16
|
+
};
|
|
17
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
18
|
+
__exportStar(require("./parse"), exports);
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { TSchema } from '../../type/schema/index';
|
|
2
|
+
import { StaticDecode } from '../../type/static/index';
|
|
3
|
+
/** Parses a value or throws an `AssertError` if invalid. */
|
|
4
|
+
export declare function Parse<T extends TSchema, R = StaticDecode<T>>(schema: T, references: TSchema[], value: unknown): R;
|
|
5
|
+
/** Parses a value or throws an `AssertError` if invalid. */
|
|
6
|
+
export declare function Parse<T extends TSchema, R = StaticDecode<T>>(schema: T, value: unknown): R;
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.Parse = Parse;
|
|
5
|
+
const index_1 = require("../transform/index");
|
|
6
|
+
const assert_1 = require("../assert/assert");
|
|
7
|
+
const default_1 = require("../default/default");
|
|
8
|
+
const convert_1 = require("../convert/convert");
|
|
9
|
+
const clean_1 = require("../clean/clean");
|
|
10
|
+
const index_2 = require("../clone/index");
|
|
11
|
+
// prettier-ignore
|
|
12
|
+
const ParseReducer = [
|
|
13
|
+
(_schema, _references, value) => (0, index_2.Clone)(value),
|
|
14
|
+
(schema, references, value) => (0, default_1.Default)(schema, references, value),
|
|
15
|
+
(schema, references, value) => (0, clean_1.Clean)(schema, references, value),
|
|
16
|
+
(schema, references, value) => (0, convert_1.Convert)(schema, references, value),
|
|
17
|
+
(schema, references, value) => { (0, assert_1.Assert)(schema, references, value); return value; },
|
|
18
|
+
(schema, references, value) => ((0, index_1.HasTransform)(schema, references) ? (0, index_1.TransformDecode)(schema, references, value) : value),
|
|
19
|
+
];
|
|
20
|
+
// ------------------------------------------------------------------
|
|
21
|
+
// ParseValue
|
|
22
|
+
// ------------------------------------------------------------------
|
|
23
|
+
function ParseValue(schema, references, value) {
|
|
24
|
+
return ParseReducer.reduce((value, reducer) => reducer(schema, references, value), value);
|
|
25
|
+
}
|
|
26
|
+
/** Parses a value or throws an `AssertError` if invalid. */
|
|
27
|
+
function Parse(...args) {
|
|
28
|
+
return args.length === 3 ? ParseValue(args[0], args[1], args[2]) : ParseValue(args[0], [], args[1]);
|
|
29
|
+
}
|