typebox 1.0.50 → 1.0.52

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.
@@ -7,14 +7,17 @@ export declare class Validator<Context extends TProperties = TProperties, Type e
7
7
  private readonly hasCodec;
8
8
  private readonly code;
9
9
  private readonly check;
10
+ /** Constructs a Validator with the given Context and Type. */
10
11
  constructor(context: Context, type: Type);
11
- /** Returns true if this validator is using runtime eval optimizations */
12
+ /** Constructs a Validator with the given arguments. */
13
+ constructor(context: Context, type: Type, isEvaluated: boolean, hasCodec: boolean, code: string, check: (value: unknown) => boolean);
14
+ /** Returns true if this validator is using runtime eval optimizations. */
12
15
  IsEvaluated(): boolean;
13
- /** Returns the Context for this validator */
16
+ /** Returns the Context for this validator. */
14
17
  Context(): Context;
15
- /** Returns the Type for this validator */
18
+ /** Returns the Type for this validator. */
16
19
  Type(): Type;
17
- /** Returns the generated code for this validator */
20
+ /** Returns the generated code for this validator. */
18
21
  Code(): string;
19
22
  /** Checks a value matches the Validator type. */
20
23
  Check(value: unknown): value is StaticEncode<Type, Context>;
@@ -28,6 +31,8 @@ export declare class Validator<Context extends TProperties = TProperties, Type e
28
31
  Create(): StaticEncode<Type, Context>;
29
32
  /** Creates defaults using the Validator type. */
30
33
  Default(value: unknown): unknown;
34
+ /** Clones this validator. */
35
+ Clone(): Validator<Context, Type>;
31
36
  /** Parses a value */
32
37
  Parse(value: unknown): StaticDecode<Type, Context>;
33
38
  /** Decodes a value */
@@ -1,44 +1,62 @@
1
1
  // deno-fmt-ignore-file
2
+ import { Arguments } from '../system/arguments/index.mjs';
2
3
  import { Environment } from '../system/environment/index.mjs';
3
4
  import { Base } from '../type/index.mjs';
4
5
  import { Errors, Clean, Convert, Create, Default, Decode, Encode, HasCodec, Parser } from '../value/index.mjs';
5
6
  import { Build } from '../schema/index.mjs';
6
7
  // ------------------------------------------------------------------
7
- // ValidatorType<...>
8
+ // Validator<...>
8
9
  // ------------------------------------------------------------------
