typebox 1.0.6 → 1.0.7

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.
@@ -4,6 +4,7 @@ export declare class Validator<Context extends TProperties = TProperties, Type e
4
4
  private readonly context;
5
5
  private readonly type;
6
6
  private readonly isEvaluated;
7
+ private readonly hasCodec;
7
8
  private readonly code;
8
9
  private readonly check;
9
10
  constructor(context: Context, type: Type);
@@ -15,16 +16,22 @@ export declare class Validator<Context extends TProperties = TProperties, Type e
15
16
  Type(): Type;
16
17
  /** Returns the generated code for this validator */
17
18
  Code(): string;
19
+ /** Checks a value matches the Validator type. */
18
20
  Check(value: unknown): value is StaticEncode<Type, Context>;
21
+ /** Returns errors for the given value. */
19
22
  Errors(value: unknown): TLocalizedValidationError[];
23
+ /** Cleans a value using the Validator type. */
20
24
  Clean(value: unknown): unknown;
25
+ /** Converts a value using the Validator type. */
21
26
  Convert(value: unknown): unknown;
27
+ /** Creates a value using the Validator type. */
22
28
  Create(): StaticEncode<Type, Context>;
29
+ /** Creates defaults using the Validator type. */
23
30
  Default(value: unknown): unknown;
24
- /** Parses a value of this type */
31
+ /** Parses a value */
25
32
  Parse(value: unknown): StaticDecode<Type, Context>;
26
- /** Decodes a value of this type */
33
+ /** Decodes a value */
27
34
  Decode(value: unknown): StaticDecode<Type, Context>;
28
- /** Encodes a value of this type */
35
+ /** Encodes a value */
29
36
  Encode(value: unknown): StaticEncode<Type, Context>;
30
37
  }
@@ -1,7 +1,7 @@
1
1
  // deno-fmt-ignore-file
2
2
  import { Environment } from '../system/environment/index.mjs';
3
3
  import { Base } from '../type/index.mjs';
4
- import { Errors, Clean, Convert, Create, Default, Parser, Decode, Encode } from '../value/index.mjs';
4
+ import { Errors, Clean, Convert, Create, Default, Decode, Encode, HasCodec, Parser } from '../value/index.mjs';
5
5
  import { Build } from '../schema/index.mjs';
6
6
  // ------------------------------------------------------------------
7
7
  // ValidatorType<...>
