@esmj/schema 0.2.3 → 0.3.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/README.md +94 -35
- package/dist/index.d.mts +25 -15
- package/dist/index.d.ts +25 -15
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -17,6 +17,15 @@ npm install @esmj/schema
|
|
|
17
17
|
3. **Rich Features**: Includes advanced features like preprocessing, transformations, piping, and refinements, which are not always available in similar libraries.
|
|
18
18
|
4. **Lightweight**: None dependencies and a small footprint make it ideal for projects where performance and simplicity are key.
|
|
19
19
|
5. **Customizable**: Offers fine-grained control over validation and error handling.
|
|
20
|
+
6. **Performance**: `@esmj/schema` is optimized for speed, making it one of the fastest schema validation libraries available. Whether you're creating schemas, parsing data, or handling errors, `@esmj/schema` consistently outperforms many popular alternatives. Its minimalistic design ensures low overhead, even in high-performance applications.
|
|
21
|
+
|
|
22
|
+
### Performance Highlights
|
|
23
|
+
|
|
24
|
+
- **Schema Creation**: Create schemas in as little as `0.02 ms`, even for complex structures.
|
|
25
|
+
- **Parsing**: Parse data with blazing-fast speeds, handling 1,000,000 iterations in under `300 ms`.
|
|
26
|
+
- **Error Handling**: Efficiently manage errors with minimal performance impact, processing 1,000,000 iterations in under `400 ms`.
|
|
27
|
+
|
|
28
|
+
These performance metrics make `@esmj/schema` an excellent choice for both frontend and backend applications where speed and efficiency are critical.
|
|
20
29
|
|
|
21
30
|
## Comparison with Similar Libraries
|
|
22
31
|
|
|
@@ -121,83 +130,133 @@ console.log(result);
|
|
|
121
130
|
|
|
122
131
|
### Schema Types
|
|
123
132
|
|
|
124
|
-
#### `s.string()`
|
|
133
|
+
#### `s.string(options?)`
|
|
125
134
|
|
|
126
|
-
Creates a string schema.
|
|
135
|
+
Creates a string schema. You can optionally pass `SchemaInterfaceOptions` to customize error messages.
|
|
127
136
|
|
|
128
137
|
```typescript
|
|
129
|
-
const stringSchema = s.string(
|
|
138
|
+
const stringSchema = s.string({
|
|
139
|
+
message: (value) => `Custom error: "${value}" is not a valid string.`,
|
|
140
|
+
});
|
|
130
141
|
```
|
|
131
142
|
|
|
132
|
-
#### `s.number()`
|
|
143
|
+
#### `s.number(options?)`
|
|
133
144
|
|
|
134
|
-
Creates a number schema.
|
|
145
|
+
Creates a number schema. You can optionally pass `SchemaInterfaceOptions` to customize error messages.
|
|
135
146
|
|
|
136
147
|
```typescript
|
|
137
|
-
const numberSchema = s.number(
|
|
148
|
+
const numberSchema = s.number({
|
|
149
|
+
message: (value) => `Custom error: "${value}" is not a valid number.`,
|
|
150
|
+
});
|
|
138
151
|
```
|
|
139
152
|
|
|
140
|
-
#### `s.boolean()`
|
|
153
|
+
#### `s.boolean(options?)`
|
|
141
154
|
|
|
142
|
-
Creates a boolean schema.
|
|
155
|
+
Creates a boolean schema. You can optionally pass `SchemaInterfaceOptions` to customize error messages.
|
|
143
156
|
|
|
144
157
|
```typescript
|
|
145
|
-
const booleanSchema = s.boolean(
|
|
158
|
+
const booleanSchema = s.boolean({
|
|
159
|
+
message: (value) => `Custom error: "${value}" is not a valid boolean.`,
|
|
160
|
+
});
|
|
146
161
|
```
|
|
147
162
|
|
|
148
|
-
#### `s.date()`
|
|
163
|
+
#### `s.date(options?)`
|
|
149
164
|
|
|
150
|
-
Creates a date schema.
|
|
165
|
+
Creates a date schema. You can optionally pass `SchemaInterfaceOptions` to customize error messages.
|
|
151
166
|
|
|
152
167
|
```typescript
|
|
153
|
-
const dateSchema = s.date(
|
|
168
|
+
const dateSchema = s.date({
|
|
169
|
+
message: (value) => `Custom error: "${value}" is not a valid date.`,
|
|
170
|
+
});
|
|
154
171
|
```
|
|
155
172
|
|
|
156
|
-
#### `s.object(definition)`
|
|
173
|
+
#### `s.object(definition, options?)`
|
|
157
174
|
|
|
158
|
-
Creates an object schema with the given definition.
|
|
175
|
+
Creates an object schema with the given definition. You can optionally pass `SchemaInterfaceOptions` to customize error messages.
|
|
159
176
|
|
|
160
177
|
```typescript
|
|
161
|
-
const objectSchema = s.object(
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
178
|
+
const objectSchema = s.object(
|
|
179
|
+
{
|
|
180
|
+
key: s.string(),
|
|
181
|
+
value: s.number(),
|
|
182
|
+
},
|
|
183
|
+
{
|
|
184
|
+
message: (value) => `Custom error: "${JSON.stringify(value)}" is not a valid object.`,
|
|
185
|
+
},
|
|
186
|
+
);
|
|
165
187
|
```
|
|
166
188
|
|
|
167
|
-
#### `s.array(definition)`
|
|
189
|
+
#### `s.array(definition, options?)`
|
|
168
190
|
|
|
169
|
-
Creates an array schema with the given item definition.
|
|
191
|
+
Creates an array schema with the given item definition. You can optionally pass `SchemaInterfaceOptions` to customize error messages.
|
|
170
192
|
|
|
171
193
|
```typescript
|
|
172
|
-
const arraySchema = s.array(s.string()
|
|
194
|
+
const arraySchema = s.array(s.string(), {
|
|
195
|
+
message: (value) => `Custom error: "${JSON.stringify(value)}" is not a valid array.`,
|
|
196
|
+
});
|
|
173
197
|
```
|
|
174
198
|
|
|
175
|
-
#### `s.
|
|
199
|
+
#### `s.enum(values, options?)`
|
|
176
200
|
|
|
177
|
-
Creates
|
|
201
|
+
Creates an enum schema that validates against a predefined set of string values. You can optionally pass `SchemaInterfaceOptions` to customize error messages.
|
|
202
|
+
|
|
203
|
+
- **`values`**: An array of strings representing the allowed values for the enum. Each value must be a string.
|
|
178
204
|
|
|
179
205
|
```typescript
|
|
180
|
-
const
|
|
206
|
+
const enumSchema = s.enum(['admin', 'user', 'guest'], {
|
|
207
|
+
message: (value) => `Custom error: "${value}" is not a valid enum value.`,
|
|
208
|
+
});
|
|
181
209
|
```
|
|
182
210
|
|
|
183
|
-
#### `s.
|
|
211
|
+
#### `s.union(definitions, options?)`
|
|
184
212
|
|
|
185
|
-
Creates
|
|
213
|
+
Creates a schema that validates against multiple schemas (a union of schemas). The value must match at least one of the provided schemas. You can optionally pass `SchemaInterfaceOptions` to customize error messages.
|
|
186
214
|
|
|
187
|
-
- **`
|
|
215
|
+
- **`definitions`**: An array of schemas to validate against.
|
|
188
216
|
|
|
189
217
|
```typescript
|
|
190
|
-
const
|
|
218
|
+
const schema = s.union([
|
|
219
|
+
s.string(),
|
|
220
|
+
s.number(),
|
|
221
|
+
s.boolean(),
|
|
222
|
+
]);
|
|
223
|
+
|
|
224
|
+
const validString = schema.parse('hello');
|
|
225
|
+
console.log(validString);
|
|
226
|
+
// 'hello'
|
|
227
|
+
|
|
228
|
+
const validNumber = schema.parse(42);
|
|
229
|
+
console.log(validNumber);
|
|
230
|
+
// 42
|
|
191
231
|
|
|
192
|
-
const
|
|
193
|
-
console.log(
|
|
194
|
-
//
|
|
232
|
+
const validBoolean = schema.parse(true);
|
|
233
|
+
console.log(validBoolean);
|
|
234
|
+
// true
|
|
195
235
|
|
|
196
|
-
const
|
|
197
|
-
console.log(
|
|
236
|
+
const invalidValue = schema.safeParse({ key: 'value' });
|
|
237
|
+
console.log(invalidValue.success);
|
|
198
238
|
// false
|
|
199
|
-
console.log(
|
|
200
|
-
//
|
|
239
|
+
console.log(invalidValue.error.message);
|
|
240
|
+
// Validation failed. Expected the value to match one of the schemas: "string" | "number" | "boolean", but received "object" with value "{"key":"value"}".
|
|
241
|
+
```
|
|
242
|
+
|
|
243
|
+
**Use Case**: The `union` method is useful when you need to validate data that can be of multiple types, such as a value that can be a string, number, or boolean.
|
|
244
|
+
|
|
245
|
+
```typescript
|
|
246
|
+
const schema = s.union(
|
|
247
|
+
[s.string(), s.number(), s.boolean()],
|
|
248
|
+
{
|
|
249
|
+
message: (value) => `Custom error: "${value}" does not match any of the union schemas.`,
|
|
250
|
+
},
|
|
251
|
+
);
|
|
252
|
+
```
|
|
253
|
+
|
|
254
|
+
#### `s.any()`
|
|
255
|
+
|
|
256
|
+
Creates a schema that accepts any value.
|
|
257
|
+
|
|
258
|
+
```typescript
|
|
259
|
+
const anySchema = s.any();
|
|
201
260
|
```
|
|
202
261
|
|
|
203
262
|
#### `s.preprocess(callback, schema)`
|
package/dist/index.d.mts
CHANGED
|
@@ -14,9 +14,10 @@ type Invalid = {
|
|
|
14
14
|
};
|
|
15
15
|
type InternalParseOutput<Output> = Valid<Output> | Invalid;
|
|
16
16
|
interface SchemaInterface<Input, Output> {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
_getType(): string;
|
|
18
|
+
_parse(value: Input | Partial<Input>): InternalParseOutput<Output>;
|
|
19
|
+
parse(value: Input | Partial<Input>): Output;
|
|
20
|
+
safeParse(value: Input | Partial<Input>): InternalParseOutput<Output>;
|
|
20
21
|
optional(): SchemaInterface<Input, Partial<Output> | undefined>;
|
|
21
22
|
transform<NewOutput>(callback: (value: Input) => NewOutput): SchemaInterface<Input, NewOutput>;
|
|
22
23
|
nullable(): SchemaInterface<Input, Output | null>;
|
|
@@ -24,9 +25,11 @@ interface SchemaInterface<Input, Output> {
|
|
|
24
25
|
default(defaultValue: Partial<Input> | (() => Partial<Input>) | Partial<Output>): SchemaInterface<Input, Output>;
|
|
25
26
|
pipe<NewOutput>(schema: SchemaInterface<Output, NewOutput>): SchemaInterface<Output, NewOutput>;
|
|
26
27
|
refine(validation: (value: Output) => boolean, { message }: {
|
|
27
|
-
message:
|
|
28
|
+
message: ErrorMessage;
|
|
28
29
|
}): SchemaInterface<Input, Output>;
|
|
29
30
|
}
|
|
31
|
+
interface UnionSchemaInterface<T extends Array<SchemaInterface<unknown, unknown>>> extends SchemaInterface<ReturnType<T[number]['parse']>, ReturnType<T[number]['parse']>> {
|
|
32
|
+
}
|
|
30
33
|
interface EnumSchemaInterface<T extends string> extends SchemaInterface<string, T> {
|
|
31
34
|
}
|
|
32
35
|
interface StringSchemaInterface extends SchemaInterface<string, string> {
|
|
@@ -40,28 +43,35 @@ interface DateSchemaInterface extends SchemaInterface<Date, Date> {
|
|
|
40
43
|
interface ArraySchemaInterface<T extends SchemaType> extends SchemaInterface<Array<T>, Array<ReturnType<T['parse']>>> {
|
|
41
44
|
}
|
|
42
45
|
interface ObjectSchemaInterface<T extends Record<string, SchemaType>> extends SchemaInterface<{
|
|
43
|
-
[Property in keyof T]: T[Property]
|
|
46
|
+
[Property in keyof T]: ReturnType<T[Property]['parse']>;
|
|
44
47
|
}, {
|
|
45
48
|
[Property in keyof T]: ReturnType<T[Property]['parse']>;
|
|
46
49
|
}> {
|
|
47
50
|
}
|
|
48
|
-
type SchemaType = StringSchemaInterface | SchemaInterface<string, string> | SchemaInterface<string, string | undefined> | SchemaInterface<string, string | null> | SchemaInterface<string, string | undefined | null> | ObjectSchemaInterface<Record<string, SchemaType>> | SchemaInterface<object, object> | SchemaInterface<object, object | undefined> | SchemaInterface<object, object | null> | SchemaInterface<object, object | undefined | null> | NumberSchemaInterface | SchemaInterface<number, number> | SchemaInterface<number, number | undefined> | SchemaInterface<number, number | null> | SchemaInterface<number, number | undefined | null> | BooleanSchemaInterface | SchemaInterface<boolean, boolean> | SchemaInterface<boolean, boolean | undefined> | SchemaInterface<boolean, boolean | null> | SchemaInterface<boolean, boolean | undefined | null> | DateSchemaInterface | SchemaInterface<Date, Date> | SchemaInterface<Date, Date | undefined> | SchemaInterface<Date, Date | null> | SchemaInterface<Date, Date | undefined | null> | EnumSchemaInterface<string> | ArraySchemaInterface<StringSchemaInterface | ObjectSchemaInterface<Record<string, SchemaType>> | NumberSchemaInterface | BooleanSchemaInterface | DateSchemaInterface | EnumSchemaInterface<string>> | SchemaInterface<Array<unknown>, Array<unknown>> | SchemaInterface<Array<unknown>, Array<unknown> | undefined> | SchemaInterface<Array<unknown>, Array<unknown> | null> | SchemaInterface<Array<unknown>, Array<unknown> | undefined | null>;
|
|
51
|
+
type SchemaType = StringSchemaInterface | SchemaInterface<unknown, unknown> | SchemaInterface<string, string> | SchemaInterface<string, string | undefined> | SchemaInterface<string, string | null> | SchemaInterface<string, string | undefined | null> | ObjectSchemaInterface<Record<string, SchemaType>> | SchemaInterface<object, object> | SchemaInterface<object, object | undefined> | SchemaInterface<object, object | null> | SchemaInterface<object, object | undefined | null> | NumberSchemaInterface | SchemaInterface<number, number> | SchemaInterface<number, number | undefined> | SchemaInterface<number, number | null> | SchemaInterface<number, number | undefined | null> | BooleanSchemaInterface | SchemaInterface<boolean, boolean> | SchemaInterface<boolean, boolean | undefined> | SchemaInterface<boolean, boolean | null> | SchemaInterface<boolean, boolean | undefined | null> | DateSchemaInterface | SchemaInterface<Date, Date> | SchemaInterface<Date, Date | undefined> | SchemaInterface<Date, Date | null> | SchemaInterface<Date, Date | undefined | null> | EnumSchemaInterface<string> | UnionSchemaInterface<Array<SchemaInterface<unknown, unknown>>> | ArraySchemaInterface<StringSchemaInterface | ObjectSchemaInterface<Record<string, SchemaType>> | NumberSchemaInterface | BooleanSchemaInterface | DateSchemaInterface | EnumSchemaInterface<string>> | SchemaInterface<Array<unknown>, Array<unknown>> | SchemaInterface<Array<unknown>, Array<unknown> | undefined> | SchemaInterface<Array<unknown>, Array<unknown> | null> | SchemaInterface<Array<unknown>, Array<unknown> | undefined | null>;
|
|
52
|
+
type ErrorMessage = string | ((value: unknown) => string);
|
|
49
53
|
type ExtenderType = (inter: SchemaType, validation: Function, options: {
|
|
50
|
-
message:
|
|
54
|
+
message: ErrorMessage;
|
|
51
55
|
type: string;
|
|
52
56
|
}) => SchemaType;
|
|
57
|
+
interface CreateSchemaInterfaceOptions {
|
|
58
|
+
type?: string;
|
|
59
|
+
message?: (value: unknown) => string;
|
|
60
|
+
}
|
|
61
|
+
type SchemaInterfaceOptions = Omit<CreateSchemaInterfaceOptions, 'type'>;
|
|
53
62
|
declare const s: {
|
|
54
|
-
object<T extends Record<string, SchemaType>>(definition: { [Property in keyof T]: T[Property]; }): ObjectSchemaInterface<T>;
|
|
55
|
-
string(): StringSchemaInterface;
|
|
56
|
-
number(): NumberSchemaInterface;
|
|
57
|
-
boolean(): BooleanSchemaInterface;
|
|
58
|
-
date(): DateSchemaInterface;
|
|
59
|
-
enum(definition: Readonly<Array<string
|
|
60
|
-
array<T extends SchemaType>(definition: T): ArraySchemaInterface<T>;
|
|
63
|
+
object<T extends Record<string, SchemaType>>(definition: { [Property in keyof T]: T[Property]; }, options: SchemaInterfaceOptions): ObjectSchemaInterface<T>;
|
|
64
|
+
string(options: SchemaInterfaceOptions): StringSchemaInterface;
|
|
65
|
+
number(options: SchemaInterfaceOptions): NumberSchemaInterface;
|
|
66
|
+
boolean(options: SchemaInterfaceOptions): BooleanSchemaInterface;
|
|
67
|
+
date(options: SchemaInterfaceOptions): DateSchemaInterface;
|
|
68
|
+
enum(definition: Readonly<Array<string>>, options: SchemaInterfaceOptions): EnumSchemaInterface<(typeof definition)[number]>;
|
|
69
|
+
array<T extends SchemaType>(definition: T, options: SchemaInterfaceOptions): ArraySchemaInterface<T>;
|
|
61
70
|
any(): any;
|
|
62
71
|
preprocess<T extends SchemaType>(callback: Function, schema: T): T;
|
|
72
|
+
union<T extends Array<SchemaType>>(definitions: T, options: SchemaInterfaceOptions): UnionSchemaInterface<T>;
|
|
63
73
|
};
|
|
64
74
|
declare function extend(callback: ExtenderType): void;
|
|
65
75
|
type Infer<T> = T extends SchemaType ? ReturnType<T['parse']> : unknown;
|
|
66
76
|
|
|
67
|
-
export { type ArraySchemaInterface, type BooleanSchemaInterface, type DateSchemaInterface, type EnumSchemaInterface, type ExtenderType, type Infer, type NumberSchemaInterface, type ObjectSchemaInterface, type SchemaInterface, type SchemaType, type StringSchemaInterface, extend, s };
|
|
77
|
+
export { type ArraySchemaInterface, type BooleanSchemaInterface, type DateSchemaInterface, type EnumSchemaInterface, type ExtenderType, type Infer, type NumberSchemaInterface, type ObjectSchemaInterface, type SchemaInterface, type SchemaInterfaceOptions, type SchemaType, type StringSchemaInterface, type UnionSchemaInterface, extend, s };
|
package/dist/index.d.ts
CHANGED
|
@@ -14,9 +14,10 @@ type Invalid = {
|
|
|
14
14
|
};
|
|
15
15
|
type InternalParseOutput<Output> = Valid<Output> | Invalid;
|
|
16
16
|
interface SchemaInterface<Input, Output> {
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
_getType(): string;
|
|
18
|
+
_parse(value: Input | Partial<Input>): InternalParseOutput<Output>;
|
|
19
|
+
parse(value: Input | Partial<Input>): Output;
|
|
20
|
+
safeParse(value: Input | Partial<Input>): InternalParseOutput<Output>;
|
|
20
21
|
optional(): SchemaInterface<Input, Partial<Output> | undefined>;
|
|
21
22
|
transform<NewOutput>(callback: (value: Input) => NewOutput): SchemaInterface<Input, NewOutput>;
|
|
22
23
|
nullable(): SchemaInterface<Input, Output | null>;
|
|
@@ -24,9 +25,11 @@ interface SchemaInterface<Input, Output> {
|
|
|
24
25
|
default(defaultValue: Partial<Input> | (() => Partial<Input>) | Partial<Output>): SchemaInterface<Input, Output>;
|
|
25
26
|
pipe<NewOutput>(schema: SchemaInterface<Output, NewOutput>): SchemaInterface<Output, NewOutput>;
|
|
26
27
|
refine(validation: (value: Output) => boolean, { message }: {
|
|
27
|
-
message:
|
|
28
|
+
message: ErrorMessage;
|
|
28
29
|
}): SchemaInterface<Input, Output>;
|
|
29
30
|
}
|
|
31
|
+
interface UnionSchemaInterface<T extends Array<SchemaInterface<unknown, unknown>>> extends SchemaInterface<ReturnType<T[number]['parse']>, ReturnType<T[number]['parse']>> {
|
|
32
|
+
}
|
|
30
33
|
interface EnumSchemaInterface<T extends string> extends SchemaInterface<string, T> {
|
|
31
34
|
}
|
|
32
35
|
interface StringSchemaInterface extends SchemaInterface<string, string> {
|
|
@@ -40,28 +43,35 @@ interface DateSchemaInterface extends SchemaInterface<Date, Date> {
|
|
|
40
43
|
interface ArraySchemaInterface<T extends SchemaType> extends SchemaInterface<Array<T>, Array<ReturnType<T['parse']>>> {
|
|
41
44
|
}
|
|
42
45
|
interface ObjectSchemaInterface<T extends Record<string, SchemaType>> extends SchemaInterface<{
|
|
43
|
-
[Property in keyof T]: T[Property]
|
|
46
|
+
[Property in keyof T]: ReturnType<T[Property]['parse']>;
|
|
44
47
|
}, {
|
|
45
48
|
[Property in keyof T]: ReturnType<T[Property]['parse']>;
|
|
46
49
|
}> {
|
|
47
50
|
}
|
|
48
|
-
type SchemaType = StringSchemaInterface | SchemaInterface<string, string> | SchemaInterface<string, string | undefined> | SchemaInterface<string, string | null> | SchemaInterface<string, string | undefined | null> | ObjectSchemaInterface<Record<string, SchemaType>> | SchemaInterface<object, object> | SchemaInterface<object, object | undefined> | SchemaInterface<object, object | null> | SchemaInterface<object, object | undefined | null> | NumberSchemaInterface | SchemaInterface<number, number> | SchemaInterface<number, number | undefined> | SchemaInterface<number, number | null> | SchemaInterface<number, number | undefined | null> | BooleanSchemaInterface | SchemaInterface<boolean, boolean> | SchemaInterface<boolean, boolean | undefined> | SchemaInterface<boolean, boolean | null> | SchemaInterface<boolean, boolean | undefined | null> | DateSchemaInterface | SchemaInterface<Date, Date> | SchemaInterface<Date, Date | undefined> | SchemaInterface<Date, Date | null> | SchemaInterface<Date, Date | undefined | null> | EnumSchemaInterface<string> | ArraySchemaInterface<StringSchemaInterface | ObjectSchemaInterface<Record<string, SchemaType>> | NumberSchemaInterface | BooleanSchemaInterface | DateSchemaInterface | EnumSchemaInterface<string>> | SchemaInterface<Array<unknown>, Array<unknown>> | SchemaInterface<Array<unknown>, Array<unknown> | undefined> | SchemaInterface<Array<unknown>, Array<unknown> | null> | SchemaInterface<Array<unknown>, Array<unknown> | undefined | null>;
|
|
51
|
+
type SchemaType = StringSchemaInterface | SchemaInterface<unknown, unknown> | SchemaInterface<string, string> | SchemaInterface<string, string | undefined> | SchemaInterface<string, string | null> | SchemaInterface<string, string | undefined | null> | ObjectSchemaInterface<Record<string, SchemaType>> | SchemaInterface<object, object> | SchemaInterface<object, object | undefined> | SchemaInterface<object, object | null> | SchemaInterface<object, object | undefined | null> | NumberSchemaInterface | SchemaInterface<number, number> | SchemaInterface<number, number | undefined> | SchemaInterface<number, number | null> | SchemaInterface<number, number | undefined | null> | BooleanSchemaInterface | SchemaInterface<boolean, boolean> | SchemaInterface<boolean, boolean | undefined> | SchemaInterface<boolean, boolean | null> | SchemaInterface<boolean, boolean | undefined | null> | DateSchemaInterface | SchemaInterface<Date, Date> | SchemaInterface<Date, Date | undefined> | SchemaInterface<Date, Date | null> | SchemaInterface<Date, Date | undefined | null> | EnumSchemaInterface<string> | UnionSchemaInterface<Array<SchemaInterface<unknown, unknown>>> | ArraySchemaInterface<StringSchemaInterface | ObjectSchemaInterface<Record<string, SchemaType>> | NumberSchemaInterface | BooleanSchemaInterface | DateSchemaInterface | EnumSchemaInterface<string>> | SchemaInterface<Array<unknown>, Array<unknown>> | SchemaInterface<Array<unknown>, Array<unknown> | undefined> | SchemaInterface<Array<unknown>, Array<unknown> | null> | SchemaInterface<Array<unknown>, Array<unknown> | undefined | null>;
|
|
52
|
+
type ErrorMessage = string | ((value: unknown) => string);
|
|
49
53
|
type ExtenderType = (inter: SchemaType, validation: Function, options: {
|
|
50
|
-
message:
|
|
54
|
+
message: ErrorMessage;
|
|
51
55
|
type: string;
|
|
52
56
|
}) => SchemaType;
|
|
57
|
+
interface CreateSchemaInterfaceOptions {
|
|
58
|
+
type?: string;
|
|
59
|
+
message?: (value: unknown) => string;
|
|
60
|
+
}
|
|
61
|
+
type SchemaInterfaceOptions = Omit<CreateSchemaInterfaceOptions, 'type'>;
|
|
53
62
|
declare const s: {
|
|
54
|
-
object<T extends Record<string, SchemaType>>(definition: { [Property in keyof T]: T[Property]; }): ObjectSchemaInterface<T>;
|
|
55
|
-
string(): StringSchemaInterface;
|
|
56
|
-
number(): NumberSchemaInterface;
|
|
57
|
-
boolean(): BooleanSchemaInterface;
|
|
58
|
-
date(): DateSchemaInterface;
|
|
59
|
-
enum(definition: Readonly<Array<string
|
|
60
|
-
array<T extends SchemaType>(definition: T): ArraySchemaInterface<T>;
|
|
63
|
+
object<T extends Record<string, SchemaType>>(definition: { [Property in keyof T]: T[Property]; }, options: SchemaInterfaceOptions): ObjectSchemaInterface<T>;
|
|
64
|
+
string(options: SchemaInterfaceOptions): StringSchemaInterface;
|
|
65
|
+
number(options: SchemaInterfaceOptions): NumberSchemaInterface;
|
|
66
|
+
boolean(options: SchemaInterfaceOptions): BooleanSchemaInterface;
|
|
67
|
+
date(options: SchemaInterfaceOptions): DateSchemaInterface;
|
|
68
|
+
enum(definition: Readonly<Array<string>>, options: SchemaInterfaceOptions): EnumSchemaInterface<(typeof definition)[number]>;
|
|
69
|
+
array<T extends SchemaType>(definition: T, options: SchemaInterfaceOptions): ArraySchemaInterface<T>;
|
|
61
70
|
any(): any;
|
|
62
71
|
preprocess<T extends SchemaType>(callback: Function, schema: T): T;
|
|
72
|
+
union<T extends Array<SchemaType>>(definitions: T, options: SchemaInterfaceOptions): UnionSchemaInterface<T>;
|
|
63
73
|
};
|
|
64
74
|
declare function extend(callback: ExtenderType): void;
|
|
65
75
|
type Infer<T> = T extends SchemaType ? ReturnType<T['parse']> : unknown;
|
|
66
76
|
|
|
67
|
-
export { type ArraySchemaInterface, type BooleanSchemaInterface, type DateSchemaInterface, type EnumSchemaInterface, type ExtenderType, type Infer, type NumberSchemaInterface, type ObjectSchemaInterface, type SchemaInterface, type SchemaType, type StringSchemaInterface, extend, s };
|
|
77
|
+
export { type ArraySchemaInterface, type BooleanSchemaInterface, type DateSchemaInterface, type EnumSchemaInterface, type ExtenderType, type Infer, type NumberSchemaInterface, type ObjectSchemaInterface, type SchemaInterface, type SchemaInterfaceOptions, type SchemaType, type StringSchemaInterface, type UnionSchemaInterface, extend, s };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';var h=
|
|
1
|
+
'use strict';var h=e=>typeof e=="string"||e instanceof String,I=e=>typeof e=="number"||e instanceof Number,l=e=>e===true||e===false,y=e=>e instanceof Date&&!Number.isNaN(e.getTime()),S=e=>Array.isArray(e),d=e=>typeof e=="object"&&e!==null&&!Array.isArray(e),g={object(e,c){let s=p(d,{...c,type:"object"});return i(s,"_parse",(u,...o)=>{let t=u(...o);if(t.success===false)return t;let n={};for(let r in e){let a=e[r]._parse(t.data[r]);if(a.success)n[r]=a.data;else {a=a;let f=a?.error?.cause?.key?`${r}.${a?.error?.cause?.key}`:r;return a.error.message=`Error parsing key "${f}": ${a.error.message}`,a.error.cause={key:f},a}}return {success:true,data:n}}),s},string(e){return p(h,{...e,type:"string"})},number(e){return p(I,{...e,type:"number"})},boolean(e){return p(l,{...e,type:"boolean"})},date(e){return p(y,{...e,type:"date"})},enum(e,c){let s=n=>e.includes(n),u=n=>`Invalid ${o} value. Expected ${e.map(r=>`"${r}"`).join(" | ")}, received "${n}".`,o="enum";return p(s,{message:u,...c,type:o})},array(e,c){let s=p(S,{...c,type:"array"});return i(s,"_parse",(u,...o)=>{let t=u(...o);if(t.success===false)return t;let n=[];for(let r=0;r<t.data.length;r++){let a=e._parse(t.data[r]);if(a.success)n.push(a.data);else {a=a;let f=a.error?.cause?.key?`${r}.${a.error.cause.key}`:`${r}`;return a.error.message=`Error parsing index "${r}": ${a.error.message}`,a.error.cause={key:f},a}}return {success:true,data:n}}),s},any(){return p(()=>true)},preprocess(e,c){return i(c,"_parse",(s,u)=>(u=e(u),s(u))),c},union(e,c){return p(t=>{for(let n=0;n<e.length;n++)if(e[n]._parse(t).success)return true;return false},{message:t=>`Invalid union value. Expected the value to match one of the schemas: ${e.map(n=>`"${n._getType()}"`).join(" | ")}, but received "${typeof t}" with value "${t}".`,...c,type:"union"})}};function T(e){return c=>`The value "${c}" must be type of ${e} but is type of "${typeof c}".`}function i(e,c,s){let u=e[c];e[c]=(...o)=>s(u,...o);}function p(e,{type:c="any",message:s}={}){s=s||T(c);let u={message:s,type:c},o={_getType(){return c},_parse(t){return e(t)?{success:true,data:t}:{success:false,error:{message:s(t)}}},parse(t){let n=o._parse(t);if(!n.success)throw n=n,new Error(n.error.message,{cause:n.error.cause});return n.data},safeParse(t){return o._parse(t)},transform(t){return i(this,"_parse",(n,r)=>{let a=n(r);return a.success&&(a.data=t(a.data)),a}),this},optional(){return i(this,"_parse",(t,n)=>{let r=t(n);return r.success||(r.data=void 0,r.success=true),r}),this},nullable(){return i(this,"_parse",(t,n)=>{let r=t(n);return r.success||(r.data=null,r.success=true),r}),this},nullish(){return i(this,"_parse",(t,n)=>{let r=t(n);return !r.success&&n==null&&(r.success=true,r.data=n),r}),this},default(t){return i(this,"_parse",(n,r)=>(r===void 0&&(r=t,r=typeof t=="function"?t():t),n(r))),this},pipe(t){return i(this,"_parse",(n,r)=>{let a=n(r);return a.success?t._parse(a.data):a}),this},refine(t,{message:n}){return i(this,"_parse",(r,a)=>{let f=r(a);return t(f.data)?f:{success:false,error:{message:n}}}),this}};return m.length>0?m.reduce((t,n)=>n(t,e,u)??t,o):o}var m=[];function b(e){m.push(e);}exports.extend=b;exports.s=g;
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var h=
|
|
1
|
+
var h=e=>typeof e=="string"||e instanceof String,I=e=>typeof e=="number"||e instanceof Number,l=e=>e===true||e===false,y=e=>e instanceof Date&&!Number.isNaN(e.getTime()),S=e=>Array.isArray(e),d=e=>typeof e=="object"&&e!==null&&!Array.isArray(e),g={object(e,c){let s=p(d,{...c,type:"object"});return i(s,"_parse",(u,...o)=>{let t=u(...o);if(t.success===false)return t;let n={};for(let r in e){let a=e[r]._parse(t.data[r]);if(a.success)n[r]=a.data;else {a=a;let f=a?.error?.cause?.key?`${r}.${a?.error?.cause?.key}`:r;return a.error.message=`Error parsing key "${f}": ${a.error.message}`,a.error.cause={key:f},a}}return {success:true,data:n}}),s},string(e){return p(h,{...e,type:"string"})},number(e){return p(I,{...e,type:"number"})},boolean(e){return p(l,{...e,type:"boolean"})},date(e){return p(y,{...e,type:"date"})},enum(e,c){let s=n=>e.includes(n),u=n=>`Invalid ${o} value. Expected ${e.map(r=>`"${r}"`).join(" | ")}, received "${n}".`,o="enum";return p(s,{message:u,...c,type:o})},array(e,c){let s=p(S,{...c,type:"array"});return i(s,"_parse",(u,...o)=>{let t=u(...o);if(t.success===false)return t;let n=[];for(let r=0;r<t.data.length;r++){let a=e._parse(t.data[r]);if(a.success)n.push(a.data);else {a=a;let f=a.error?.cause?.key?`${r}.${a.error.cause.key}`:`${r}`;return a.error.message=`Error parsing index "${r}": ${a.error.message}`,a.error.cause={key:f},a}}return {success:true,data:n}}),s},any(){return p(()=>true)},preprocess(e,c){return i(c,"_parse",(s,u)=>(u=e(u),s(u))),c},union(e,c){return p(t=>{for(let n=0;n<e.length;n++)if(e[n]._parse(t).success)return true;return false},{message:t=>`Invalid union value. Expected the value to match one of the schemas: ${e.map(n=>`"${n._getType()}"`).join(" | ")}, but received "${typeof t}" with value "${t}".`,...c,type:"union"})}};function T(e){return c=>`The value "${c}" must be type of ${e} but is type of "${typeof c}".`}function i(e,c,s){let u=e[c];e[c]=(...o)=>s(u,...o);}function p(e,{type:c="any",message:s}={}){s=s||T(c);let u={message:s,type:c},o={_getType(){return c},_parse(t){return e(t)?{success:true,data:t}:{success:false,error:{message:s(t)}}},parse(t){let n=o._parse(t);if(!n.success)throw n=n,new Error(n.error.message,{cause:n.error.cause});return n.data},safeParse(t){return o._parse(t)},transform(t){return i(this,"_parse",(n,r)=>{let a=n(r);return a.success&&(a.data=t(a.data)),a}),this},optional(){return i(this,"_parse",(t,n)=>{let r=t(n);return r.success||(r.data=void 0,r.success=true),r}),this},nullable(){return i(this,"_parse",(t,n)=>{let r=t(n);return r.success||(r.data=null,r.success=true),r}),this},nullish(){return i(this,"_parse",(t,n)=>{let r=t(n);return !r.success&&n==null&&(r.success=true,r.data=n),r}),this},default(t){return i(this,"_parse",(n,r)=>(r===void 0&&(r=t,r=typeof t=="function"?t():t),n(r))),this},pipe(t){return i(this,"_parse",(n,r)=>{let a=n(r);return a.success?t._parse(a.data):a}),this},refine(t,{message:n}){return i(this,"_parse",(r,a)=>{let f=r(a);return t(f.data)?f:{success:false,error:{message:n}}}),this}};return m.length>0?m.reduce((t,n)=>n(t,e,u)??t,o):o}var m=[];function b(e){m.push(e);}export{b as extend,g as s};
|