typebox 1.0.22 → 1.0.23

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.
@@ -1,6 +1,4 @@
1
1
  // deno-fmt-ignore-file
2
- import { IsLocalizedValidationError } from '../../error/index.mjs';
3
- import { Guard } from '../../guard/index.mjs';
4
2
  import { IsKind } from './schema.mjs';
5
3
  // ------------------------------------------------------------------
6
4
  // BaseNotImplemented
@@ -16,31 +14,8 @@ export class BaseNotImplemented extends Error {
16
14
  });
17
15
  }
18
16
  }
19
- // ------------------------------------------------------------------------------------
20
- // BaseValidator
21
- // ------------------------------------------------------------------------------------
22
- class BaseValidator {
23
- constructor(check, errors) {
24
- this.check = check;
25
- this.errors = errors;
26
- this.vendor = 'typebox';
27
- this.version = 1;
28
- this.validate = (value) => {
29
- return this.check(value)
30
- ? this.Success(value)
31
- : this.Failure(this.errors(value));
32
- };
33
- }
34
- Success(value) {
35
- return { value };
36
- }
37
- Failure(errors) {
38
- const issues = errors.reduce((result, error) => [...result, ...CreateIssues(error)], []);
39
- return { issues };
40
- }
41
- }
42
17
  // ------------------------------------------------------------------
43
- // Type
18
+ // Type.Base<...>
44
19
  // ------------------------------------------------------------------
45
20
  /** Base class for creating extension types. */
