typebox 1.1.28 → 1.1.30
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/schema/engine/_refine.mjs +4 -4
- package/build/schema/types/_refine.d.mts +2 -2
- package/build/schema/types/_refine.mjs +4 -4
- package/build/type/engine/indexed/from-object.d.mts +10 -3
- package/build/type/engine/indexed/from-object.mjs +29 -7
- package/build/type/engine/mapped/mapped-variants.d.mts +3 -2
- package/build/type/engine/mapped/mapped-variants.mjs +7 -2
- package/build/type/engine/this/expand-this.d.mts +19 -0
- package/build/type/engine/this/expand-this.mjs +32 -0
- package/build/type/extends/extends.d.mts +4 -2
- package/build/type/extends/extends.mjs +9 -7
- package/build/type/types/_refine.d.mts +13 -6
- package/build/type/types/_refine.mjs +21 -4
- package/build/type/types/properties.d.mts +2 -1
- package/build/type/types/record.d.mts +0 -2
- package/build/type/types/record.mjs +1 -10
- package/build/type/types/unsafe.d.mts +1 -2
- package/build/type/types/unsafe.mjs +5 -3
- package/build/typebox.d.mts +1 -1
- package/build/value/mutate/mutate.d.mts +2 -0
- package/build/value/mutate/mutate.mjs +2 -0
- package/package.json +30 -30
|
@@ -6,24 +6,24 @@ import { EmitGuard as E, Guard as G } from '../../guard/index.mjs';
|
|
|
6
6
|
// ------------------------------------------------------------------
|
|
7
7
|
export function BuildRefine(_stack, _context, schema, value) {
|
|
8
8
|
const refinements = V.CreateVariable(schema['~refine'].map((refinement) => refinement));
|
|
9
|
-
return E.Every(refinements, E.Constant(0), ['refinement', '_'], E.Call(E.Member('refinement', '
|
|
9
|
+
return E.Every(refinements, E.Constant(0), ['refinement', '_'], E.Call(E.Member('refinement', 'check'), [value]));
|
|
10
10
|
}
|
|
11
11
|
// ------------------------------------------------------------------
|
|
12
12
|
// Check
|
|
13
13
|
// ------------------------------------------------------------------
|
|
14
14
|
export function CheckRefine(_stack, _context, schema, value) {
|
|
15
|
-
return G.Every(schema['~refine'], 0, (refinement, _) => refinement.
|
|
15
|
+
return G.Every(schema['~refine'], 0, (refinement, _) => refinement.check(value));
|
|
16
16
|
}
|
|
17
17
|
// ------------------------------------------------------------------
|
|
18
18
|
// Error
|
|
19
19
|
// ------------------------------------------------------------------
|
|
20
20
|
export function ErrorRefine(_stack, context, schemaPath, instancePath, schema, value) {
|
|
21
21
|
return G.EveryAll(schema['~refine'], 0, (refinement, index) => {
|
|
22
|
-
return refinement.
|
|
22
|
+
return refinement.check(value) || context.AddError({
|
|
23
23
|
keyword: '~refine',
|
|
24
24
|
schemaPath,
|
|
25
25
|
instancePath,
|
|
26
|
-
params: { index, message: refinement.
|
|
26
|
+
params: { index, message: refinement.error(value) },
|
|
27
27
|
});
|
|
28
28
|
});
|
|
29
29
|
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { XSchemaObject } from './schema.mjs';
|
|
2
2
|
export interface XRefinement {
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
check: (value: unknown) => boolean;
|
|
4
|
+
error: (value: unknown) => string;
|
|
5
5
|
}
|
|
6
6
|
export interface XRefine<Refinements extends XRefinement[] = XRefinement[]> {
|
|
7
7
|
'~refine': Refinements;
|
|
@@ -11,8 +11,8 @@ export function IsRefine(value) {
|
|
|
11
11
|
return Guard.HasPropertyKey(value, '~refine')
|
|
12
12
|
&& Guard.IsArray(value["~refine"])
|
|
13
13
|
&& Guard.Every(value['~refine'], 0, value => Guard.IsObject(value)
|
|
14
|
-
&& Guard.HasPropertyKey(value, '
|
|
15
|
-
&& Guard.HasPropertyKey(value, '
|
|
16
|
-
&& Guard.IsFunction(value.
|
|
17
|
-
&& Guard.
|
|
14
|
+
&& Guard.HasPropertyKey(value, 'check')
|
|
15
|
+
&& Guard.HasPropertyKey(value, 'error')
|
|
16
|
+
&& Guard.IsFunction(value.check)
|
|
17
|
+
&& Guard.IsFunction(value.error));
|
|
18
18
|
}
|
|
@@ -1,9 +1,16 @@
|
|
|
1
1
|
import { type TProperties } from '../../types/properties.mjs';
|
|
2
2
|
import { type TSchema } from '../../types/schema.mjs';
|
|
3
|
+
import { type TNumber } from '../../types/number.mjs';
|
|
4
|
+
import { type TNever } from '../../types/never.mjs';
|
|
5
|
+
import { type TPropertyKeys } from '../../types/properties.mjs';
|
|
3
6
|
import { type TEvaluateUnion } from '../evaluate/evaluate.mjs';
|
|
4
7
|
import { type TToIndexableKeys } from '../indexable/to-indexable-keys.mjs';
|
|
5
|
-
|
|
6
|
-
type
|
|
7
|
-
|
|
8
|
+
import { type TExpandThis } from '../this/expand-this.mjs';
|
|
9
|
+
type TIndexProperty<Properties extends TProperties, Key extends string, CanonicalKey extends string = keyof Properties extends string | number ? `${keyof Properties}` : never, SelectedType extends TSchema = Key extends CanonicalKey ? Properties[Key] : TNever, Result extends TSchema = TExpandThis<Properties, SelectedType>> = Result;
|
|
10
|
+
type TIndexProperties<Properties extends TProperties, Keys extends string[], Result extends TSchema[] = []> = (Keys extends [infer Left extends string, ...infer Right extends string[]] ? TIndexProperties<Properties, Right, [...Result, TIndexProperty<Properties, Left>]> : Result);
|
|
11
|
+
type TFromIndexer<Properties extends TProperties, Indexer extends TSchema, Keys extends string[] = TToIndexableKeys<Indexer>, Variants extends TSchema[] = TIndexProperties<Properties, Keys>, Result extends TSchema = TEvaluateUnion<Variants>> = Result;
|
|
12
|
+
type TNumericKeys<Keys extends string[], Result extends string[] = []> = (Keys extends [infer Left extends string, ...infer Right extends string[]] ? Left extends `${infer _ extends number}` ? TNumericKeys<Right, [...Result, Left]> : TNumericKeys<Right, Result> : Result);
|
|
13
|
+
type TFromIndexerNumber<Properties extends TProperties, Keys extends string[] = TPropertyKeys<Properties>, NumericKeys extends string[] = TNumericKeys<Keys>, Variants extends TSchema[] = TIndexProperties<Properties, NumericKeys>, Result extends TSchema = TEvaluateUnion<Variants>> = Result;
|
|
14
|
+
export type TFromObject<Properties extends TProperties, Indexer extends TSchema, Result extends TSchema = Indexer extends TNumber ? TFromIndexerNumber<Properties> : TFromIndexer<Properties, Indexer>> = Result;
|
|
8
15
|
export declare function FromObject<Properties extends TProperties, Indexer extends TSchema>(properties: Properties, indexer: Indexer): TFromObject<Properties, Indexer>;
|
|
9
16
|
export {};
|
|
@@ -1,18 +1,40 @@
|
|
|
1
1
|
// deno-fmt-ignore-file
|
|
2
|
+
import { IsNumber } from '../../types/number.mjs';
|
|
3
|
+
import { Never } from '../../types/never.mjs';
|
|
4
|
+
import { PropertyKeys } from '../../types/properties.mjs';
|
|
2
5
|
import { EvaluateUnion } from '../evaluate/evaluate.mjs';
|
|
3
6
|
import { ToIndexableKeys } from '../indexable/to-indexable-keys.mjs';
|
|
4
|
-
|
|
5
|
-
|
|
7
|
+
import { IntegerKey } from '../../types/record.mjs';
|
|
8
|
+
import { ExpandThis } from '../this/expand-this.mjs';
|
|
9
|
+
function IndexProperty(properties, key) {
|
|
10
|
+
const selectedType = key in properties ? properties[key] : Never();
|
|
11
|
+
const result = ExpandThis(properties, selectedType);
|
|
6
12
|
return result;
|
|
7
13
|
}
|
|
8
|
-
function
|
|
9
|
-
return
|
|
10
|
-
return [...result,
|
|
14
|
+
function IndexProperties(properties, keys) {
|
|
15
|
+
return keys.reduce((result, left) => {
|
|
16
|
+
return [...result, IndexProperty(properties, left)];
|
|
11
17
|
}, []);
|
|
12
18
|
}
|
|
13
|
-
|
|
19
|
+
function FromIndexer(properties, indexer) {
|
|
14
20
|
const keys = ToIndexableKeys(indexer);
|
|
15
|
-
const variants =
|
|
21
|
+
const variants = IndexProperties(properties, keys);
|
|
16
22
|
const result = EvaluateUnion(variants);
|
|
17
23
|
return result;
|
|
18
24
|
}
|
|
25
|
+
const NumericKeyPattern = new RegExp(IntegerKey);
|
|
26
|
+
function NumericKeys(keys) {
|
|
27
|
+
const result = keys.filter(key => NumericKeyPattern.test(key));
|
|
28
|
+
return result;
|
|
29
|
+
}
|
|
30
|
+
function FromIndexerNumber(properties) {
|
|
31
|
+
const keys = PropertyKeys(properties);
|
|
32
|
+
const numericKeys = NumericKeys(keys);
|
|
33
|
+
const variants = IndexProperties(properties, numericKeys);
|
|
34
|
+
const result = EvaluateUnion(variants);
|
|
35
|
+
return result;
|
|
36
|
+
}
|
|
37
|
+
export function FromObject(properties, indexer) {
|
|
38
|
+
const result = IsNumber(indexer) ? FromIndexerNumber(properties) : FromIndexer(properties, indexer);
|
|
39
|
+
return result;
|
|
40
|
+
}
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { type TSchema } from '../../types/index.mjs';
|
|
2
|
-
import { type TLiteral } from '../../types/literal.mjs';
|
|
2
|
+
import { type TLiteral, type TLiteralValue } from '../../types/literal.mjs';
|
|
3
3
|
import { type TEnum, type TEnumValue } from '../../types/enum.mjs';
|
|
4
4
|
import { type TTemplateLiteral } from '../../types/template-literal.mjs';
|
|
5
5
|
import { type TUnion } from '../../types/union.mjs';
|
|
@@ -7,7 +7,8 @@ import { type TEnumValuesToVariants } from '../enum/index.mjs';
|
|
|
7
7
|
import { type TTemplateLiteralDecode } from '../template-literal/decode.mjs';
|
|
8
8
|
type TFromTemplateLiteral<Pattern extends string, Decoded extends TSchema = TTemplateLiteralDecode<Pattern>, Result extends TSchema[] = TFromType<Decoded>> = Result;
|
|
9
9
|
type TFromUnion<Types extends TSchema[], Result extends TSchema[] = []> = (Types extends [infer Left extends TSchema, ...infer Right extends TSchema[]] ? TFromUnion<Right, [...Result, ...TFromType<Left>]> : Result);
|
|
10
|
-
type
|
|
10
|
+
type TFromLiteral<Value extends TLiteralValue, Result extends TSchema[] = Value extends number ? [TLiteral<`${Value}`>] : [TLiteral<Value>]> = Result;
|
|
11
|
+
type TFromType<Type extends TSchema, Result extends TSchema[] = (Type extends TEnum<infer Values extends TEnumValue[]> ? TFromUnion<TEnumValuesToVariants<Values>> : Type extends TLiteral<infer Value extends number> ? TFromLiteral<Value> : Type extends TTemplateLiteral<infer Pattern extends string> ? TFromTemplateLiteral<Pattern> : Type extends TUnion<infer Types extends TSchema[]> ? TFromUnion<Types> : [
|
|
11
12
|
Type
|
|
12
13
|
])> = Result;
|
|
13
14
|
export type TMappedVariants<Type extends TSchema, Result extends TSchema[] = TFromType<Type>> = Result;
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
// deno-fmt-ignore-file
|
|
2
|
-
import {
|
|
2
|
+
import { Guard } from '../../../guard/index.mjs';
|
|
3
|
+
import { Literal, IsLiteral } from '../../types/literal.mjs';
|
|
3
4
|
import { IsEnum } from '../../types/enum.mjs';
|
|
4
5
|
import { IsTemplateLiteral } from '../../types/template-literal.mjs';
|
|
5
6
|
import { IsUnion } from '../../types/union.mjs';
|
|
@@ -15,9 +16,13 @@ function FromUnion(types) {
|
|
|
15
16
|
return [...result, ...FromType(left)];
|
|
16
17
|
}, []);
|
|
17
18
|
}
|
|
19
|
+
function FromLiteral(value) {
|
|
20
|
+
const result = Guard.IsNumber(value) ? [Literal(`${value}`)] : [Literal(value)];
|
|
21
|
+
return result;
|
|
22
|
+
}
|
|
18
23
|
function FromType(type) {
|
|
19
24
|
const result = (IsEnum(type) ? FromUnion(EnumValuesToVariants(type.enum)) :
|
|
20
|
-
|
|
25
|
+
IsLiteral(type) ? FromLiteral(type.const) :
|
|
21
26
|
IsTemplateLiteral(type) ? FromTemplateLiteral(type.pattern) :
|
|
22
27
|
IsUnion(type) ? FromUnion(type.anyOf) :
|
|
23
28
|
[type]);
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { type TArray } from '../../types/array.mjs';
|
|
2
|
+
import { type TAsyncIterator } from '../../types/async-iterator.mjs';
|
|
3
|
+
import { type TConstructor } from '../../types/constructor.mjs';
|
|
4
|
+
import { type TFunction } from '../../types/function.mjs';
|
|
5
|
+
import { type TIterator } from '../../types/iterator.mjs';
|
|
6
|
+
import { type TIntersect } from '../../types/intersect.mjs';
|
|
7
|
+
import { type TObject } from '../../types/object.mjs';
|
|
8
|
+
import { type TProperties } from '../../types/properties.mjs';
|
|
9
|
+
import { type TSchema } from '../../types/schema.mjs';
|
|
10
|
+
import { type TPromise } from '../../types/promise.mjs';
|
|
11
|
+
import { type TTuple } from '../../types/tuple.mjs';
|
|
12
|
+
import { type TThis } from '../../types/this.mjs';
|
|
13
|
+
import { type TUnion } from '../../types/union.mjs';
|
|
14
|
+
type TFromTypes<Properties extends TProperties, Types extends TSchema[], Result extends TSchema[] = []> = (Types extends [infer Left extends TSchema, ...infer Right extends TSchema[]] ? TFromTypes<Properties, Right, [...Result, TFromType<Properties, Left>]> : Result);
|
|
15
|
+
export type TFromType<Properties extends TProperties, Type extends TSchema> = (Type extends TArray<infer Type extends TSchema> ? TArray<TFromType<Properties, Type>> : Type extends TAsyncIterator<infer Type extends TSchema> ? TAsyncIterator<TFromType<Properties, Type>> : Type extends TConstructor<infer Parameters extends TSchema[], infer InstanceType extends TSchema> ? TConstructor<TFromTypes<Properties, Parameters>, TFromType<Properties, InstanceType>> : Type extends TFunction<infer Parameters extends TSchema[], infer ReturnType extends TSchema> ? TFunction<TFromTypes<Properties, Parameters>, TFromType<Properties, ReturnType>> : Type extends TIterator<infer Type extends TSchema> ? TIterator<TFromType<Properties, Type>> : Type extends TPromise<infer Type extends TSchema> ? TPromise<TFromType<Properties, Type>> : Type extends TTuple<infer Types extends TSchema[]> ? TTuple<TFromTypes<Properties, Types>> : Type extends TUnion<infer Types extends TSchema[]> ? TUnion<TFromTypes<Properties, Types>> : Type extends TIntersect<infer Types extends TSchema[]> ? TIntersect<TFromTypes<Properties, Types>> : Type extends TThis ? TObject<Properties> : Type);
|
|
16
|
+
export declare function FromType<Properties extends TProperties, Type extends TSchema>(properties: Properties, type: Type): TFromType<Properties, Type>;
|
|
17
|
+
export type TExpandThis<Properties extends TProperties, Type extends TSchema, Result extends TSchema = TFromType<Properties, Type>> = Result;
|
|
18
|
+
export declare function ExpandThis<Properties extends TProperties, Type extends TSchema>(properties: TProperties, type: Type): TExpandThis<Properties, Type>;
|
|
19
|
+
export {};
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
// deno-fmt-ignore-file
|
|
2
|
+
import { IsArray, Array } from '../../types/array.mjs';
|
|
3
|
+
import { IsAsyncIterator, AsyncIterator } from '../../types/async-iterator.mjs';
|
|
4
|
+
import { IsConstructor, Constructor } from '../../types/constructor.mjs';
|
|
5
|
+
import { IsFunction, Function } from '../../types/function.mjs';
|
|
6
|
+
import { IsIterator, Iterator } from '../../types/iterator.mjs';
|
|
7
|
+
import { IsIntersect, Intersect } from '../../types/intersect.mjs';
|
|
8
|
+
import { Object } from '../../types/object.mjs';
|
|
9
|
+
import { IsPromise, Promise } from '../../types/promise.mjs';
|
|
10
|
+
import { IsTuple, Tuple } from '../../types/tuple.mjs';
|
|
11
|
+
import { IsThis } from '../../types/this.mjs';
|
|
12
|
+
import { IsUnion, Union } from '../../types/union.mjs';
|
|
13
|
+
function FromTypes(properties, types) {
|
|
14
|
+
return types.map(type => FromType(properties, type));
|
|
15
|
+
}
|
|
16
|
+
export function FromType(properties, type) {
|
|
17
|
+
return (IsArray(type) ? Array(FromType(properties, type.items)) :
|
|
18
|
+
IsAsyncIterator(type) ? AsyncIterator(FromType(properties, type.iteratorItems)) :
|
|
19
|
+
IsConstructor(type) ? Constructor(FromTypes(properties, type.parameters), FromType(properties, type.instanceType)) :
|
|
20
|
+
IsFunction(type) ? Function(FromTypes(properties, type.parameters), FromType(properties, type.returnType)) :
|
|
21
|
+
IsIterator(type) ? Iterator(FromType(properties, type.iteratorItems)) :
|
|
22
|
+
IsPromise(type) ? Promise(FromType(properties, type.item)) :
|
|
23
|
+
IsTuple(type) ? Tuple(FromTypes(properties, type.items)) :
|
|
24
|
+
IsUnion(type) ? Union(FromTypes(properties, type.anyOf)) :
|
|
25
|
+
IsIntersect(type) ? Intersect(FromTypes(properties, type.allOf)) :
|
|
26
|
+
IsThis(type) ? Object(properties) :
|
|
27
|
+
type);
|
|
28
|
+
}
|
|
29
|
+
export function ExpandThis(properties, type) {
|
|
30
|
+
const result = FromType(properties, type);
|
|
31
|
+
return result;
|
|
32
|
+
}
|
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { type TSchema } from '../types/schema.mjs';
|
|
2
2
|
import { type TProperties } from '../types/properties.mjs';
|
|
3
3
|
import { type TCyclic } from '../types/cyclic.mjs';
|
|
4
|
+
import { type TUnknown } from '../types/unknown.mjs';
|
|
5
|
+
import { type TUnsafe } from '../types/unsafe.mjs';
|
|
4
6
|
import { type TExtendsLeft } from './extends-left.mjs';
|
|
5
7
|
import { type TCyclicExtends } from '../engine/cyclic/index.mjs';
|
|
6
|
-
type
|
|
8
|
+
type TCanonical<Type extends TSchema> = (Type extends TCyclic ? TCyclicExtends<Type> : Type extends TUnsafe ? TUnknown : Type);
|
|
7
9
|
/** Performs a structural extends check on left and right types and yields inferred types on right if specified. */
|
|
8
|
-
export type TExtends<Inferred extends TProperties, Left extends TSchema, Right extends TSchema,
|
|
10
|
+
export type TExtends<Inferred extends TProperties, Left extends TSchema, Right extends TSchema, CanonicalLeft extends TSchema = TCanonical<Left>, CanonicalRight extends TSchema = TCanonical<Right>> = TExtendsLeft<Inferred, CanonicalLeft, CanonicalRight>;
|
|
9
11
|
/** Performs a structural extends check on left and right types and yields inferred types on right if specified. */
|
|
10
12
|
export declare function Extends<Inferred extends TProperties, Left extends TSchema, Right extends TSchema>(inferred: Inferred, left: Left, right: Right): TExtends<Inferred, Left, Right>;
|
|
11
13
|
export {};
|
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
// deno-fmt-ignore-file
|
|
2
2
|
import { IsCyclic } from '../types/cyclic.mjs';
|
|
3
|
+
import { Unknown } from '../types/unknown.mjs';
|
|
4
|
+
import { IsUnsafe } from '../types/unsafe.mjs';
|
|
3
5
|
import { ExtendsLeft } from './extends-left.mjs';
|
|
4
6
|
import { CyclicExtends } from '../engine/cyclic/index.mjs';
|
|
5
|
-
function
|
|
6
|
-
return (IsCyclic(type)
|
|
7
|
-
?
|
|
8
|
-
|
|
7
|
+
function Canonical(type) {
|
|
8
|
+
return (IsCyclic(type) ? CyclicExtends(type) :
|
|
9
|
+
IsUnsafe(type) ? Unknown() :
|
|
10
|
+
type);
|
|
9
11
|
}
|
|
10
12
|
/** Performs a structural extends check on left and right types and yields inferred types on right if specified. */
|
|
11
13
|
export function Extends(inferred, left, right) {
|
|
12
|
-
const
|
|
13
|
-
const
|
|
14
|
-
return ExtendsLeft(inferred,
|
|
14
|
+
const canonicalLeft = Canonical(left);
|
|
15
|
+
const canonicalRight = Canonical(right);
|
|
16
|
+
return ExtendsLeft(inferred, canonicalLeft, canonicalRight);
|
|
15
17
|
}
|
|
@@ -8,12 +8,19 @@ export declare function RefineAdd<Type extends TSchema>(type: Type, refinement:
|
|
|
8
8
|
export type TRefine<Type extends TSchema = TSchema> = (Type & {
|
|
9
9
|
'~refine': TRefinement<Type>[];
|
|
10
10
|
});
|
|
11
|
-
export type
|
|
11
|
+
export type TRefineCheckCallback<Type extends TSchema = TSchema> = (value: Static<Type>) => boolean;
|
|
12
|
+
export type TRefineErrorCallback<Type extends TSchema = TSchema> = (value: Static<Type>) => string;
|
|
12
13
|
export interface TRefinement<Type extends TSchema = TSchema> {
|
|
13
|
-
|
|
14
|
-
|
|
14
|
+
check: TRefineCheckCallback<Type>;
|
|
15
|
+
error: TRefineErrorCallback<Type>;
|
|
15
16
|
}
|
|
16
|
-
/**
|
|
17
|
-
export declare function Refine<Type extends TSchema>(type: Type,
|
|
17
|
+
/** Refines a type with an explicit check */
|
|
18
|
+
export declare function Refine<Type extends TSchema>(type: Type, check: TRefineCheckCallback<Type>, error: TRefineErrorCallback<Type>): TRefineAdd<Type>;
|
|
19
|
+
/** Refines a type with an explicit check */
|
|
20
|
+
export declare function Refine<Type extends TSchema>(type: Type, check: TRefineCheckCallback<Type>): TRefineAdd<Type>;
|
|
21
|
+
/** @deprecated Use the error callback signature to generate error message. This overload will be removed in the next version */
|
|
22
|
+
export declare function Refine<Type extends TSchema>(type: Type, check: TRefineCheckCallback<Type>, message: string): TRefineAdd<Type>;
|
|
23
|
+
/** Returns true if the given value is a TRefinement. */
|
|
24
|
+
export declare function IsRefinement(value: unknown): value is TRefinement;
|
|
18
25
|
/** Returns true if the given value is a TRefine. */
|
|
19
|
-
export declare function IsRefine(value: unknown): value is TRefine
|
|
26
|
+
export declare function IsRefine(value: unknown): value is TRefine;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
// deno-fmt-ignore-file
|
|
2
|
+
import { Arguments } from '../../system/arguments/index.mjs';
|
|
2
3
|
import { Memory } from '../../system/memory/index.mjs';
|
|
3
4
|
import { Guard } from '../../guard/index.mjs';
|
|
4
5
|
import { IsSchema } from './schema.mjs';
|
|
@@ -7,14 +8,30 @@ export function RefineAdd(type, refinement) {
|
|
|
7
8
|
const refinements = IsRefine(type) ? [...type['~refine'], refinement] : [refinement];
|
|
8
9
|
return Memory.Update(type, { '~refine': refinements }, {});
|
|
9
10
|
}
|
|
10
|
-
/**
|
|
11
|
-
export function Refine(
|
|
12
|
-
|
|
11
|
+
/** Refines a type with an explicit check */
|
|
12
|
+
export function Refine(...args) {
|
|
13
|
+
const [type, check, error_or_message] = Arguments.Match(args, {
|
|
14
|
+
3: (type, check, error) => [type, check, error],
|
|
15
|
+
2: (type, check) => [type, check, () => 'Refine Error'],
|
|
16
|
+
});
|
|
17
|
+
const error = Guard.IsString(error_or_message) ? () => error_or_message : error_or_message;
|
|
18
|
+
return RefineAdd(type, { check, error });
|
|
13
19
|
}
|
|
14
20
|
// ------------------------------------------------------------------
|
|
15
21
|
// Guard
|
|
16
22
|
// ------------------------------------------------------------------
|
|
23
|
+
/** Returns true if the given value is a TRefinement. */
|
|
24
|
+
export function IsRefinement(value) {
|
|
25
|
+
return Guard.IsObjectNotArray(value)
|
|
26
|
+
&& Guard.HasPropertyKey(value, 'check')
|
|
27
|
+
&& Guard.HasPropertyKey(value, 'error')
|
|
28
|
+
&& Guard.IsFunction(value.check)
|
|
29
|
+
&& Guard.IsFunction(value.error);
|
|
30
|
+
}
|
|
17
31
|
/** Returns true if the given value is a TRefine. */
|
|
18
32
|
export function IsRefine(value) {
|
|
19
|
-
return IsSchema(value)
|
|
33
|
+
return IsSchema(value)
|
|
34
|
+
&& Guard.HasPropertyKey(value, '~refine')
|
|
35
|
+
&& Guard.IsArray(value['~refine'])
|
|
36
|
+
&& Guard.Every(value['~refine'], 0, value => IsRefinement(value));
|
|
20
37
|
}
|
|
@@ -30,8 +30,9 @@ export type TRequiredArray<Properties extends TProperties, RequiredProperties ex
|
|
|
30
30
|
}, RequiredKeys extends string[] = TUnionToTuple<Extract<keyof RequiredProperties, string>>, Result extends string[] | undefined = RequiredKeys extends [] ? undefined : RequiredKeys> = Result;
|
|
31
31
|
/** Creates a RequiredArray derived from the given TProperties value. */
|
|
32
32
|
export declare function RequiredArray<Properties extends TProperties>(properties: Properties): TRequiredArray<Properties>;
|
|
33
|
+
type TKeyToString<Key extends number | string> = `${Key}`;
|
|
33
34
|
/** Extracts a tuple of keys from a TProperties value. */
|
|
34
|
-
export type TPropertyKeys<Properties extends TProperties,
|
|
35
|
+
export type TPropertyKeys<Properties extends TProperties, ExtractKey extends number | string = Extract<keyof Properties, number | string>, StringKey extends string = TKeyToString<ExtractKey>, Result extends string[] = TUnionToTuple<StringKey>> = Result;
|
|
35
36
|
/** Extracts a tuple of keys from a TProperties value. */
|
|
36
37
|
export declare function PropertyKeys<Properties extends TProperties>(properties: Properties): TPropertyKeys<Properties>;
|
|
37
38
|
type TPropertyValuesReduce<Properties extends TProperties, Keys extends string[], Result extends TSchema[] = []> = Keys extends [infer Left extends string, ...infer Right extends string[]] ? Left extends keyof Properties ? TPropertyValuesReduce<Properties, Right, [...Result, Properties[Left]]> : TPropertyValuesReduce<Properties, Right, Result> : Result;
|
|
@@ -27,8 +27,6 @@ export interface TRecord<Key extends string = string, Value extends TSchema = TS
|
|
|
27
27
|
export type TRecordDeferred<Key extends TSchema = TSchema, Value extends TSchema = TSchema> = (TDeferred<'Record', [Key, Value]>);
|
|
28
28
|
/** Represents a deferred Record action. */
|
|
29
29
|
export declare function RecordDeferred<Key extends TSchema, Value extends TSchema>(key: Key, value: Value, options?: TObjectOptions): TRecordDeferred<Key, Value>;
|
|
30
|
-
/** Returns true if this value is a deferred Interface action. */
|
|
31
|
-
export declare function IsRecordDeferred(value: unknown): value is TRecordDeferred;
|
|
32
30
|
/** Creates a Record type. */
|
|
33
31
|
export declare function Record<Key extends TSchema, Value extends TSchema>(key: Key, value: Value, options?: TObjectOptions): TRecordAction<Key, Value>;
|
|
34
32
|
/** Creates a Record type from regular expression pattern. */
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// deno-fmt-ignore-file
|
|
2
2
|
import { Memory } from '../../system/memory/index.mjs';
|
|
3
3
|
import { Guard } from '../../guard/index.mjs';
|
|
4
|
-
import { IsKind
|
|
4
|
+
import { IsKind } from './schema.mjs';
|
|
5
5
|
import { Integer, IntegerPattern } from './integer.mjs';
|
|
6
6
|
import { Number, NumberPattern } from './number.mjs';
|
|
7
7
|
import { String, StringPattern } from './string.mjs';
|
|
@@ -16,15 +16,6 @@ export const StringKey = `^${StringPattern}$`;
|
|
|
16
16
|
export function RecordDeferred(key, value, options = {}) {
|
|
17
17
|
return Deferred('Record', [key, value], options);
|
|
18
18
|
}
|
|
19
|
-
// ------------------------------------------------------------------
|
|
20
|
-
// Guard
|
|
21
|
-
// ------------------------------------------------------------------
|
|
22
|
-
/** Returns true if this value is a deferred Interface action. */
|
|
23
|
-
export function IsRecordDeferred(value) {
|
|
24
|
-
return IsSchema(value)
|
|
25
|
-
&& Guard.HasPropertyKey(value, 'action')
|
|
26
|
-
&& Guard.IsEqual(value.action, 'Record');
|
|
27
|
-
}
|
|
28
19
|
// -------------------------------------------------------------------
|
|
29
20
|
// Factory
|
|
30
21
|
// -------------------------------------------------------------------
|
|
@@ -2,8 +2,7 @@ import { type TSchema } from './schema.mjs';
|
|
|
2
2
|
export type StaticUnsafe<Type extends unknown> = Type;
|
|
3
3
|
/** Represents an Unsafe type. */
|
|
4
4
|
export interface TUnsafe<Type extends unknown = unknown> extends TSchema {
|
|
5
|
-
'~
|
|
6
|
-
'~hint': Type;
|
|
5
|
+
'~unsafe': Type;
|
|
7
6
|
}
|
|
8
7
|
/** Creates a Unsafe type. */
|
|
9
8
|
export declare function Unsafe<Type extends unknown>(schema: TSchema): TUnsafe<Type>;
|
|
@@ -1,18 +1,20 @@
|
|
|
1
1
|
// deno-fmt-ignore-file
|
|
2
2
|
// deno-lint-ignore-file
|
|
3
|
+
import { Guard } from '../../guard/index.mjs';
|
|
3
4
|
import { Memory } from '../../system/memory/index.mjs';
|
|
4
|
-
import { IsKind } from './schema.mjs';
|
|
5
5
|
// ------------------------------------------------------------------
|
|
6
6
|
// Factory
|
|
7
7
|
// ------------------------------------------------------------------
|
|
8
8
|
/** Creates a Unsafe type. */
|
|
9
9
|
export function Unsafe(schema) {
|
|
10
|
-
return Memory.
|
|
10
|
+
return Memory.Update(schema, { ['~unsafe']: null }, {});
|
|
11
11
|
}
|
|
12
12
|
// ------------------------------------------------------------------
|
|
13
13
|
// Guard
|
|
14
14
|
// ------------------------------------------------------------------
|
|
15
15
|
/** Returns true if the given value is TUnsafe. */
|
|
16
16
|
export function IsUnsafe(value) {
|
|
17
|
-
return
|
|
17
|
+
return Guard.IsObjectNotArray(value)
|
|
18
|
+
&& Guard.HasPropertyKey(value, '~unsafe')
|
|
19
|
+
&& Guard.IsNull(value['~unsafe']);
|
|
18
20
|
}
|
package/build/typebox.d.mts
CHANGED
|
@@ -30,7 +30,7 @@ export { Codec, Decode, DecodeBuilder, Encode, EncodeBuilder, IsCodec, type TCod
|
|
|
30
30
|
export { Immutable, IsImmutable, type TImmutable } from './type/types/_immutable.mjs';
|
|
31
31
|
export { IsOptional, Optional, type TOptional } from './type/types/_optional.mjs';
|
|
32
32
|
export { IsReadonly, Readonly, type TReadonly } from './type/types/_readonly.mjs';
|
|
33
|
-
export { IsRefine, Refine, type TRefine, type
|
|
33
|
+
export { IsRefine, Refine, type TRefine, type TRefineCheckCallback, type TRefineErrorCallback, type TRefinement } from './type/types/_refine.mjs';
|
|
34
34
|
export { Any, IsAny, type TAny } from './type/types/any.mjs';
|
|
35
35
|
export { Array, IsArray, type TArray } from './type/types/array.mjs';
|
|
36
36
|
export { AsyncIterator, IsAsyncIterator, type TAsyncIterator } from './type/types/async-iterator.mjs';
|
|
@@ -5,5 +5,7 @@ export type TMutable = {
|
|
|
5
5
|
* Performs a deep structural assignment, applying values from next to current while retaining internal references. This function
|
|
6
6
|
* is written for use in infrastructure that interprets reference changes as a signal to perform some action (i.e. React redraw), this
|
|
7
7
|
* function can mitigate this by applying mutable updates deep within a value, ensuring parent references are retained.
|
|
8
|
+
*
|
|
9
|
+
* @deprecated This function is being removed in the next version but will be retained as a reference under examples.
|
|
8
10
|
*/
|
|
9
11
|
export declare function Mutate(current: TMutable, next: TMutable): void;
|
|
@@ -29,6 +29,8 @@ function IsMismatchedValue(left, right) {
|
|
|
29
29
|
* Performs a deep structural assignment, applying values from next to current while retaining internal references. This function
|
|
30
30
|
* is written for use in infrastructure that interprets reference changes as a signal to perform some action (i.e. React redraw), this
|
|
31
31
|
* function can mitigate this by applying mutable updates deep within a value, ensuring parent references are retained.
|
|
32
|
+
*
|
|
33
|
+
* @deprecated This function is being removed in the next version but will be retained as a reference under examples.
|
|
32
34
|
*/
|
|
33
35
|
export function Mutate(current, next) {
|
|
34
36
|
if (IsNonMutableValue(current) || IsNonMutableValue(next))
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "typebox",
|
|
3
3
|
"description": "Json Schema Type Builder with Static Type Resolution for TypeScript",
|
|
4
|
-
"version": "1.1.
|
|
4
|
+
"version": "1.1.30",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"typescript",
|
|
7
7
|
"jsonschema"
|
|
@@ -16,37 +16,37 @@
|
|
|
16
16
|
"types": "./build/index.d.mts",
|
|
17
17
|
"module": "./build/index.mjs",
|
|
18
18
|
"exports": {
|
|
19
|
+
"./value": {
|
|
20
|
+
"import": "./build/value/index.mjs",
|
|
21
|
+
"default": "./build/value/index.mjs"
|
|
22
|
+
},
|
|
19
23
|
"./guard": {
|
|
20
24
|
"import": "./build/guard/index.mjs",
|
|
21
25
|
"default": "./build/guard/index.mjs"
|
|
22
26
|
},
|
|
23
|
-
"./
|
|
24
|
-
"import": "./build/
|
|
25
|
-
"default": "./build/
|
|
26
|
-
},
|
|
27
|
-
"./compile": {
|
|
28
|
-
"import": "./build/compile/index.mjs",
|
|
29
|
-
"default": "./build/compile/index.mjs"
|
|
27
|
+
"./schema": {
|
|
28
|
+
"import": "./build/schema/index.mjs",
|
|
29
|
+
"default": "./build/schema/index.mjs"
|
|
30
30
|
},
|
|
31
31
|
"./system": {
|
|
32
32
|
"import": "./build/system/index.mjs",
|
|
33
33
|
"default": "./build/system/index.mjs"
|
|
34
34
|
},
|
|
35
|
-
"./format": {
|
|
36
|
-
"import": "./build/format/index.mjs",
|
|
37
|
-
"default": "./build/format/index.mjs"
|
|
38
|
-
},
|
|
39
35
|
"./type": {
|
|
40
36
|
"import": "./build/type/index.mjs",
|
|
41
37
|
"default": "./build/type/index.mjs"
|
|
42
38
|
},
|
|
43
|
-
"./
|
|
44
|
-
"import": "./build/
|
|
45
|
-
"default": "./build/
|
|
39
|
+
"./compile": {
|
|
40
|
+
"import": "./build/compile/index.mjs",
|
|
41
|
+
"default": "./build/compile/index.mjs"
|
|
46
42
|
},
|
|
47
|
-
"./
|
|
48
|
-
"import": "./build/
|
|
49
|
-
"default": "./build/
|
|
43
|
+
"./error": {
|
|
44
|
+
"import": "./build/error/index.mjs",
|
|
45
|
+
"default": "./build/error/index.mjs"
|
|
46
|
+
},
|
|
47
|
+
"./format": {
|
|
48
|
+
"import": "./build/format/index.mjs",
|
|
49
|
+
"default": "./build/format/index.mjs"
|
|
50
50
|
},
|
|
51
51
|
".": {
|
|
52
52
|
"import": "./build/index.mjs",
|
|
@@ -55,29 +55,29 @@
|
|
|
55
55
|
},
|
|
56
56
|
"typesVersions": {
|
|
57
57
|
"*": {
|
|
58
|
+
"value": [
|
|
59
|
+
"./build/value/index.d.mts"
|
|
60
|
+
],
|
|
58
61
|
"guard": [
|
|
59
62
|
"./build/guard/index.d.mts"
|
|
60
63
|
],
|
|
61
|
-
"
|
|
62
|
-
"./build/
|
|
63
|
-
],
|
|
64
|
-
"compile": [
|
|
65
|
-
"./build/compile/index.d.mts"
|
|
64
|
+
"schema": [
|
|
65
|
+
"./build/schema/index.d.mts"
|
|
66
66
|
],
|
|
67
67
|
"system": [
|
|
68
68
|
"./build/system/index.d.mts"
|
|
69
69
|
],
|
|
70
|
-
"format": [
|
|
71
|
-
"./build/format/index.d.mts"
|
|
72
|
-
],
|
|
73
70
|
"type": [
|
|
74
71
|
"./build/type/index.d.mts"
|
|
75
72
|
],
|
|
76
|
-
"
|
|
77
|
-
"./build/
|
|
73
|
+
"compile": [
|
|
74
|
+
"./build/compile/index.d.mts"
|
|
78
75
|
],
|
|
79
|
-
"
|
|
80
|
-
"./build/
|
|
76
|
+
"error": [
|
|
77
|
+
"./build/error/index.d.mts"
|
|
78
|
+
],
|
|
79
|
+
"format": [
|
|
80
|
+
"./build/format/index.d.mts"
|
|
81
81
|
],
|
|
82
82
|
".": [
|
|
83
83
|
"./build/index.d.mts"
|