@@ -12,6 +12,7 @@ export class Validator extends Base {
12
12
  this.context = context;
13
13
  this.type = type;
14
14
  const result = Build(context, type).Evaluate();
15
+ this.hasCodec = HasCodec(context, type);
15
16
  this.isEvaluated = result.IsEvaluated;
16
17
  this.code = result.Code;
17
18
  this.check = result.Check;
@@ -44,40 +45,48 @@ export class Validator extends Base {
44
45
  // ----------------------------------------------------------------
45
46
  // Base<...>
46
47
  // ----------------------------------------------------------------
48
+ /** Checks a value matches the Validator type. */
47
49
  Check(value) {
48
50
  return this.check(value);
49
51
  }
52
+ /** Returns errors for the given value. */
50
53
  Errors(value) {
51
54
  if (Environment.CanEvaluate() && this.check(value))
52
55
  return [];
53
56
  return Errors(this.context, this.type, value);
54
57
  }
58
+ /** Cleans a value using the Validator type. */
55
59
  Clean(value) {
56
60
  return Clean(this.context, this.type, value);
57
61
  }
62
+ /** Converts a value using the Validator type. */
58
63
  Convert(value) {
59
64
  return Convert(this.context, this.type, value);
60
65
  }
66
+ /** Creates a value using the Validator type. */
61
67
  Create() {
62
68
  return Create(this.context, this.type);
63
69
  }
70
+ /** Creates defaults using the Validator type. */
64
71
  Default(value) {
65
72
  return Default(this.context, this.type, value);
66
73
  }
67
74
  // ----------------------------------------------------------------
68
- // Parse
75
+ // Parse | Decode | Encode
69
76
  // ----------------------------------------------------------------
70
- /** Parses a value of this type */
77
+ /** Parses a value */
71
78
  Parse(value) {
72
79
  const result = this.Check(value) ? value : Parser(this.context, this.type, value);
73
80
  return result;
74
81
  }
75
- /** Decodes a value of this type */
82
+ /** Decodes a value */
76
83
  Decode(value) {
77
- return Decode(this.context, this.type, value);
84
+ const result = this.hasCodec ? Decode(this.context, this.type, value) : this.Parse(value);
85
+ return result;
78
86
  }
79
- /** Encodes a value of this type */
87
+ /** Encodes a value */
80
88
  Encode(value) {
81
- return Encode(this.context, this.type, value);
89
+ const result = this.hasCodec ? Encode(this.context, this.type, value) : this.Parse(value);
90
+ return result;
82
91
  }
83
92
  }
@@ -1,5 +1,5 @@
1
1
  import type { Static, TProperties, TSchema } from '../../type/index.mjs';
2
- /** Checks a value matches the provided type. This function returns a TypeScript type predicate and does not throw. */
2
+ /** Checks a value matches the provided type. */
3
3
  export declare function Check<const Type extends TSchema, Result extends unknown = Static<Type>>(type: Type, value: unknown): value is Result;
4
- /** Checks a value matches the provided type. This function returns a TypeScript type predicate and does not throw. */
4
+ /** Checks a value matches the provided type. */
5
5
  export declare function Check<Context extends TProperties, const Type extends TSchema, Result extends unknown = Static<Type, Context>>(context: Context, type: Type, value: unknown): value is Result;
@@ -1,7 +1,7 @@
1
1
  // deno-fmt-ignore-file
2
2
  import { Arguments } from '../../system/arguments/index.mjs';
3
3
  import { Check as SchemaCheck } from '../../schema/index.mjs';
4
- /** Checks a value matches the provided type. This function returns a TypeScript type predicate and does not throw. */
4
+ /** Checks a value matches the provided type. */
5
5
  export function Check(...args) {
6
6
  const [context, type, value] = Arguments.Match(args, {
7
7
  3: (context, type, value) => [context, type, value],
@@ -46,6 +46,9 @@ function FromRecord(context, type) {
46
46
  // Ref
47
47
  // ------------------------------------------------------------------
48
48
  function FromRef(context, type) {
49
+ if (visited.has(type.$ref))
50
+ return false;
51
+ visited.add(type.$ref);
49
52
  return IsCodec(type) || (Guard.HasPropertyKey(context, type.$ref)
50
53
  && FromType(context, context[type.$ref]));
51
54
  }
@@ -75,11 +78,16 @@ function FromType(context, type) {
75
78
  IsUnion(type) ? FromUnion(context, type) :
76
79
  IsCodec(type));
77
80
  }
81
+ // ------------------------------------------------------------------
82
+ // Visited
83
+ // ------------------------------------------------------------------
84
+ const visited = new Set();
78
85
  /** Returns true if this type contains a Codec */
79
86
  export function HasCodec(...args) {
80
87
  const [context, type] = Arguments.Match(args, {
81
88
  2: (context, type) => [context, type],
82
89
  1: (type) => [{}, type]
83
90
  });
91
+ visited.clear();
84
92
  return FromType(context, type);
85
93
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "typebox",
3
3
  "description": "A Runtime Type System for JavaScript",
4
- "version": "1.0.6",
4
+ "version": "1.0.7",
5
5
  "keywords": [
6
6
  "typescript",
7
7
  "jsonschema"
package/readme.md CHANGED
@@ -33,7 +33,7 @@ import Type, { type Static } from 'typebox'
33
33
  const T = Type.Object({ // const T = {
34
34
  x: Type.Number(), // type: 'object',
35
35
  y: Type.Number(), // required: ['x', 'y', 'z'],
36
- z: Type.Number(), // properties: {
36
+ z: Type.Number() // properties: {
37
37
  }) // x: { type: 'number' },
38
38
  // y: { type: 'number' },
39
39
  // z: { type: 'number' }
@@ -49,25 +49,28 @@ type T = Static<typeof T> // type T = {
49
49
 
50
50
  ## Overview
51
51
 
52
- [Documentation](https://sinclairzx81.github.io/typebox/) | [1.0 Migration Guide](https://github.com/sinclairzx81/typebox/blob/main/changelog/1.0.0-migration.md)
52
+ [Documentation](https://sinclairzx81.github.io/typebox/)
53
53
 
54
54
  TypeBox is a runtime type system that creates in-memory Json Schema objects that infer as TypeScript types. The schematics produced by this library are designed to match the static type checking rules of the TypeScript compiler. TypeBox offers a unified type system that can be statically checked by TypeScript and validated at runtime using standard Json Schema.
55
55
 
56
56
  This library is designed to allow Json Schema to compose similar to how types compose within TypeScript's type system. It can be used as a simple tool to build up complex schematics or integrated into REST and RPC services to help validate data received over the wire.
57
57
 
58
-
59
-
60
58
  License: MIT
61
59
 
62
60
  ## Contents
63
61
 
62
+ - [Upgrade](#Upgrade)
64
63
  - [Type](#Type)
65
64
  - [Script](#Script)
66
65
  - [Value](#Value)
67
66
  - [Compile](#Compile)
68
67
  - [Contribute](#Contribute)
69
68
 
69
+ ## Upgrade
70
+
71
+ Refer to the following URL for information on upgrading from 0.34.x to 1.0.
70
72
 
73
+ [1.0 Migration Guide](https://github.com/sinclairzx81/typebox/blob/main/changelog/1.0.0-migration.md)
71
74
 
72
75
  <a name="Type"></a>
73
76