46
21
  export class Base {
@@ -86,54 +61,25 @@ export class Base {
86
61
  export function IsBase(value) {
87
62
  return IsKind(value, 'Base');
88
63
  }
89
- // --------------------------------------------------------
90
- // StandardSchema: PathSegments
91
- // --------------------------------------------------------
92
- function PathSegments(pointer) {
93
- if (Guard.IsEqual(pointer.length, 0))
94
- return [];
95
- return pointer.slice(1).split("/").map(segment => segment.replace(/~1/g, "/").replace(/~0/g, "~"));
96
- }
97
- // --------------------------------------------------------
98
- // IsStandardSchemaV1Error
99
- // --------------------------------------------------------
100
- function IsStandardSchemaV1Error(error) {
101
- return Guard.IsEqual(error.keyword, '~standard');
102
- }
103
- // --------------------------------------------------------
104
- // IssuesFromLocalizedError
105
- // --------------------------------------------------------
106
- function IssuesFromStandardSchemaV1Error(error) {
107
- const leading = PathSegments(error.instancePath);
108
- const issues = Guard.IsArray(error.params.issues) ? error.params.issues : [];
109
- return issues.map(issue => {
110
- const message = Guard.IsString(issue.message) ? issue.message : 'unknown';
111
- const path = Guard.IsArray(issue.path) ? [...leading, ...issue.path] : leading;
112
- return { message, path };
113
- });
114
- }
115
- function IssuesFromRegularError(error) {
116
- const path = PathSegments(error.instancePath);
117
- return [{ path, message: error.message }];
118
- }
119
- function IssuesFromLocalizedError(error) {
120
- return IsStandardSchemaV1Error(error)
121
- ? IssuesFromStandardSchemaV1Error(error)
122
- : IssuesFromRegularError(error);
123
- }
124
- // --------------------------------------------------------
125
- // IssuesFromUnknown
126
- // --------------------------------------------------------
127
- function IssuesFromUnknown(error) {
128
- const path = Guard.HasPropertyKey(error, 'path') && Guard.IsArray(error.path) && error.path.every(segment => Guard.IsString(segment)) ? error.path : [];
129
- const message = Guard.HasPropertyKey(error, 'message') && Guard.IsString(error.message) ? error.message : 'unknown';
130
- return [{ path, message }];
131
- }
132
- // --------------------------------------------------------
133
- // CreateIssues
134
- // --------------------------------------------------------
135
- function CreateIssues(error) {
136
- return IsLocalizedValidationError(error)
137
- ? IssuesFromLocalizedError(error)
138
- : IssuesFromUnknown(error);
64
+ // ------------------------------------------------------------------
65
+ // BaseValidator
66
+ // ------------------------------------------------------------------
67
+ class BaseValidator {
68
+ constructor(check, errors) {
69
+ this.check = check;
70
+ this.errors = errors;
71
+ this.vendor = 'typebox';
72
+ this.version = 1;
73
+ this.validate = (value) => {
74
+ return this.check(value)
75
+ ? this.Success(value)
76
+ : this.Failure(this.errors(value));
77
+ };
78
+ }
79
+ Success(value) {
80
+ return { value };
81
+ }
82
+ Failure(issues) {
83
+ return { issues };
84
+ }
139
85
  }
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.22",
4
+ "version": "1.0.23",
5
5
  "keywords": [
6
6
  "typescript",
7
7
  "jsonschema"
package/readme.md CHANGED
@@ -110,13 +110,13 @@ type T = Static<typeof T> // type T = {
110
110
 
111
111
  ## Script
112
112
 
113
- [Documentation](https://sinclairzx81.github.io/typebox/#/docs/script/overview) | [Example](https://www.typescriptlang.org/play/?#code/JYWwDg9gTgLgBAFQJ5gKYBo4G84xauAZRgEMZgBjOAXzgDMoIQ4ByPNAIwgA8WAoPhQgA7AM7wEcALyJ8AOkIUowMDAAUAAxx84cbgC44wgK4gOqKJh1wkhk2YtXdALzunzUOH2oaAlLoDAoOCQ0LCggHoIuCExCWlEAHkOACtUChgAHixrcLz8gsCogINEADl3R1zCmtqi6N1bcsrLarr2guKXQwQKhyg2jqHQ4uoAPgFY8SIE5DQFJRV1HElqTC1rAG0AaThgYTgAa1QkCDpEAF0enYu4AB8jYwAbJ+8-YY-g4qn4Qh7ktIZbKDT6fLp6HoAVWEwBEmU2vRamERLwuY3QINBw3BTQQ0NhwnhiP6yIqqPRmKxHXBrkQ+LhCL6HlJzyeaMpVLqowmfHYBEICWIZEomT5ZyIY05xT5MxkOU5WPBpXsHnujxeGIVoJxbn6apMGo5Ws6DTgtJVFn1rKNxryowEfCAA)
113
+ [Documentation](https://sinclairzx81.github.io/typebox/#/docs/script/overview) | [Example](https://www.typescriptlang.org/play/?moduleResolution=99&target=99&jsx=0&module=199#code/JYWwDg9gTgLgBAFQJ5gKYBo4G84xauAZRgEMZgBjOAXzgDMoIQ4ByPNAIwgA8WAoPhQgA7AM7wEcALyJ8AOkIUowMDAAUAAxx84cbgC44wgK4gOqKJh1wkhk2YtXdALzunzUOH2oaAlLoDAoOCQ0LCggHoIuCExCWlsa3DklNTAqID2VEMWCA4AK1QKGBZ0JLSKyvTo3ShUAEdjYDqAE0MAbRZeTBYkUtZnFgBdMqqxqozdMEY0WGBUUUMscvHVsMmAg2xcfBz7DxYaUbWT0I3dW22svfcLQ+pj06eA87hXK93WfbuaFeeTjbUP7-VYZIGCETiIgJZBoBRKFTqHCSB5wLTWdoAaTgwGEcAA1qgkBA6IghoYEFihnAAD5GYwAGwZ3j8IIB0ViUMICWWbKeG2urDyhWKpWBfMqGzqjWaqDacE63VYfR6gxG4olaQ20wgs3ICyWGs1KVeW20xtBNSCJGESAA8nQOkaLesrcEcIKWN8oPcnC6Jm6gh7Pl7GQz7s7-cFXrpqZGo9Ugg94wndK9LubU6kY3AbfbHQqU6mc7pg2gbg4fUcvFns4HAmXsl8wxHayb63A423kq9k93wq93pn+yEc3mHU6R2cO6WduXm5XfTWp9GZx956Gma2V5EO12d4nAn2Dy8rUCT6ffnwstCZMQyJQADxZElEAB8IIyN+5Ml5F7gGxbN6tL0kyjwnhslzAXSJhgUWCYbO80Ggcy-4AdE1BAA)
114
114
 
115
- TypeBox is a runtime type system that uses Json Schema as an AST for runtime type representation. The Script function provides a syntactic frontend to the type system and allows Json Schema to be created using native TypeScript syntax. TypeBox provides full static and runtime type safety for string-encoded types.
115
+ TypeBox is a type system designed to use Json Schema as an AST for runtime type representation. The Script function provides a full syntactic frontend to the type system and enables Json Schema to be constructed using native TypeScript syntax. TypeBox provides full static and runtime type safety for string-encoded types.
116
116
 
117
117
  ### Example
118
118
 
119
- The following uses Script to create and map types.
119
+ The following uses Script to construct and map Json Schema.
120
120
 
121
121
  ```typescript
122
122
  import Type, { type Static } from 'typebox'
@@ -125,19 +125,42 @@ const T = Type.Script(`{
125
125
  x: number,
126
126
  y: number,
127
127
  z: number
128
- }`) // const T = TObject<{
129
- // x: TNumber,
130
- // y: TNumber,
131
- // z: TNumber
132
- // }>
128
+ }`) // const T = {
129
+ // type: 'object',
130
+ // required: ['x', 'y', 'z'],
131
+ // properties: {
132
+ // x: { type: 'number' },
133
+ // y: { type: 'number' },
134
+ // z: { type: 'number' }
135
+ // }
136
+ // }
133
137
 
134
138
  const S = Type.Script({ T }, `{
135
139
  [K in keyof T]: T[K] | null
136
- }`) // const S: TObject<{
137
- // x: TUnion<[TNumber, TNull]>,
138
- // y: TUnion<[TNumber, TNull]>,
139
- // z: TUnion<[TNumber, TNull]>
140
- // }>
140
+ }`) // const S = {
141
+ // type: 'object',
142
+ // required: ['x', 'y', 'z'],
143
+ // properties: {
144
+ // x: {
145
+ // anyOf: [
146
+ // { type: 'number' },
147
+ // { type: 'null' }
148
+ // ]
149
+ // },
150
+ // y: {
151
+ // anyOf: [
152
+ // { type: 'number' },
153
+ // { type: 'null' }
154
+ // ]
155
+ // },
156
+ // z: {
157
+ // anyOf: [
158
+ // { type: 'number' },
159
+ // { type: 'null' }
160
+ // ]
161
+ // },
162
+ // }
163
+ // }
141
164
 
142
165
  type S = Static<typeof S> // type S = {
143
166
  // x: number | null,