@nmtjs/type 0.5.0 → 0.5.2
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/dist/compiler.js +5 -5
- package/dist/compiler.js.map +1 -1
- package/package.json +3 -3
- package/src/compiler.ts +5 -5
package/dist/compiler.js
CHANGED
|
@@ -96,23 +96,23 @@ export function compile(schema) {
|
|
|
96
96
|
export var runtime;
|
|
97
97
|
(function(runtime) {
|
|
98
98
|
function parse(type, value) {
|
|
99
|
-
return _parse(type.
|
|
99
|
+
return _parse(type.final, value);
|
|
100
100
|
}
|
|
101
101
|
runtime.parse = parse;
|
|
102
102
|
function errors(type, value) {
|
|
103
|
-
return _errors(Value.Errors(type.
|
|
103
|
+
return _errors(Value.Errors(type.final, value));
|
|
104
104
|
}
|
|
105
105
|
runtime.errors = errors;
|
|
106
106
|
function check(type, value) {
|
|
107
|
-
return Value.Check(type.
|
|
107
|
+
return Value.Check(type.final, value);
|
|
108
108
|
}
|
|
109
109
|
runtime.check = check;
|
|
110
110
|
function decode(type, value) {
|
|
111
|
-
return TransformDecode(type.
|
|
111
|
+
return TransformDecode(type.final, [], value);
|
|
112
112
|
}
|
|
113
113
|
runtime.decode = decode;
|
|
114
114
|
function encode(type, value) {
|
|
115
|
-
return TransformEncode(type.
|
|
115
|
+
return TransformEncode(type.final, [], value);
|
|
116
116
|
}
|
|
117
117
|
runtime.encode = encode;
|
|
118
118
|
})(runtime || (runtime = {}));
|
package/dist/compiler.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/compiler.ts"],"sourcesContent":["import type { TSchema } from '@sinclair/typebox'\nimport {\n TypeCompiler,\n type ValueErrorIterator,\n} from '@sinclair/typebox/compiler'\nimport {\n TransformDecode,\n TransformEncode,\n Value,\n} from '@sinclair/typebox/value'\nimport type { t } from './index.ts'\nimport { IsDiscriminatedUnion } from './schemas/discriminated-union.ts'\nimport type { BaseType } from './types/base.ts'\n\ntype ValidationError = {\n path: string\n message: string\n value: unknown\n}\n\nexport type Compiled<T extends BaseType = BaseType> = {\n check: (val: unknown) => boolean\n errors: (val: unknown) => ValidationError[]\n parse: (val: unknown) => unknown\n /**\n * Requires to `check` before calling\n */\n decode: (val: unknown) => t.infer.decoded<T>\n /**\n * Requires to `check` before calling\n */\n encode: (val: unknown) => t.infer.encoded<T>\n /**\n * Requires to `check` before calling\n */\n decodeSafe: (\n val: unknown,\n ) =>\n | { success: true; value: t.infer.decoded<T> }\n | { success: false; error: any }\n /**\n * Requires to `check` before calling\n */\n encodeSafe: (\n val: unknown,\n ) =>\n | { success: true; value: t.infer.encoded<T> }\n | { success: false; error: any }\n}\n\n// FIXME: this one is very slow\nfunction _parse(schema: TSchema, value: any) {\n // Clone -> Clean -> Default -> Convert\n return Value.Convert(\n schema,\n Value.Default(schema, Value.Clean(schema, Value.Clone(value))),\n )\n}\n\nfunction _errors(errors: ValueErrorIterator) {\n const result: ValidationError[] = []\n\n for (const error of errors) {\n if (IsDiscriminatedUnion(error.schema)) {\n const discriminator = error.schema.discriminator\n const discriminatorValue = error.value?.[discriminator]\n if (discriminatorValue !== undefined) {\n const variantSchema = error.schema.anyOf.find(\n (schema) =>\n schema.properties[discriminator].const === discriminatorValue,\n )\n if (variantSchema) {\n const propertiesSchemas: TSchema[] = []\n for (const element in variantSchema.properties) {\n const propertySchema = variantSchema.properties[element]\n if (propertySchema !== variantSchema.properties[discriminator]) {\n propertiesSchemas.push(propertySchema)\n }\n }\n\n for (const iter of error.errors) {\n for (const err of iter) {\n if (!propertiesSchemas.includes(err.schema)) continue\n result.push({\n path: err.path,\n message: err.message,\n value: err.value,\n })\n }\n }\n\n continue\n }\n }\n }\n\n result.push({\n path: error.path,\n message: error.message,\n value: error.value,\n })\n\n for (const nestedError of error.errors) {\n result.push(..._errors(nestedError))\n }\n }\n\n return result\n}\n\nfunction compileType(type: BaseType) {\n const { final: schema } = type\n const compiled = TypeCompiler.Compile(schema)\n const errors = (value) => {\n return _errors(compiled.Errors(value))\n }\n\n return {\n check: compiled.Check.bind(compiled),\n parse: _parse.bind(null, schema),\n errors,\n decode: TransformDecode.bind(null, schema, compiled.References()),\n encode: TransformEncode.bind(null, schema, compiled.References()),\n }\n}\n\nexport function compile<T extends BaseType>(schema: T): Compiled<T> {\n const compiled = compileType(schema)\n\n function decodeSafe(val: unknown) {\n try {\n return {\n success: true as const,\n value: compiled.decode(val),\n }\n } catch (error) {\n return { success: false as const, error }\n }\n }\n\n function encodeSafe(val: unknown) {\n try {\n return {\n success: true as const,\n value: compiled.encode(val),\n }\n } catch (error) {\n return { success: false as const, error }\n }\n }\n\n return {\n ...compiled,\n decodeSafe,\n encodeSafe,\n } as any\n}\n\nexport namespace runtime {\n export function parse(type: BaseType, value: any) {\n return _parse(type.
|
|
1
|
+
{"version":3,"sources":["../../src/compiler.ts"],"sourcesContent":["import type { TSchema } from '@sinclair/typebox'\nimport {\n TypeCompiler,\n type ValueErrorIterator,\n} from '@sinclair/typebox/compiler'\nimport {\n TransformDecode,\n TransformEncode,\n Value,\n} from '@sinclair/typebox/value'\nimport type { t } from './index.ts'\nimport { IsDiscriminatedUnion } from './schemas/discriminated-union.ts'\nimport type { BaseType } from './types/base.ts'\n\ntype ValidationError = {\n path: string\n message: string\n value: unknown\n}\n\nexport type Compiled<T extends BaseType = BaseType> = {\n check: (val: unknown) => boolean\n errors: (val: unknown) => ValidationError[]\n parse: (val: unknown) => unknown\n /**\n * Requires to `check` before calling\n */\n decode: (val: unknown) => t.infer.decoded<T>\n /**\n * Requires to `check` before calling\n */\n encode: (val: unknown) => t.infer.encoded<T>\n /**\n * Requires to `check` before calling\n */\n decodeSafe: (\n val: unknown,\n ) =>\n | { success: true; value: t.infer.decoded<T> }\n | { success: false; error: any }\n /**\n * Requires to `check` before calling\n */\n encodeSafe: (\n val: unknown,\n ) =>\n | { success: true; value: t.infer.encoded<T> }\n | { success: false; error: any }\n}\n\n// FIXME: this one is very slow\nfunction _parse(schema: TSchema, value: any) {\n // Clone -> Clean -> Default -> Convert\n return Value.Convert(\n schema,\n Value.Default(schema, Value.Clean(schema, Value.Clone(value))),\n )\n}\n\nfunction _errors(errors: ValueErrorIterator) {\n const result: ValidationError[] = []\n\n for (const error of errors) {\n if (IsDiscriminatedUnion(error.schema)) {\n const discriminator = error.schema.discriminator\n const discriminatorValue = error.value?.[discriminator]\n if (discriminatorValue !== undefined) {\n const variantSchema = error.schema.anyOf.find(\n (schema) =>\n schema.properties[discriminator].const === discriminatorValue,\n )\n if (variantSchema) {\n const propertiesSchemas: TSchema[] = []\n for (const element in variantSchema.properties) {\n const propertySchema = variantSchema.properties[element]\n if (propertySchema !== variantSchema.properties[discriminator]) {\n propertiesSchemas.push(propertySchema)\n }\n }\n\n for (const iter of error.errors) {\n for (const err of iter) {\n if (!propertiesSchemas.includes(err.schema)) continue\n result.push({\n path: err.path,\n message: err.message,\n value: err.value,\n })\n }\n }\n\n continue\n }\n }\n }\n\n result.push({\n path: error.path,\n message: error.message,\n value: error.value,\n })\n\n for (const nestedError of error.errors) {\n result.push(..._errors(nestedError))\n }\n }\n\n return result\n}\n\nfunction compileType(type: BaseType) {\n const { final: schema } = type\n const compiled = TypeCompiler.Compile(schema)\n const errors = (value) => {\n return _errors(compiled.Errors(value))\n }\n\n return {\n check: compiled.Check.bind(compiled),\n parse: _parse.bind(null, schema),\n errors,\n decode: TransformDecode.bind(null, schema, compiled.References()),\n encode: TransformEncode.bind(null, schema, compiled.References()),\n }\n}\n\nexport function compile<T extends BaseType>(schema: T): Compiled<T> {\n const compiled = compileType(schema)\n\n function decodeSafe(val: unknown) {\n try {\n return {\n success: true as const,\n value: compiled.decode(val),\n }\n } catch (error) {\n return { success: false as const, error }\n }\n }\n\n function encodeSafe(val: unknown) {\n try {\n return {\n success: true as const,\n value: compiled.encode(val),\n }\n } catch (error) {\n return { success: false as const, error }\n }\n }\n\n return {\n ...compiled,\n decodeSafe,\n encodeSafe,\n } as any\n}\n\nexport namespace runtime {\n export function parse(type: BaseType, value: any) {\n return _parse(type.final, value)\n }\n\n export function errors(type: BaseType, value: any): ValidationError[] {\n return _errors(Value.Errors(type.final, value))\n }\n\n export function check(type: BaseType, value: any): boolean {\n return Value.Check(type.final, value)\n }\n\n export function decode<T extends BaseType>(\n type: T,\n value: any,\n ): t.infer.decoded<T> {\n return TransformDecode(type.final, [], value)\n }\n\n export function encode<T extends BaseType>(\n type: T,\n value: any,\n ): t.infer.encoded<T> {\n return TransformEncode(type.final, [], value)\n }\n}\n"],"names":["TypeCompiler","TransformDecode","TransformEncode","Value","IsDiscriminatedUnion","_parse","schema","value","Convert","Default","Clean","Clone","_errors","errors","result","error","discriminator","discriminatorValue","undefined","variantSchema","anyOf","find","properties","const","propertiesSchemas","element","propertySchema","push","iter","err","includes","path","message","nestedError","compileType","type","final","compiled","Compile","Errors","check","Check","bind","parse","decode","References","encode","compile","decodeSafe","val","success","encodeSafe","runtime"],"mappings":"AACA,SACEA,YAAY,QAEP,6BAA4B;AACnC,SACEC,eAAe,EACfC,eAAe,EACfC,KAAK,QACA,0BAAyB;AAEhC,SAASC,oBAAoB,QAAQ,mCAAkC;AAwCvE,SAASC,OAAOC,MAAe,EAAEC,KAAU;IAEzC,OAAOJ,MAAMK,OAAO,CAClBF,QACAH,MAAMM,OAAO,CAACH,QAAQH,MAAMO,KAAK,CAACJ,QAAQH,MAAMQ,KAAK,CAACJ;AAE1D;AAEA,SAASK,QAAQC,MAA0B;IACzC,MAAMC,SAA4B,EAAE;IAEpC,KAAK,MAAMC,SAASF,OAAQ;QAC1B,IAAIT,qBAAqBW,MAAMT,MAAM,GAAG;YACtC,MAAMU,gBAAgBD,MAAMT,MAAM,CAACU,aAAa;YAChD,MAAMC,qBAAqBF,MAAMR,KAAK,EAAE,CAACS,cAAc;YACvD,IAAIC,uBAAuBC,WAAW;gBACpC,MAAMC,gBAAgBJ,MAAMT,MAAM,CAACc,KAAK,CAACC,IAAI,CAC3C,CAACf,SACCA,OAAOgB,UAAU,CA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sinclair/typebox": "^0.34.13",
|
|
23
23
|
"temporal-polyfill": "^0.2.5",
|
|
24
|
-
"@nmtjs/common": "0.5.
|
|
24
|
+
"@nmtjs/common": "0.5.2"
|
|
25
25
|
},
|
|
26
26
|
"devDependencies": {
|
|
27
27
|
"@sinclair/typebox": "^0.34.13",
|
|
28
28
|
"temporal-polyfill": "^0.2.5",
|
|
29
|
-
"@nmtjs/common": "0.5.
|
|
29
|
+
"@nmtjs/common": "0.5.2"
|
|
30
30
|
},
|
|
31
31
|
"files": [
|
|
32
32
|
"src",
|
|
@@ -35,7 +35,7 @@
|
|
|
35
35
|
"LICENSE.md",
|
|
36
36
|
"README.md"
|
|
37
37
|
],
|
|
38
|
-
"version": "0.5.
|
|
38
|
+
"version": "0.5.2",
|
|
39
39
|
"scripts": {
|
|
40
40
|
"build": "neemata-build -p neutral --root=./src './**/*.ts'",
|
|
41
41
|
"type-check": "tsc --noEmit"
|
package/src/compiler.ts
CHANGED
|
@@ -158,28 +158,28 @@ export function compile<T extends BaseType>(schema: T): Compiled<T> {
|
|
|
158
158
|
|
|
159
159
|
export namespace runtime {
|
|
160
160
|
export function parse(type: BaseType, value: any) {
|
|
161
|
-
return _parse(type.
|
|
161
|
+
return _parse(type.final, value)
|
|
162
162
|
}
|
|
163
163
|
|
|
164
164
|
export function errors(type: BaseType, value: any): ValidationError[] {
|
|
165
|
-
return _errors(Value.Errors(type.
|
|
165
|
+
return _errors(Value.Errors(type.final, value))
|
|
166
166
|
}
|
|
167
167
|
|
|
168
168
|
export function check(type: BaseType, value: any): boolean {
|
|
169
|
-
return Value.Check(type.
|
|
169
|
+
return Value.Check(type.final, value)
|
|
170
170
|
}
|
|
171
171
|
|
|
172
172
|
export function decode<T extends BaseType>(
|
|
173
173
|
type: T,
|
|
174
174
|
value: any,
|
|
175
175
|
): t.infer.decoded<T> {
|
|
176
|
-
return TransformDecode(type.
|
|
176
|
+
return TransformDecode(type.final, [], value)
|
|
177
177
|
}
|
|
178
178
|
|
|
179
179
|
export function encode<T extends BaseType>(
|
|
180
180
|
type: T,
|
|
181
181
|
value: any,
|
|
182
182
|
): t.infer.encoded<T> {
|
|
183
|
-
return TransformEncode(type.
|
|
183
|
+
return TransformEncode(type.final, [], value)
|
|
184
184
|
}
|
|
185
185
|
}
|