@forklaunch/validator 0.3.13 → 0.4.1
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/lib/index.d.mts +17 -0
- package/lib/index.d.ts +17 -2
- package/lib/index.js +44 -1
- package/lib/index.mjs +17 -0
- package/lib/schema.types-Do9dhcdB.d.mts +476 -0
- package/lib/schema.types-Do9dhcdB.d.ts +476 -0
- package/lib/src/typebox/index.d.mts +213 -0
- package/lib/src/typebox/index.d.ts +213 -3
- package/lib/src/typebox/index.js +435 -1
- package/lib/src/typebox/index.mjs +393 -0
- package/lib/src/zod/index.d.mts +106 -0
- package/lib/src/zod/index.d.ts +106 -3
- package/lib/src/zod/index.js +289 -1
- package/lib/src/zod/index.mjs +242 -0
- package/lib/tests/utils/mockSchemaValidator.d.mts +69 -0
- package/lib/tests/utils/mockSchemaValidator.d.ts +29 -25
- package/lib/tests/utils/mockSchemaValidator.js +143 -79
- package/lib/tests/utils/mockSchemaValidator.mjs +99 -0
- package/package.json +48 -35
- package/lib/index.d.ts.map +0 -1
- package/lib/src/shared/types/schema.types.d.ts +0 -255
- package/lib/src/shared/types/schema.types.d.ts.map +0 -1
- package/lib/src/shared/types/schema.types.js +0 -1
- package/lib/src/typebox/index.d.ts.map +0 -1
- package/lib/src/typebox/staticSchemaValidator.d.ts +0 -99
- package/lib/src/typebox/staticSchemaValidator.d.ts.map +0 -1
- package/lib/src/typebox/staticSchemaValidator.js +0 -99
- package/lib/src/typebox/typeboxSchemaValidator.d.ts +0 -122
- package/lib/src/typebox/typeboxSchemaValidator.d.ts.map +0 -1
- package/lib/src/typebox/typeboxSchemaValidator.js +0 -362
- package/lib/src/typebox/types/schema.types.d.ts +0 -59
- package/lib/src/typebox/types/schema.types.d.ts.map +0 -1
- package/lib/src/typebox/types/schema.types.js +0 -1
- package/lib/src/zod/index.d.ts.map +0 -1
- package/lib/src/zod/staticSchemaValidator.d.ts +0 -99
- package/lib/src/zod/staticSchemaValidator.d.ts.map +0 -1
- package/lib/src/zod/staticSchemaValidator.js +0 -99
- package/lib/src/zod/types/schema.types.d.ts +0 -71
- package/lib/src/zod/types/schema.types.d.ts.map +0 -1
- package/lib/src/zod/types/schema.types.js +0 -1
- package/lib/src/zod/zodSchemaValidator.d.ts +0 -110
- package/lib/src/zod/zodSchemaValidator.d.ts.map +0 -1
- package/lib/src/zod/zodSchemaValidator.js +0 -192
- package/lib/tests/typebox/advancedParse.test.d.ts +0 -2
- package/lib/tests/typebox/advancedParse.test.d.ts.map +0 -1
- package/lib/tests/typebox/advancedParse.test.js +0 -514
- package/lib/tests/typebox/largeSchema.test.d.ts +0 -2
- package/lib/tests/typebox/largeSchema.test.d.ts.map +0 -1
- package/lib/tests/typebox/largeSchema.test.js +0 -150
- package/lib/tests/typebox/schemaValidator.test.d.ts +0 -2
- package/lib/tests/typebox/schemaValidator.test.d.ts.map +0 -1
- package/lib/tests/typebox/schemaValidator.test.js +0 -253
- package/lib/tests/typebox/typeEquality.test.d.ts +0 -2
- package/lib/tests/typebox/typeEquality.test.d.ts.map +0 -1
- package/lib/tests/typebox/typeEquality.test.js +0 -100
- package/lib/tests/utils/compare.d.ts +0 -2
- package/lib/tests/utils/compare.d.ts.map +0 -1
- package/lib/tests/utils/compare.js +0 -8
- package/lib/tests/utils/mockSchemaValidator.d.ts.map +0 -1
- package/lib/tests/zod/advancedParse.test.d.ts +0 -2
- package/lib/tests/zod/advancedParse.test.d.ts.map +0 -1
- package/lib/tests/zod/advancedParse.test.js +0 -514
- package/lib/tests/zod/largeSchema.test.d.ts +0 -2
- package/lib/tests/zod/largeSchema.test.d.ts.map +0 -1
- package/lib/tests/zod/largeSchema.test.js +0 -150
- package/lib/tests/zod/schemaValidator.test.d.ts +0 -2
- package/lib/tests/zod/schemaValidator.test.d.ts.map +0 -1
- package/lib/tests/zod/schemaValidator.test.js +0 -205
- package/lib/tests/zod/typeEquality.test.d.ts +0 -2
- package/lib/tests/zod/typeEquality.test.d.ts.map +0 -1
- package/lib/tests/zod/typeEquality.test.js +0 -100
- package/lib/vitest.config.d.ts +0 -3
- package/lib/vitest.config.d.ts.map +0 -1
- package/lib/vitest.config.js +0 -7
|
@@ -1,3 +1,213 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
import * as openapi3_ts_oas31 from 'openapi3-ts/oas31';
|
|
2
|
+
import { SchemaObject } from 'openapi3-ts/oas31';
|
|
3
|
+
import * as _sinclair_typebox_compiler from '@sinclair/typebox/compiler';
|
|
4
|
+
import { TypeCheck } from '@sinclair/typebox/compiler';
|
|
5
|
+
import { S as SchemaValidator$1, T as TObject, f as TIdiomaticSchema, g as TResolve, h as TUnionContainer, i as UnionTResolve, L as LiteralSchema, a as ParseResult } from '../../schema.types-Do9dhcdB.js';
|
|
6
|
+
import * as _sinclair_typebox from '@sinclair/typebox';
|
|
7
|
+
import { TProperties, TOptional, TArray, TUnion, TLiteral, TSchema, TKind } from '@sinclair/typebox';
|
|
8
|
+
import '@forklaunch/common';
|
|
9
|
+
import 'zod';
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* Class representing a TypeBox schema definition.
|
|
13
|
+
* @implements {SchemaValidator}
|
|
14
|
+
*/
|
|
15
|
+
declare class TypeboxSchemaValidator implements SchemaValidator$1<(<T extends TObject<TProperties>>(schema: T) => TypeCheck<T>), <T extends TIdiomaticSchema>(schema: T) => TResolve<T>, <T extends TIdiomaticSchema>(schema: T) => TOptional<TResolve<T>>, <T extends TIdiomaticSchema>(schema: T) => TArray<TResolve<T>>, <T extends TUnionContainer>(schemas: [...T]) => TUnion<UnionTResolve<T>>, <T extends LiteralSchema>(value: T) => TLiteral<T>, <T extends LiteralSchema>(schemaEnum: Record<string, T>) => TUnion<UnionTResolve<T[]>>, (value: unknown) => value is TSchema, <T extends TIdiomaticSchema>(schema: T, value: unknown) => boolean, <T extends TIdiomaticSchema>(schema: T, value: unknown) => ParseResult<TResolve<T>>, <T extends TIdiomaticSchema>(schema: T) => SchemaObject> {
|
|
16
|
+
_Type: 'TypeBox';
|
|
17
|
+
_SchemaCatchall: TKind;
|
|
18
|
+
_ValidSchemaObject: TObject<TProperties> | TArray<TObject<TProperties>>;
|
|
19
|
+
string: _sinclair_typebox.TString;
|
|
20
|
+
uuid: _sinclair_typebox.TString;
|
|
21
|
+
uri: _sinclair_typebox.TString;
|
|
22
|
+
email: _sinclair_typebox.TString;
|
|
23
|
+
number: _sinclair_typebox.TTransform<TUnion<[_sinclair_typebox.TNumber, _sinclair_typebox.TString, _sinclair_typebox.TBoolean, _sinclair_typebox.TNull, _sinclair_typebox.TDate, _sinclair_typebox.TBigInt]>, number>;
|
|
24
|
+
bigint: _sinclair_typebox.TTransform<TUnion<[_sinclair_typebox.TBigInt, _sinclair_typebox.TNumber, _sinclair_typebox.TString, _sinclair_typebox.TBoolean, _sinclair_typebox.TDate]>, bigint>;
|
|
25
|
+
boolean: _sinclair_typebox.TTransform<TUnion<[_sinclair_typebox.TBoolean, _sinclair_typebox.TString]>, boolean | undefined>;
|
|
26
|
+
date: _sinclair_typebox.TTransform<TUnion<[_sinclair_typebox.TDate, _sinclair_typebox.TNumber, _sinclair_typebox.TString, _sinclair_typebox.TBoolean, _sinclair_typebox.TNull]>, Date>;
|
|
27
|
+
symbol: _sinclair_typebox.TSymbol;
|
|
28
|
+
nullish: TUnion<[_sinclair_typebox.TVoid, _sinclair_typebox.TNull, _sinclair_typebox.TUndefined]>;
|
|
29
|
+
any: _sinclair_typebox.TAny;
|
|
30
|
+
unknown: _sinclair_typebox.TUnknown;
|
|
31
|
+
never: _sinclair_typebox.TNever;
|
|
32
|
+
/**
|
|
33
|
+
* Extracts the error type of a schema for error messages.
|
|
34
|
+
*
|
|
35
|
+
* @param {TCatchall} schema - A schema that contains some type information.
|
|
36
|
+
* @returns The type of the schema for error messages.
|
|
37
|
+
*/
|
|
38
|
+
private errorType;
|
|
39
|
+
/**
|
|
40
|
+
* Compiles schema if this exists, for optimal performance.
|
|
41
|
+
*
|
|
42
|
+
* @param {TObject<TProperties>} schema - The schema to compile.
|
|
43
|
+
* @returns {TypeCheck<T>} - The compiled schema.
|
|
44
|
+
*/
|
|
45
|
+
compile<T extends TObject<TProperties>>(schema: T): TypeCheck<T>;
|
|
46
|
+
/**
|
|
47
|
+
* Convert a schema to a TypeBox schema.
|
|
48
|
+
* @param {TIdiomaticSchema} schema - The schema to convert.
|
|
49
|
+
* @returns {TResolve<T>} The resolved schema.
|
|
50
|
+
*/
|
|
51
|
+
schemify<T extends TIdiomaticSchema>(schema: T): TResolve<T>;
|
|
52
|
+
/**
|
|
53
|
+
* Make a schema optional.
|
|
54
|
+
* @param {TIdiomaticSchema} schema - The schema to make optional.
|
|
55
|
+
* @returns {TOptional<TResolve<T>>} The optional schema.
|
|
56
|
+
*/
|
|
57
|
+
optional<T extends TIdiomaticSchema>(schema: T): TOptional<TResolve<T>>;
|
|
58
|
+
/**
|
|
59
|
+
* Create an array schema.
|
|
60
|
+
* @param {TIdiomaticSchema} schema - The schema to use for array items.
|
|
61
|
+
* @returns {TArray<TResolve<T>>} The array schema.
|
|
62
|
+
*/
|
|
63
|
+
array<T extends TIdiomaticSchema>(schema: T): TArray<TResolve<T>>;
|
|
64
|
+
/**
|
|
65
|
+
* Create a union schema.
|
|
66
|
+
* @param {TUnionContainer} schemas - The schemas to union.
|
|
67
|
+
* @returns {TUnion<UnionTResolve<T>>} The union schema.
|
|
68
|
+
*
|
|
69
|
+
* WARNING: If "nullish" or TUndefined is included in the union, the key will still be expected.
|
|
70
|
+
* This is a limitation of TypeBox. Consider using "optional" instead.
|
|
71
|
+
*/
|
|
72
|
+
union<T extends TUnionContainer>(schemas: [...T]): TUnion<UnionTResolve<T>>;
|
|
73
|
+
/**
|
|
74
|
+
* Create a literal schema.
|
|
75
|
+
* @param {LiteralSchema} value - The literal value.
|
|
76
|
+
* @returns {TLiteral<T>} The literal schema.
|
|
77
|
+
*/
|
|
78
|
+
literal<T extends LiteralSchema>(value: T): TLiteral<T>;
|
|
79
|
+
/**
|
|
80
|
+
* Create an enum schema.
|
|
81
|
+
* @param {Record<string, LiteralSchema>} schemaEnum - The enum schema.
|
|
82
|
+
* @returns {TUnion<UnionTResolve<T[]>>} The enum schema.
|
|
83
|
+
*/
|
|
84
|
+
enum_<T extends LiteralSchema>(schemaEnum: Record<string, T>): TUnion<UnionTResolve<T[]>>;
|
|
85
|
+
/**
|
|
86
|
+
* Check if a value is a TypeBox object schema.
|
|
87
|
+
* @param {unknown} value - The value to check.
|
|
88
|
+
* @returns {boolean} True if the value is a TypeBox object schema.
|
|
89
|
+
*/
|
|
90
|
+
isSchema(value: unknown): value is TSchema;
|
|
91
|
+
/**
|
|
92
|
+
* Validate a value against a schema.
|
|
93
|
+
*
|
|
94
|
+
* @param {TSchema} schema - The schema to validate against.
|
|
95
|
+
* @param {unknown} value - The value to validate.
|
|
96
|
+
* @returns {boolean} True if valid, otherwise false.
|
|
97
|
+
*/
|
|
98
|
+
validate<T extends TIdiomaticSchema | TSchema>(schema: T | TypeCheck<TResolve<T>>, value: unknown): boolean;
|
|
99
|
+
/**
|
|
100
|
+
* Parse a value against a schema.
|
|
101
|
+
*
|
|
102
|
+
* @param {TSchema} schema - The schema to validate against.
|
|
103
|
+
* @param {unknown} value - The value to validate.
|
|
104
|
+
* @returns {ParseResult<TResolve<T>>} The parsing result.
|
|
105
|
+
*/
|
|
106
|
+
parse<T extends TIdiomaticSchema | TSchema>(schema: T | TypeCheck<TResolve<T>>, value: unknown): ParseResult<TResolve<T>>;
|
|
107
|
+
/**
|
|
108
|
+
* Convert a schema to an OpenAPI schema object.
|
|
109
|
+
* @param {TIdiomaticSchema | TSchema} schema - The schema to convert.
|
|
110
|
+
* @returns {SchemaObject} The OpenAPI schema object.
|
|
111
|
+
*/
|
|
112
|
+
openapi<T extends TIdiomaticSchema | TSchema>(schema: T): SchemaObject;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Factory function for creating a TypeboxSchemaValidator instance.
|
|
117
|
+
* @returns {TypeboxSchemaValidator} The TypeboxSchemaValidator instance.
|
|
118
|
+
*/
|
|
119
|
+
declare const SchemaValidator: () => TypeboxSchemaValidator;
|
|
120
|
+
/**
|
|
121
|
+
* TypeBox schema definition for string type.
|
|
122
|
+
*/
|
|
123
|
+
declare const string: _sinclair_typebox.TString;
|
|
124
|
+
/**
|
|
125
|
+
* TypeBox schema definition for UUID type.
|
|
126
|
+
*/
|
|
127
|
+
declare const uuid: _sinclair_typebox.TString;
|
|
128
|
+
/**
|
|
129
|
+
* TypeBox schema definition for URI type.
|
|
130
|
+
*/
|
|
131
|
+
declare const uri: _sinclair_typebox.TString;
|
|
132
|
+
/**
|
|
133
|
+
* TypeBox schema definition for email type.
|
|
134
|
+
*/
|
|
135
|
+
declare const email: _sinclair_typebox.TString;
|
|
136
|
+
/**
|
|
137
|
+
* TypeBox schema definition for number type.
|
|
138
|
+
*/
|
|
139
|
+
declare const number: _sinclair_typebox.TTransform<_sinclair_typebox.TUnion<[_sinclair_typebox.TNumber, _sinclair_typebox.TString, _sinclair_typebox.TBoolean, _sinclair_typebox.TNull, _sinclair_typebox.TDate, _sinclair_typebox.TBigInt]>, number>;
|
|
140
|
+
/**
|
|
141
|
+
* TypeBox schema definition for bigint type.
|
|
142
|
+
*/
|
|
143
|
+
declare const bigint: _sinclair_typebox.TTransform<_sinclair_typebox.TUnion<[_sinclair_typebox.TBigInt, _sinclair_typebox.TNumber, _sinclair_typebox.TString, _sinclair_typebox.TBoolean, _sinclair_typebox.TDate]>, bigint>;
|
|
144
|
+
/**
|
|
145
|
+
* TypeBox schema definition for boolean type.
|
|
146
|
+
*/
|
|
147
|
+
declare const boolean: _sinclair_typebox.TTransform<_sinclair_typebox.TUnion<[_sinclair_typebox.TBoolean, _sinclair_typebox.TString]>, boolean | undefined>;
|
|
148
|
+
/**
|
|
149
|
+
* TypeBox schema definition for date type.
|
|
150
|
+
*/
|
|
151
|
+
declare const date: _sinclair_typebox.TTransform<_sinclair_typebox.TUnion<[_sinclair_typebox.TDate, _sinclair_typebox.TNumber, _sinclair_typebox.TString, _sinclair_typebox.TBoolean, _sinclair_typebox.TNull]>, Date>;
|
|
152
|
+
/**
|
|
153
|
+
* TypeBox schema definition for symbol type.
|
|
154
|
+
*/
|
|
155
|
+
declare const symbol: _sinclair_typebox.TSymbol;
|
|
156
|
+
/**
|
|
157
|
+
* TypeBox schema definition for undefined, null, void types.
|
|
158
|
+
*/
|
|
159
|
+
declare const nullish: _sinclair_typebox.TUnion<[_sinclair_typebox.TVoid, _sinclair_typebox.TNull, _sinclair_typebox.TUndefined]>;
|
|
160
|
+
/**
|
|
161
|
+
* TypeBox schema definition for any type.
|
|
162
|
+
*/
|
|
163
|
+
declare const any: _sinclair_typebox.TAny;
|
|
164
|
+
/**
|
|
165
|
+
* TypeBox schema definition for unknown type.
|
|
166
|
+
*/
|
|
167
|
+
declare const unknown: _sinclair_typebox.TUnknown;
|
|
168
|
+
/**
|
|
169
|
+
* TypeBox schema definition for never type.
|
|
170
|
+
*/
|
|
171
|
+
declare const never: _sinclair_typebox.TNever;
|
|
172
|
+
/**
|
|
173
|
+
* Transforms valid schema into TypeBox schema.
|
|
174
|
+
*/
|
|
175
|
+
declare const schemify: <T extends TIdiomaticSchema>(schema: T) => TResolve<T>;
|
|
176
|
+
/**
|
|
177
|
+
* Makes a valid schema optional.
|
|
178
|
+
*/
|
|
179
|
+
declare const optional: <T extends TIdiomaticSchema>(schema: T) => _sinclair_typebox.TOptional<TResolve<T>>;
|
|
180
|
+
/**
|
|
181
|
+
* Defines an array for a valid schema.
|
|
182
|
+
*/
|
|
183
|
+
declare const array: <T extends TIdiomaticSchema>(schema: T) => _sinclair_typebox.TArray<TResolve<T>>;
|
|
184
|
+
/**
|
|
185
|
+
* Defines a union for a valid schema.
|
|
186
|
+
*/
|
|
187
|
+
declare const union: <T extends TUnionContainer>(schemas: [...T]) => _sinclair_typebox.TUnion<UnionTResolve<T>>;
|
|
188
|
+
/**
|
|
189
|
+
* Defines a literal for a valid schema.
|
|
190
|
+
*/
|
|
191
|
+
declare const literal: <T extends LiteralSchema>(value: T) => _sinclair_typebox.TLiteral<T>;
|
|
192
|
+
/**
|
|
193
|
+
* Defines an enum for a valid schema.
|
|
194
|
+
*/
|
|
195
|
+
declare const enum_: <T extends LiteralSchema>(schemaEnum: Record<string, T>) => _sinclair_typebox.TUnion<UnionTResolve<T[]>>;
|
|
196
|
+
/**
|
|
197
|
+
* Checks if a value is a TypeBox schema.
|
|
198
|
+
*/
|
|
199
|
+
declare const isSchema: (value: unknown) => value is _sinclair_typebox.TSchema;
|
|
200
|
+
/**
|
|
201
|
+
* Validates a value against a valid schema.
|
|
202
|
+
*/
|
|
203
|
+
declare const validate: <T extends TIdiomaticSchema | _sinclair_typebox.TSchema>(schema: T | _sinclair_typebox_compiler.TypeCheck<TResolve<T>>, value: unknown) => boolean;
|
|
204
|
+
/**
|
|
205
|
+
* Parses a value against a valid schema.
|
|
206
|
+
*/
|
|
207
|
+
declare const parse: <T extends TIdiomaticSchema | _sinclair_typebox.TSchema>(schema: T | _sinclair_typebox_compiler.TypeCheck<TResolve<T>>, value: unknown) => ParseResult<TResolve<T>>;
|
|
208
|
+
/**
|
|
209
|
+
* Generates an OpenAPI schema object from a valid schema.
|
|
210
|
+
*/
|
|
211
|
+
declare const openapi: <T extends TIdiomaticSchema | _sinclair_typebox.TSchema>(schema: T) => openapi3_ts_oas31.SchemaObject;
|
|
212
|
+
|
|
213
|
+
export { SchemaValidator, TypeboxSchemaValidator, any, array, bigint, boolean, date, email, enum_, isSchema, literal, never, nullish, number, openapi, optional, parse, schemify, string, symbol, union, unknown, uri, uuid, validate };
|
package/lib/src/typebox/index.js
CHANGED
|
@@ -1 +1,435 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
|
|
20
|
+
// src/typebox/index.ts
|
|
21
|
+
var typebox_exports = {};
|
|
22
|
+
__export(typebox_exports, {
|
|
23
|
+
SchemaValidator: () => SchemaValidator,
|
|
24
|
+
any: () => any,
|
|
25
|
+
array: () => array,
|
|
26
|
+
bigint: () => bigint,
|
|
27
|
+
boolean: () => boolean,
|
|
28
|
+
date: () => date,
|
|
29
|
+
email: () => email,
|
|
30
|
+
enum_: () => enum_,
|
|
31
|
+
isSchema: () => isSchema,
|
|
32
|
+
literal: () => literal,
|
|
33
|
+
never: () => never,
|
|
34
|
+
nullish: () => nullish,
|
|
35
|
+
number: () => number,
|
|
36
|
+
openapi: () => openapi,
|
|
37
|
+
optional: () => optional,
|
|
38
|
+
parse: () => parse,
|
|
39
|
+
schemify: () => schemify,
|
|
40
|
+
string: () => string,
|
|
41
|
+
symbol: () => symbol,
|
|
42
|
+
union: () => union,
|
|
43
|
+
unknown: () => unknown,
|
|
44
|
+
uri: () => uri,
|
|
45
|
+
uuid: () => uuid,
|
|
46
|
+
validate: () => validate
|
|
47
|
+
});
|
|
48
|
+
module.exports = __toCommonJS(typebox_exports);
|
|
49
|
+
|
|
50
|
+
// src/typebox/typeboxSchemaValidator.ts
|
|
51
|
+
var import_typebox = require("@sinclair/typebox");
|
|
52
|
+
var import_compiler = require("@sinclair/typebox/compiler");
|
|
53
|
+
var import_errors = require("@sinclair/typebox/errors");
|
|
54
|
+
var import_value = require("@sinclair/typebox/value");
|
|
55
|
+
(0, import_errors.SetErrorFunction)((params) => {
|
|
56
|
+
switch (params.errorType) {
|
|
57
|
+
case import_errors.ValueErrorType.Union:
|
|
58
|
+
case import_errors.ValueErrorType.Array:
|
|
59
|
+
case import_errors.ValueErrorType.String:
|
|
60
|
+
case import_errors.ValueErrorType.Number:
|
|
61
|
+
return params.schema.errorType ? `Expected ${params.schema.errorType} value${params.schema.errorSuffix ? "s" : ""}` : (0, import_errors.DefaultErrorFunction)(params);
|
|
62
|
+
default:
|
|
63
|
+
return (0, import_errors.DefaultErrorFunction)(params);
|
|
64
|
+
}
|
|
65
|
+
});
|
|
66
|
+
var TypeboxSchemaValidator = class {
|
|
67
|
+
_Type;
|
|
68
|
+
_SchemaCatchall;
|
|
69
|
+
_ValidSchemaObject;
|
|
70
|
+
string = import_typebox.Type.String();
|
|
71
|
+
// uuid = Type.String({ format: 'uuid' });
|
|
72
|
+
uuid = import_typebox.Type.String({
|
|
73
|
+
pattern: "^[a-fA-F0-9]{8}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{4}-[a-fA-F0-9]{12}$",
|
|
74
|
+
errorType: "uuid"
|
|
75
|
+
});
|
|
76
|
+
uri = import_typebox.Type.String({
|
|
77
|
+
pattern: "^[a-zA-Z][a-zA-Z\\d+-.]*:[^\\s]*$",
|
|
78
|
+
errorType: "uri"
|
|
79
|
+
});
|
|
80
|
+
email = import_typebox.Type.String({
|
|
81
|
+
pattern: `(?:[a-z0-9!#$%&'*+/=?^_{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_{|}~-]+)*|"(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21\\x23-\\x5b\\x5d-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\\[(?:(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9]))\\.){3}(?:(2(5[0-5]|[0-4][0-9])|1[0-9][0-9]|[1-9]?[0-9])|[a-z0-9-]*[a-z0-9]:(?:[\\x01-\\x08\\x0b\\x0c\\x0e-\\x1f\\x21-\\x5a\\x53-\\x7f]|\\\\[\\x01-\\x09\\x0b\\x0c\\x0e-\\x7f])+)])`,
|
|
82
|
+
errorType: "email"
|
|
83
|
+
});
|
|
84
|
+
number = import_typebox.Type.Transform(
|
|
85
|
+
import_typebox.Type.Union(
|
|
86
|
+
[
|
|
87
|
+
import_typebox.Type.Number(),
|
|
88
|
+
import_typebox.Type.String({ pattern: "^[0-9]+$" }),
|
|
89
|
+
import_typebox.Type.Boolean(),
|
|
90
|
+
import_typebox.Type.Null(),
|
|
91
|
+
import_typebox.Type.Date(),
|
|
92
|
+
import_typebox.Type.BigInt()
|
|
93
|
+
],
|
|
94
|
+
{
|
|
95
|
+
errorType: "number-like",
|
|
96
|
+
openapiType: import_typebox.Type.Number()
|
|
97
|
+
}
|
|
98
|
+
)
|
|
99
|
+
).Decode((value) => {
|
|
100
|
+
if (typeof value !== "number") {
|
|
101
|
+
const num = Number(value);
|
|
102
|
+
if (isNaN(num)) {
|
|
103
|
+
throw new Error("Invalid number");
|
|
104
|
+
} else {
|
|
105
|
+
return num;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return value;
|
|
109
|
+
}).Encode(Number);
|
|
110
|
+
bigint = import_typebox.Type.Transform(
|
|
111
|
+
import_typebox.Type.Union(
|
|
112
|
+
[
|
|
113
|
+
import_typebox.Type.BigInt(),
|
|
114
|
+
import_typebox.Type.Number(),
|
|
115
|
+
import_typebox.Type.String({ pattern: "^[0-9]+$" }),
|
|
116
|
+
import_typebox.Type.Boolean(),
|
|
117
|
+
import_typebox.Type.Date()
|
|
118
|
+
],
|
|
119
|
+
{
|
|
120
|
+
errorType: "BigInt-like",
|
|
121
|
+
openapiType: import_typebox.Type.BigInt()
|
|
122
|
+
}
|
|
123
|
+
)
|
|
124
|
+
).Decode((value) => {
|
|
125
|
+
if (typeof value !== "bigint") {
|
|
126
|
+
try {
|
|
127
|
+
return BigInt(value instanceof Date ? value.getTime() : value);
|
|
128
|
+
} catch {
|
|
129
|
+
throw new Error("Invalid bigint");
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
return value;
|
|
133
|
+
}).Encode(BigInt);
|
|
134
|
+
boolean = import_typebox.Type.Transform(
|
|
135
|
+
import_typebox.Type.Union(
|
|
136
|
+
[
|
|
137
|
+
import_typebox.Type.Boolean(),
|
|
138
|
+
import_typebox.Type.String({
|
|
139
|
+
pattern: "^(t|T)(r|R)(u|U)(e|E)$|^(f|F)(a|A)(l|L)(s|S)(e|E)$"
|
|
140
|
+
})
|
|
141
|
+
],
|
|
142
|
+
{
|
|
143
|
+
errorType: "boolean-like",
|
|
144
|
+
openapiType: import_typebox.Type.Boolean()
|
|
145
|
+
}
|
|
146
|
+
)
|
|
147
|
+
).Decode((value) => {
|
|
148
|
+
if (typeof value === "string") {
|
|
149
|
+
if (value.toLowerCase() === "true") return true;
|
|
150
|
+
if (value.toLowerCase() === "false") return false;
|
|
151
|
+
} else {
|
|
152
|
+
return value;
|
|
153
|
+
}
|
|
154
|
+
}).Encode(Boolean);
|
|
155
|
+
date = import_typebox.Type.Transform(
|
|
156
|
+
import_typebox.Type.Union(
|
|
157
|
+
[
|
|
158
|
+
import_typebox.Type.Date(),
|
|
159
|
+
import_typebox.Type.Number(),
|
|
160
|
+
import_typebox.Type.String({
|
|
161
|
+
pattern: "^\\d{4}(-\\d{2}){0,2}(T\\d{2}:\\d{2}(:\\d{2}(\\.\\d{1,3})?)?(Z|([+-]\\d{2}:\\d{2}))?)?$|^\\d{1,2}\\/\\d{1,2}\\/\\d{4}$|^\\d{4}\\/\\d{1,2}\\/\\d{1,2}$|^\\d+$"
|
|
162
|
+
}),
|
|
163
|
+
import_typebox.Type.Boolean(),
|
|
164
|
+
import_typebox.Type.Null()
|
|
165
|
+
],
|
|
166
|
+
{
|
|
167
|
+
errorType: "date",
|
|
168
|
+
openapiType: import_typebox.Type.Date()
|
|
169
|
+
}
|
|
170
|
+
)
|
|
171
|
+
).Decode((value) => {
|
|
172
|
+
if (!(value instanceof Date)) {
|
|
173
|
+
if (value === null || typeof value === "boolean") {
|
|
174
|
+
return /* @__PURE__ */ new Date(value ? 1 : 0);
|
|
175
|
+
}
|
|
176
|
+
return new Date(value);
|
|
177
|
+
}
|
|
178
|
+
return value;
|
|
179
|
+
}).Encode((value) => new Date(value));
|
|
180
|
+
symbol = import_typebox.Type.Symbol();
|
|
181
|
+
nullish = import_typebox.Type.Union([import_typebox.Type.Void(), import_typebox.Type.Null(), import_typebox.Type.Undefined()], {
|
|
182
|
+
errorType: "nullish"
|
|
183
|
+
});
|
|
184
|
+
any = import_typebox.Type.Any();
|
|
185
|
+
unknown = import_typebox.Type.Unknown();
|
|
186
|
+
never = import_typebox.Type.Never();
|
|
187
|
+
/**
|
|
188
|
+
* Extracts the error type of a schema for error messages.
|
|
189
|
+
*
|
|
190
|
+
* @param {TCatchall} schema - A schema that contains some type information.
|
|
191
|
+
* @returns The type of the schema for error messages.
|
|
192
|
+
*/
|
|
193
|
+
errorType(schema) {
|
|
194
|
+
if (Object.hasOwn(schema, "errorType")) {
|
|
195
|
+
return schema.errorType;
|
|
196
|
+
} else if (import_typebox.KindGuard.IsLiteral(schema)) {
|
|
197
|
+
return schema.const;
|
|
198
|
+
}
|
|
199
|
+
return schema[import_typebox.Kind].toLowerCase();
|
|
200
|
+
}
|
|
201
|
+
/**
|
|
202
|
+
* Compiles schema if this exists, for optimal performance.
|
|
203
|
+
*
|
|
204
|
+
* @param {TObject<TProperties>} schema - The schema to compile.
|
|
205
|
+
* @returns {TypeCheck<T>} - The compiled schema.
|
|
206
|
+
*/
|
|
207
|
+
compile(schema) {
|
|
208
|
+
return import_compiler.TypeCompiler.Compile(schema);
|
|
209
|
+
}
|
|
210
|
+
/**
|
|
211
|
+
* Convert a schema to a TypeBox schema.
|
|
212
|
+
* @param {TIdiomaticSchema} schema - The schema to convert.
|
|
213
|
+
* @returns {TResolve<T>} The resolved schema.
|
|
214
|
+
*/
|
|
215
|
+
schemify(schema) {
|
|
216
|
+
if (typeof schema === "string" || typeof schema === "number" || typeof schema === "boolean") {
|
|
217
|
+
return import_typebox.Type.Literal(schema);
|
|
218
|
+
}
|
|
219
|
+
if (import_typebox.KindGuard.IsSchema(schema) || schema instanceof import_compiler.TypeCheck) {
|
|
220
|
+
return schema;
|
|
221
|
+
}
|
|
222
|
+
const newSchema = {};
|
|
223
|
+
Object.getOwnPropertyNames(schema).forEach((key) => {
|
|
224
|
+
if (import_typebox.KindGuard.IsSchema(schema[key])) {
|
|
225
|
+
newSchema[key] = schema[key];
|
|
226
|
+
} else {
|
|
227
|
+
const schemified = this.schemify(schema[key]);
|
|
228
|
+
newSchema[key] = schemified;
|
|
229
|
+
}
|
|
230
|
+
});
|
|
231
|
+
return import_typebox.Type.Object(newSchema);
|
|
232
|
+
}
|
|
233
|
+
/**
|
|
234
|
+
* Make a schema optional.
|
|
235
|
+
* @param {TIdiomaticSchema} schema - The schema to make optional.
|
|
236
|
+
* @returns {TOptional<TResolve<T>>} The optional schema.
|
|
237
|
+
*/
|
|
238
|
+
optional(schema) {
|
|
239
|
+
const schemified = import_typebox.KindGuard.IsSchema(schema) ? schema : this.schemify(schema);
|
|
240
|
+
return import_typebox.Type.Optional(schemified);
|
|
241
|
+
}
|
|
242
|
+
/**
|
|
243
|
+
* Create an array schema.
|
|
244
|
+
* @param {TIdiomaticSchema} schema - The schema to use for array items.
|
|
245
|
+
* @returns {TArray<TResolve<T>>} The array schema.
|
|
246
|
+
*/
|
|
247
|
+
array(schema) {
|
|
248
|
+
const schemified = import_typebox.KindGuard.IsSchema(schema) ? schema : this.schemify(schema);
|
|
249
|
+
return import_typebox.Type.Array(schemified, {
|
|
250
|
+
errorType: `array of ${this.errorType(schemified)}`
|
|
251
|
+
});
|
|
252
|
+
}
|
|
253
|
+
/**
|
|
254
|
+
* Create a union schema.
|
|
255
|
+
* @param {TUnionContainer} schemas - The schemas to union.
|
|
256
|
+
* @returns {TUnion<UnionTResolve<T>>} The union schema.
|
|
257
|
+
*
|
|
258
|
+
* WARNING: If "nullish" or TUndefined is included in the union, the key will still be expected.
|
|
259
|
+
* This is a limitation of TypeBox. Consider using "optional" instead.
|
|
260
|
+
*/
|
|
261
|
+
union(schemas) {
|
|
262
|
+
const unionTypes = schemas.map((schema) => {
|
|
263
|
+
return import_typebox.KindGuard.IsSchema(schema) ? schema : this.schemify(schema);
|
|
264
|
+
});
|
|
265
|
+
return import_typebox.Type.Union(unionTypes, {
|
|
266
|
+
errorType: `any of ${unionTypes.map((s) => this.errorType(s)).join(", ")}`,
|
|
267
|
+
errorSuffix: true
|
|
268
|
+
});
|
|
269
|
+
}
|
|
270
|
+
/**
|
|
271
|
+
* Create a literal schema.
|
|
272
|
+
* @param {LiteralSchema} value - The literal value.
|
|
273
|
+
* @returns {TLiteral<T>} The literal schema.
|
|
274
|
+
*/
|
|
275
|
+
literal(value) {
|
|
276
|
+
return import_typebox.Type.Literal(value, {
|
|
277
|
+
errorType: value
|
|
278
|
+
});
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
* Create an enum schema.
|
|
282
|
+
* @param {Record<string, LiteralSchema>} schemaEnum - The enum schema.
|
|
283
|
+
* @returns {TUnion<UnionTResolve<T[]>>} The enum schema.
|
|
284
|
+
*/
|
|
285
|
+
enum_(schemaEnum) {
|
|
286
|
+
return this.union(Object.values(schemaEnum));
|
|
287
|
+
}
|
|
288
|
+
/**
|
|
289
|
+
* Check if a value is a TypeBox object schema.
|
|
290
|
+
* @param {unknown} value - The value to check.
|
|
291
|
+
* @returns {boolean} True if the value is a TypeBox object schema.
|
|
292
|
+
*/
|
|
293
|
+
isSchema(value) {
|
|
294
|
+
return import_typebox.KindGuard.IsSchema(value);
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* Validate a value against a schema.
|
|
298
|
+
*
|
|
299
|
+
* @param {TSchema} schema - The schema to validate against.
|
|
300
|
+
* @param {unknown} value - The value to validate.
|
|
301
|
+
* @returns {boolean} True if valid, otherwise false.
|
|
302
|
+
*/
|
|
303
|
+
validate(schema, value) {
|
|
304
|
+
if (schema instanceof import_compiler.TypeCheck) {
|
|
305
|
+
return schema.Check(value);
|
|
306
|
+
} else {
|
|
307
|
+
const schemified = import_typebox.KindGuard.IsSchema(schema) ? schema : this.schemify(schema);
|
|
308
|
+
return import_value.Value.Check(schemified, value);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
/**
|
|
312
|
+
* Parse a value against a schema.
|
|
313
|
+
*
|
|
314
|
+
* @param {TSchema} schema - The schema to validate against.
|
|
315
|
+
* @param {unknown} value - The value to validate.
|
|
316
|
+
* @returns {ParseResult<TResolve<T>>} The parsing result.
|
|
317
|
+
*/
|
|
318
|
+
parse(schema, value) {
|
|
319
|
+
let errors = [];
|
|
320
|
+
let conversion;
|
|
321
|
+
if (schema instanceof import_compiler.TypeCheck) {
|
|
322
|
+
if (schema.Check(value)) {
|
|
323
|
+
conversion = schema.Decode(value);
|
|
324
|
+
} else {
|
|
325
|
+
errors = Array.from(schema.Errors(value));
|
|
326
|
+
}
|
|
327
|
+
} else {
|
|
328
|
+
const schemified = import_typebox.KindGuard.IsSchema(schema) ? schema : this.schemify(schema);
|
|
329
|
+
if (import_value.Value.Check(schemified, value)) {
|
|
330
|
+
conversion = import_value.Value.Decode(schemified, value);
|
|
331
|
+
} else {
|
|
332
|
+
errors = Array.from(import_value.Value.Errors(schemified, value));
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
return errors != null && errors.length === 0 ? {
|
|
336
|
+
ok: true,
|
|
337
|
+
value: conversion
|
|
338
|
+
} : {
|
|
339
|
+
ok: false,
|
|
340
|
+
errors: errors.map((error) => ({
|
|
341
|
+
path: error.path.split("/").slice(1),
|
|
342
|
+
message: error.message
|
|
343
|
+
}))
|
|
344
|
+
};
|
|
345
|
+
}
|
|
346
|
+
/**
|
|
347
|
+
* Convert a schema to an OpenAPI schema object.
|
|
348
|
+
* @param {TIdiomaticSchema | TSchema} schema - The schema to convert.
|
|
349
|
+
* @returns {SchemaObject} The OpenAPI schema object.
|
|
350
|
+
*/
|
|
351
|
+
openapi(schema) {
|
|
352
|
+
const schemified = import_typebox.KindGuard.IsSchema(schema) ? schema : this.schemify(schema);
|
|
353
|
+
if (Object.hasOwn(schemified, "openapiType") || import_typebox.KindGuard.IsLiteral(schemified)) {
|
|
354
|
+
return schemified.openapiType;
|
|
355
|
+
}
|
|
356
|
+
const newSchema = Object.assign({}, schemified);
|
|
357
|
+
if (Object.hasOwn(newSchema, "properties")) {
|
|
358
|
+
newSchema.properties = { ...schemified.properties };
|
|
359
|
+
if (newSchema.properties) {
|
|
360
|
+
Object.entries(newSchema.properties).forEach(([key, value]) => {
|
|
361
|
+
if (import_typebox.KindGuard.IsSchema(value) && newSchema.properties) {
|
|
362
|
+
newSchema.properties[key] = this.openapi(value);
|
|
363
|
+
}
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
return newSchema;
|
|
368
|
+
}
|
|
369
|
+
};
|
|
370
|
+
|
|
371
|
+
// src/typebox/staticSchemaValidator.ts
|
|
372
|
+
var SchemaValidator = () => new TypeboxSchemaValidator();
|
|
373
|
+
var StaticSchemaValidator = SchemaValidator();
|
|
374
|
+
var string = StaticSchemaValidator.string;
|
|
375
|
+
var uuid = StaticSchemaValidator.uuid;
|
|
376
|
+
var uri = StaticSchemaValidator.uri;
|
|
377
|
+
var email = StaticSchemaValidator.email;
|
|
378
|
+
var number = StaticSchemaValidator.number;
|
|
379
|
+
var bigint = StaticSchemaValidator.bigint;
|
|
380
|
+
var boolean = StaticSchemaValidator.boolean;
|
|
381
|
+
var date = StaticSchemaValidator.date;
|
|
382
|
+
var symbol = StaticSchemaValidator.symbol;
|
|
383
|
+
var nullish = StaticSchemaValidator.nullish;
|
|
384
|
+
var any = StaticSchemaValidator.any;
|
|
385
|
+
var unknown = StaticSchemaValidator.unknown;
|
|
386
|
+
var never = StaticSchemaValidator.never;
|
|
387
|
+
var schemify = StaticSchemaValidator.schemify.bind(
|
|
388
|
+
StaticSchemaValidator
|
|
389
|
+
);
|
|
390
|
+
var optional = StaticSchemaValidator.optional.bind(
|
|
391
|
+
StaticSchemaValidator
|
|
392
|
+
);
|
|
393
|
+
var array = StaticSchemaValidator.array.bind(StaticSchemaValidator);
|
|
394
|
+
var union = StaticSchemaValidator.union.bind(StaticSchemaValidator);
|
|
395
|
+
var literal = StaticSchemaValidator.literal.bind(
|
|
396
|
+
StaticSchemaValidator
|
|
397
|
+
);
|
|
398
|
+
var enum_ = StaticSchemaValidator.enum_.bind(StaticSchemaValidator);
|
|
399
|
+
var isSchema = StaticSchemaValidator.isSchema.bind(
|
|
400
|
+
StaticSchemaValidator
|
|
401
|
+
);
|
|
402
|
+
var validate = StaticSchemaValidator.validate.bind(
|
|
403
|
+
StaticSchemaValidator
|
|
404
|
+
);
|
|
405
|
+
var parse = StaticSchemaValidator.parse.bind(StaticSchemaValidator);
|
|
406
|
+
var openapi = StaticSchemaValidator.openapi.bind(
|
|
407
|
+
StaticSchemaValidator
|
|
408
|
+
);
|
|
409
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
410
|
+
0 && (module.exports = {
|
|
411
|
+
SchemaValidator,
|
|
412
|
+
any,
|
|
413
|
+
array,
|
|
414
|
+
bigint,
|
|
415
|
+
boolean,
|
|
416
|
+
date,
|
|
417
|
+
email,
|
|
418
|
+
enum_,
|
|
419
|
+
isSchema,
|
|
420
|
+
literal,
|
|
421
|
+
never,
|
|
422
|
+
nullish,
|
|
423
|
+
number,
|
|
424
|
+
openapi,
|
|
425
|
+
optional,
|
|
426
|
+
parse,
|
|
427
|
+
schemify,
|
|
428
|
+
string,
|
|
429
|
+
symbol,
|
|
430
|
+
union,
|
|
431
|
+
unknown,
|
|
432
|
+
uri,
|
|
433
|
+
uuid,
|
|
434
|
+
validate
|
|
435
|
+
});
|