9
10
  export class Validator extends Base {
10
- constructor(context, type) {
11
+ /** Constructs a Validator. */
12
+ constructor(...args) {
11
13
  super();
12
- this.context = context;
13
- this.type = type;
14
- const result = Build(context, type).Evaluate();
15
- this.hasCodec = HasCodec(context, type);
16
- this.isEvaluated = result.IsEvaluated;
17
- this.code = result.Code;
18
- this.check = result.Check;
14
+ const matched = Arguments.Match(args, {
15
+ 6: (context, type, isEvalulated, hasCodec, code, check) => [context, type, isEvalulated, hasCodec, code, check],
16
+ 2: (context, type) => [context, type]
17
+ });
18
+ if (matched.length === 6) {
19
+ const [context, type, isEvaluated, hasCodec, code, check] = matched;
20
+ this.context = context;
21
+ this.type = type;
22
+ this.isEvaluated = isEvaluated;
23
+ this.hasCodec = hasCodec;
24
+ this.code = code;
25
+ this.check = check;
26
+ }
27
+ else {
28
+ const [context, type] = matched;
29
+ const result = Build(context, type).Evaluate();
30
+ this.hasCodec = HasCodec(context, type);
31
+ this.context = context;
32
+ this.type = type;
33
+ this.isEvaluated = result.IsEvaluated;
34
+ this.code = result.Code;
35
+ this.check = result.Check;
36
+ }
19
37
  }
20
38
  // ----------------------------------------------------------------
21
- // Evaluated
39
+ // IsEvaluated
22
40
  // ----------------------------------------------------------------
23
- /** Returns true if this validator is using runtime eval optimizations */
41
+ /** Returns true if this validator is using runtime eval optimizations. */
24
42
  IsEvaluated() {
25
43
  return this.isEvaluated;
26
44
  }
27
45
  // ----------------------------------------------------------------
28
- // Schema
46
+ // Context | Type
29
47
  // ----------------------------------------------------------------
30
- /** Returns the Context for this validator */
48
+ /** Returns the Context for this validator. */
31
49
  Context() {
32
50
  return this.context;
33
51
  }
34
- /** Returns the Type for this validator */
52
+ /** Returns the Type for this validator. */
35
53
  Type() {
36
54
  return this.type;
37
55
  }
38
56
  // ----------------------------------------------------------------
39
57
  // Code
40
58
  // ----------------------------------------------------------------
41
- /** Returns the generated code for this validator */
59
+ /** Returns the generated code for this validator. */
42
60
  Code() {
43
61
  return this.code;
44
62
  }
@@ -71,6 +89,10 @@ export class Validator extends Base {
71
89
  Default(value) {
72
90
  return Default(this.context, this.type, value);
73
91
  }
92
+ /** Clones this validator. */
93
+ Clone() {
94
+ return new Validator(this.context, this.type, this.isEvaluated, this.hasCodec, this.code, this.check);
95
+ }
74
96
  // ----------------------------------------------------------------
75
97
  // Parse | Decode | Encode
76
98
  // ----------------------------------------------------------------
@@ -156,7 +156,7 @@ type TInstantiateDeferred<Context extends TProperties, State extends TState, Act
156
156
  Action,
157
157
  Parameters
158
158
  ] extends ['Uppercase', [infer Type extends TSchema]] ? TUppercaseInstantiate<Context, State, Type> : TDeferred<Action, Parameters>);
159
- export type TInstantiateType<Context extends TProperties, State extends TState, Input extends TSchema, IsImmutable extends boolean = Input extends TImmutable ? true : false, ModifierState extends [TSchema, ModifierAction, ModifierAction] = TModifierActions<Input, Input extends TReadonly<Input> ? 'add' : 'none', Input extends TOptional<Input> ? 'add' : 'none'>, Type extends TSchema = ModifierState[0], Instantiated extends TSchema = (Type extends TRef<infer Ref extends string> ? TRefInstantiate<Context, State, Ref> : Type extends TArray<infer Type extends TSchema> ? TArray<TInstantiateType<Context, State, Type>> : Type extends TAsyncIterator<infer Type extends TSchema> ? TAsyncIterator<TInstantiateType<Context, State, Type>> : Type extends TCall<infer Target extends TSchema, infer Parameters extends TSchema[]> ? TCallInstantiate<Context, State, Target, Parameters> : Type extends TConstructor<infer Parameters extends TSchema[], infer InstanceType extends TSchema> ? TConstructor<TInstantiateTypes<Context, State, Parameters>, TInstantiateType<Context, State, InstanceType>> : Type extends TDeferred<infer Action extends string, infer Types extends TSchema[]> ? TInstantiateDeferred<Context, State, Action, Types> : Type extends TFunction<infer Parameters extends TSchema[], infer ReturnType extends TSchema> ? TFunction<TInstantiateTypes<Context, State, Parameters>, TInstantiateType<Context, State, ReturnType>> : Type extends TIntersect<infer Types extends TSchema[]> ? TIntersect<TInstantiateTypes<Context, State, Types>> : Type extends TIterator<infer Type extends TSchema> ? TIterator<TInstantiateType<Context, State, Type>> : Type extends TObject<infer Properties extends TProperties> ? TObject<TInstantiateProperties<Context, State, Properties>> : Type extends TPromise<infer Type extends TSchema> ? TPromise<TInstantiateType<Context, State, Type>> : Type extends TRecord<infer Key extends string, infer Type extends TSchema> ? TRecord<Key, TInstantiateType<Context, State, Type>> : Type extends TRest<infer Type extends TSchema> ? TRest<TInstantiateType<Context, State, Type>> : Type extends TTuple<infer Types extends TSchema[]> ? TTuple<TInstantiateElements<Context, State, Types>> : Type extends TUnion<infer Types extends TSchema[]> ? TUnion<TInstantiateTypes<Context, State, Types>> : Type), WithImmutable extends TSchema = IsImmutable extends true ? TImmutable<Instantiated> : Instantiated, WithModifiers extends TSchema = TApplyReadonly<ModifierState[1], TApplyOptional<ModifierState[2], WithImmutable>>> = WithModifiers;
159
+ export type TInstantiateType<Context extends TProperties, State extends TState, Input extends TSchema, Immutable extends boolean = Input extends TImmutable ? true : false, Modifiers extends [TSchema, ModifierAction, ModifierAction] = TModifierActions<Input, Input extends TReadonly<Input> ? 'add' : 'none', Input extends TOptional<Input> ? 'add' : 'none'>, Type extends TSchema = Modifiers[0], Instantiated extends TSchema = (Type extends TRef<infer Ref extends string> ? TRefInstantiate<Context, State, Ref> : Type extends TArray<infer Type extends TSchema> ? TArray<TInstantiateType<Context, State, Type>> : Type extends TAsyncIterator<infer Type extends TSchema> ? TAsyncIterator<TInstantiateType<Context, State, Type>> : Type extends TCall<infer Target extends TSchema, infer Parameters extends TSchema[]> ? TCallInstantiate<Context, State, Target, Parameters> : Type extends TConstructor<infer Parameters extends TSchema[], infer InstanceType extends TSchema> ? TConstructor<TInstantiateTypes<Context, State, Parameters>, TInstantiateType<Context, State, InstanceType>> : Type extends TDeferred<infer Action extends string, infer Types extends TSchema[]> ? TInstantiateDeferred<Context, State, Action, Types> : Type extends TFunction<infer Parameters extends TSchema[], infer ReturnType extends TSchema> ? TFunction<TInstantiateTypes<Context, State, Parameters>, TInstantiateType<Context, State, ReturnType>> : Type extends TIntersect<infer Types extends TSchema[]> ? TIntersect<TInstantiateTypes<Context, State, Types>> : Type extends TIterator<infer Type extends TSchema> ? TIterator<TInstantiateType<Context, State, Type>> : Type extends TObject<infer Properties extends TProperties> ? TObject<TInstantiateProperties<Context, State, Properties>> : Type extends TPromise<infer Type extends TSchema> ? TPromise<TInstantiateType<Context, State, Type>> : Type extends TRecord<infer Key extends string, infer Type extends TSchema> ? TRecord<Key, TInstantiateType<Context, State, Type>> : Type extends TRest<infer Type extends TSchema> ? TRest<TInstantiateType<Context, State, Type>> : Type extends TTuple<infer Types extends TSchema[]> ? TTuple<TInstantiateElements<Context, State, Types>> : Type extends TUnion<infer Types extends TSchema[]> ? TUnion<TInstantiateTypes<Context, State, Types>> : Type), WithImmutable extends TSchema = Immutable extends true ? TImmutable<Instantiated> : Instantiated, WithModifiers extends TSchema = TApplyReadonly<Modifiers[1], TApplyOptional<Modifiers[2], WithImmutable>>> = WithModifiers;
160
160
  export declare function InstantiateType<Context extends TProperties, State extends TState, Type extends TSchema>(context: Context, state: State, input: Type): TInstantiateType<Context, State, Type>;
161
161
  /** Instantiates computed schematics using the given context and type. */
162
162
  export type TInstantiate<Context extends TProperties, Type extends TSchema> = (TInstantiateType<Context, {
@@ -13,6 +13,7 @@ import { IsReadonly, ReadonlyAdd, ReadonlyRemove } from '../types/_readonly.mjs'
13
13
  import { IsSchema } from '../types/schema.mjs';
14
14
  import { Array, IsArray, ArrayOptions } from '../types/array.mjs';
15
15
  import { AsyncIterator, IsAsyncIterator, AsyncIteratorOptions } from '../types/async-iterator.mjs';
16
+ import { IsBase } from '../types/base.mjs';
16
17
  import { Constructor, IsConstructor, ConstructorOptions } from '../types/constructor.mjs';
17
18
  import { Deferred, IsDeferred } from '../types/deferred.mjs';
18
19
  import { Function as _Function, IsFunction, FunctionOptions } from '../types/function.mjs';
@@ -142,9 +143,9 @@ function InstantiateDeferred(context, state, action, parameters, options) {
142
143
  Deferred(action, parameters, options));
143
144
  }
144
145
  export function InstantiateType(context, state, input) {
145
- const isImmutable = IsImmutable(input);
146
- const modifierActions = ModifierActions(input, IsReadonly(input) ? 'add' : 'none', IsOptional(input) ? 'add' : 'none');
147
- const type = modifierActions[0];
146
+ const immutable = IsImmutable(input);
147
+ const modifiers = ModifierActions(input, IsReadonly(input) ? 'add' : 'none', IsOptional(input) ? 'add' : 'none');
148
+ const type = IsBase(modifiers[0]) ? modifiers[0].Clone() : modifiers[0];
148
149
  const instantiated = (IsRef(type) ? RefInstantiate(context, state, type.$ref) :
149
150
  IsArray(type) ? Array(InstantiateType(context, state, type.items), ArrayOptions(type)) :
150
151
  IsAsyncIterator(type) ? AsyncIterator(InstantiateType(context, state, type.iteratorItems), AsyncIteratorOptions(type)) :
@@ -161,8 +162,8 @@ export function InstantiateType(context, state, input) {
161
162
  IsTuple(type) ? Tuple(InstantiateElements(context, state, type.items), TupleOptions(type)) :
162
163
  IsUnion(type) ? Union(InstantiateTypes(context, state, type.anyOf), UnionOptions(type)) :
163
164
  type);
164
- const withImmutable = isImmutable ? Immutable(instantiated) : instantiated;
165
- const withModifiers = ApplyReadonly(modifierActions[1], ApplyOptional(modifierActions[2], withImmutable));
165
+ const withImmutable = immutable ? Immutable(instantiated) : instantiated;
166
+ const withModifiers = ApplyReadonly(modifiers[1], ApplyOptional(modifiers[2], withImmutable));
166
167
  return withModifiers;
167
168
  }
168
169
  /** Instantiates computed schematics using the given context and type. */
@@ -18,6 +18,8 @@ export declare class Base<Value extends unknown = unknown> implements TSchema, X
18
18
  Default(value: unknown): unknown;
19
19
  /** Creates a new instance of this type */
20
20
  Create(): Value;
21
+ /** Clones this type */
22
+ Clone(): Base;
21
23
  }
22
24
  /** Returns true if the given value is a Base type. */
23
25
  export declare function IsBase(value: unknown): value is Base;
@@ -4,25 +4,22 @@ import { IsKind } from './schema.mjs';
4
4
  // ------------------------------------------------------------------
5
5
  // Type.Base<...>
6
6
  // ------------------------------------------------------------------
7
+ function BaseProperty(value) {
8
+ return {
9
+ enumerable: Settings.Get().enumerableKind,
10
+ writable: false,
11
+ configurable: false,
12
+ value
13
+ };
14
+ }
7
15
  /** Base class for creating extension types. */
8
16
  export class Base {
9
17
  constructor() {
10
- const configuration = {
11
- enumerable: Settings.Get().enumerableKind,
12
- writable: false,
13
- configurable: false,
14
- };
15
- globalThis.Object.defineProperty(this, '~kind', {
16
- ...configuration,
17
- value: 'Base'
18
- });
19
- globalThis.Object.defineProperty(this, '~guard', {
20
- ...configuration,
21
- value: {
22
- check: (value) => this.Check(value),
23
- errors: (value) => this.Errors(value)
24
- }
25
- });
18
+ globalThis.Object.defineProperty(this, '~kind', BaseProperty('Base'));
19
+ globalThis.Object.defineProperty(this, '~guard', BaseProperty({
20
+ check: (value) => this.Check(value),
21
+ errors: (value) => this.Errors(value)
22
+ }));
26
23
  }
27
24
  /** Checks a value or returns false if invalid */
28
25
  Check(value) {
@@ -48,6 +45,10 @@ export class Base {
48
45
  Create() {
49
46
  throw new Error('Create not implemented');
50
47
  }
48
+ /** Clones this type */
49
+ Clone() {
50
+ throw Error('Clone not implemented');
51
+ }
51
52
  }
52
53
  // ------------------------------------------------------------------
53
54
  // Guard
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.0.50",
4
+ "version": "1.0.52",
5
5
  "keywords": [
6
6
  "typescript",
7
7
  "jsonschema"