@manassandhu/mint 0.0.0-stage → 0.1.0
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/LICENSE +21 -0
- package/README.md +659 -2
- package/package.json +34 -4
- package/src/core/ParseContext.js +82 -0
- package/src/core/Schema.js +513 -0
- package/src/core/WrapperSchemas.js +14 -0
- package/src/core/index.js +15 -0
- package/src/errors/MintError.js +108 -0
- package/src/errors/index.js +5 -0
- package/src/index.js +208 -0
- package/src/schemas/ArraySchema.js +154 -0
- package/src/schemas/BigIntSchema.js +138 -0
- package/src/schemas/BooleanSchema.js +30 -0
- package/src/schemas/DateSchema.js +144 -0
- package/src/schemas/EnumSchema.js +69 -0
- package/src/schemas/FunctionSchema.js +96 -0
- package/src/schemas/InstanceSchema.js +52 -0
- package/src/schemas/IntersectionSchema.js +175 -0
- package/src/schemas/LiteralSchema.js +51 -0
- package/src/schemas/MapSchema.js +203 -0
- package/src/schemas/NumberSchema.js +169 -0
- package/src/schemas/ObjectSchema.js +336 -0
- package/src/schemas/PromiseSchema.js +82 -0
- package/src/schemas/RecordSchema.js +132 -0
- package/src/schemas/SetSchema.js +185 -0
- package/src/schemas/SpecialSchemas.js +156 -0
- package/src/schemas/StringSchema.js +222 -0
- package/src/schemas/TupleSchema.js +187 -0
- package/src/schemas/UnionSchema.js +108 -0
- package/src/schemas/index.js +32 -0
- package/src/utils/objectUtils.js +47 -0
package/src/index.js
ADDED
|
@@ -0,0 +1,208 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mint
|
|
3
|
+
*
|
|
4
|
+
* Elegant schema validation for JavaScript and TypeScript.
|
|
5
|
+
*
|
|
6
|
+
* Public API.
|
|
7
|
+
*/
|
|
8
|
+
|
|
9
|
+
import { StringSchema } from "./schemas/StringSchema.js";
|
|
10
|
+
import { NumberSchema } from "./schemas/NumberSchema.js";
|
|
11
|
+
import { BooleanSchema } from "./schemas/BooleanSchema.js";
|
|
12
|
+
import { BigIntSchema } from "./schemas/BigIntSchema.js";
|
|
13
|
+
import { DateSchema } from "./schemas/DateSchema.js";
|
|
14
|
+
|
|
15
|
+
import {
|
|
16
|
+
NullSchema,
|
|
17
|
+
UndefinedSchema,
|
|
18
|
+
VoidSchema,
|
|
19
|
+
AnySchema,
|
|
20
|
+
UnknownSchema,
|
|
21
|
+
NeverSchema,
|
|
22
|
+
SymbolSchema,
|
|
23
|
+
} from "./schemas/SpecialSchemas.js";
|
|
24
|
+
|
|
25
|
+
import { LiteralSchema } from "./schemas/LiteralSchema.js";
|
|
26
|
+
import { EnumSchema } from "./schemas/EnumSchema.js";
|
|
27
|
+
import { ArraySchema } from "./schemas/ArraySchema.js";
|
|
28
|
+
import { ObjectSchema } from "./schemas/ObjectSchema.js";
|
|
29
|
+
import { TupleSchema } from "./schemas/TupleSchema.js";
|
|
30
|
+
import { RecordSchema } from "./schemas/RecordSchema.js";
|
|
31
|
+
import { UnionSchema } from "./schemas/UnionSchema.js";
|
|
32
|
+
import { IntersectionSchema } from "./schemas/IntersectionSchema.js";
|
|
33
|
+
import { SetSchema } from "./schemas/SetSchema.js";
|
|
34
|
+
import { MapSchema } from "./schemas/MapSchema.js";
|
|
35
|
+
import { PromiseSchema } from "./schemas/PromiseSchema.js";
|
|
36
|
+
import { FunctionSchema } from "./schemas/FunctionSchema.js";
|
|
37
|
+
import { InstanceSchema } from "./schemas/InstanceSchema.js";
|
|
38
|
+
|
|
39
|
+
/* ================================================
|
|
40
|
+
Schema Factories
|
|
41
|
+
================================================ */
|
|
42
|
+
|
|
43
|
+
export const string = () => new StringSchema();
|
|
44
|
+
|
|
45
|
+
export const number = () => new NumberSchema();
|
|
46
|
+
|
|
47
|
+
export const boolean = () => new BooleanSchema();
|
|
48
|
+
|
|
49
|
+
export const bigint = () => new BigIntSchema();
|
|
50
|
+
|
|
51
|
+
export const date = () => new DateSchema();
|
|
52
|
+
|
|
53
|
+
export const nullType = () => new NullSchema();
|
|
54
|
+
|
|
55
|
+
export const undefinedType = () => new UndefinedSchema();
|
|
56
|
+
|
|
57
|
+
export const voidType = () => new VoidSchema();
|
|
58
|
+
|
|
59
|
+
export const any = () => new AnySchema();
|
|
60
|
+
|
|
61
|
+
export const unknown = () => new UnknownSchema();
|
|
62
|
+
|
|
63
|
+
export const never = () => new NeverSchema();
|
|
64
|
+
|
|
65
|
+
export const symbol = () => new SymbolSchema();
|
|
66
|
+
|
|
67
|
+
export const literal = (value) =>
|
|
68
|
+
new LiteralSchema(value);
|
|
69
|
+
|
|
70
|
+
export const enumType = (values) =>
|
|
71
|
+
new EnumSchema(values);
|
|
72
|
+
|
|
73
|
+
export const array = (schema) =>
|
|
74
|
+
new ArraySchema(schema);
|
|
75
|
+
|
|
76
|
+
export const object = (shape) =>
|
|
77
|
+
new ObjectSchema(shape);
|
|
78
|
+
|
|
79
|
+
export const tuple = (schemas) =>
|
|
80
|
+
new TupleSchema(schemas);
|
|
81
|
+
|
|
82
|
+
export const record = (
|
|
83
|
+
valueSchema,
|
|
84
|
+
keySchema = null
|
|
85
|
+
) =>
|
|
86
|
+
new RecordSchema(valueSchema, keySchema);
|
|
87
|
+
|
|
88
|
+
export const union = (schemas) =>
|
|
89
|
+
new UnionSchema(schemas);
|
|
90
|
+
|
|
91
|
+
export const intersection = (
|
|
92
|
+
leftSchema,
|
|
93
|
+
rightSchema
|
|
94
|
+
) =>
|
|
95
|
+
new IntersectionSchema(
|
|
96
|
+
leftSchema,
|
|
97
|
+
rightSchema
|
|
98
|
+
);
|
|
99
|
+
|
|
100
|
+
export const set = (schema) =>
|
|
101
|
+
new SetSchema(schema);
|
|
102
|
+
|
|
103
|
+
export const map = (
|
|
104
|
+
keySchema,
|
|
105
|
+
valueSchema
|
|
106
|
+
) =>
|
|
107
|
+
new MapSchema(keySchema, valueSchema);
|
|
108
|
+
|
|
109
|
+
export const promise = (schema) =>
|
|
110
|
+
new PromiseSchema(schema);
|
|
111
|
+
|
|
112
|
+
export const functionType = () =>
|
|
113
|
+
new FunctionSchema();
|
|
114
|
+
|
|
115
|
+
export const instance = (constructor) =>
|
|
116
|
+
new InstanceSchema(constructor);
|
|
117
|
+
|
|
118
|
+
/* ================================================
|
|
119
|
+
Mint Namespace
|
|
120
|
+
================================================ */
|
|
121
|
+
|
|
122
|
+
export const mint = Object.freeze({
|
|
123
|
+
string,
|
|
124
|
+
number,
|
|
125
|
+
boolean,
|
|
126
|
+
bigint,
|
|
127
|
+
date,
|
|
128
|
+
|
|
129
|
+
null: nullType,
|
|
130
|
+
undefined: undefinedType,
|
|
131
|
+
void: voidType,
|
|
132
|
+
|
|
133
|
+
any,
|
|
134
|
+
unknown,
|
|
135
|
+
never,
|
|
136
|
+
symbol,
|
|
137
|
+
|
|
138
|
+
literal,
|
|
139
|
+
enum: enumType,
|
|
140
|
+
|
|
141
|
+
array,
|
|
142
|
+
object,
|
|
143
|
+
tuple,
|
|
144
|
+
record,
|
|
145
|
+
|
|
146
|
+
union,
|
|
147
|
+
intersection,
|
|
148
|
+
|
|
149
|
+
set,
|
|
150
|
+
map,
|
|
151
|
+
|
|
152
|
+
promise,
|
|
153
|
+
function: functionType,
|
|
154
|
+
instance,
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
/* ================================================
|
|
158
|
+
Public Exports
|
|
159
|
+
================================================ */
|
|
160
|
+
|
|
161
|
+
export {
|
|
162
|
+
StringSchema,
|
|
163
|
+
NumberSchema,
|
|
164
|
+
BooleanSchema,
|
|
165
|
+
BigIntSchema,
|
|
166
|
+
DateSchema,
|
|
167
|
+
|
|
168
|
+
NullSchema,
|
|
169
|
+
UndefinedSchema,
|
|
170
|
+
VoidSchema,
|
|
171
|
+
AnySchema,
|
|
172
|
+
UnknownSchema,
|
|
173
|
+
NeverSchema,
|
|
174
|
+
SymbolSchema,
|
|
175
|
+
|
|
176
|
+
LiteralSchema,
|
|
177
|
+
EnumSchema,
|
|
178
|
+
ArraySchema,
|
|
179
|
+
ObjectSchema,
|
|
180
|
+
TupleSchema,
|
|
181
|
+
RecordSchema,
|
|
182
|
+
UnionSchema,
|
|
183
|
+
IntersectionSchema,
|
|
184
|
+
SetSchema,
|
|
185
|
+
MapSchema,
|
|
186
|
+
PromiseSchema,
|
|
187
|
+
FunctionSchema,
|
|
188
|
+
InstanceSchema,
|
|
189
|
+
};
|
|
190
|
+
|
|
191
|
+
export {
|
|
192
|
+
Schema,
|
|
193
|
+
OptionalSchema,
|
|
194
|
+
NullableSchema,
|
|
195
|
+
DefaultSchema,
|
|
196
|
+
RefinementSchema,
|
|
197
|
+
TransformSchema,
|
|
198
|
+
PipeSchema,
|
|
199
|
+
MetadataSchema,
|
|
200
|
+
} from "./core/index.js";
|
|
201
|
+
|
|
202
|
+
export {
|
|
203
|
+
MintError,
|
|
204
|
+
createIssue,
|
|
205
|
+
formatIssue,
|
|
206
|
+
} from "./errors/index.js";
|
|
207
|
+
|
|
208
|
+
export default mint;
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mint - Array Schema
|
|
3
|
+
*
|
|
4
|
+
* Validates arrays and their individual elements.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import { Schema } from "../core/Schema.js";
|
|
8
|
+
|
|
9
|
+
export class ArraySchema extends Schema {
|
|
10
|
+
constructor(elementSchema, checks = []) {
|
|
11
|
+
super({
|
|
12
|
+
type: "array",
|
|
13
|
+
element: elementSchema,
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
this.elementSchema = elementSchema;
|
|
17
|
+
this.checks = [...checks];
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
_addCheck(check) {
|
|
21
|
+
return new ArraySchema(this.elementSchema, [...this.checks, check]);
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
_parse(context, value) {
|
|
25
|
+
if (!Array.isArray(value)) {
|
|
26
|
+
context.addIssue({
|
|
27
|
+
code: "invalid_type",
|
|
28
|
+
expected: "array",
|
|
29
|
+
received: getType(value),
|
|
30
|
+
message: `Expected array, received ${getType(value)}`,
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
return value;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
this._validateChecks(context, value);
|
|
37
|
+
|
|
38
|
+
const parsed = [];
|
|
39
|
+
|
|
40
|
+
for (let index = 0; index < value.length; index++) {
|
|
41
|
+
const childContext = context.child(index, value[index]);
|
|
42
|
+
|
|
43
|
+
const parsedValue = this.elementSchema._parse(childContext, value[index]);
|
|
44
|
+
|
|
45
|
+
if (isPromise(parsedValue)) {
|
|
46
|
+
throw new Error(
|
|
47
|
+
"Asynchronous array element validation requires async parsing.",
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
parsed.push(parsedValue);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return parsed;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
async _parseAsync(context, value) {
|
|
58
|
+
if (!Array.isArray(value)) {
|
|
59
|
+
context.addIssue({
|
|
60
|
+
code: "invalid_type",
|
|
61
|
+
expected: "array",
|
|
62
|
+
received: getType(value),
|
|
63
|
+
message: `Expected array, received ${getType(value)}`,
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
return value;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
this._validateChecks(context, value);
|
|
70
|
+
|
|
71
|
+
const parsed = [];
|
|
72
|
+
|
|
73
|
+
for (let index = 0; index < value.length; index++) {
|
|
74
|
+
const childContext = context.child(index, value[index]);
|
|
75
|
+
|
|
76
|
+
const parsedValue = await this.elementSchema._parseAsync(
|
|
77
|
+
childContext,
|
|
78
|
+
value[index],
|
|
79
|
+
);
|
|
80
|
+
|
|
81
|
+
parsed.push(parsedValue);
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
return parsed;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
_validateChecks(context, value) {
|
|
88
|
+
for (const check of this.checks) {
|
|
89
|
+
if (!check.validate(value)) {
|
|
90
|
+
context.addIssue({
|
|
91
|
+
code: check.code,
|
|
92
|
+
message: check.message,
|
|
93
|
+
...check.metadata,
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
min(value, message) {
|
|
100
|
+
return this._addCheck({
|
|
101
|
+
code: "too_small",
|
|
102
|
+
validate: (input) => input.length >= value,
|
|
103
|
+
message: message ?? `Array must contain at least ${value} element(s)`,
|
|
104
|
+
metadata: {
|
|
105
|
+
minimum: value,
|
|
106
|
+
inclusive: true,
|
|
107
|
+
type: "array",
|
|
108
|
+
},
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
max(value, message) {
|
|
113
|
+
return this._addCheck({
|
|
114
|
+
code: "too_big",
|
|
115
|
+
validate: (input) => input.length <= value,
|
|
116
|
+
message: message ?? `Array must contain at most ${value} element(s)`,
|
|
117
|
+
metadata: {
|
|
118
|
+
maximum: value,
|
|
119
|
+
inclusive: true,
|
|
120
|
+
type: "array",
|
|
121
|
+
},
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
length(value, message) {
|
|
126
|
+
return this._addCheck({
|
|
127
|
+
code: "invalid_length",
|
|
128
|
+
validate: (input) => input.length === value,
|
|
129
|
+
message: message ?? `Array must contain exactly ${value} element(s)`,
|
|
130
|
+
metadata: {
|
|
131
|
+
length: value,
|
|
132
|
+
type: "array",
|
|
133
|
+
},
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
nonempty(message) {
|
|
138
|
+
return this.min(1, message ?? "Array cannot be empty");
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function getType(value) {
|
|
143
|
+
if (value === null) return "null";
|
|
144
|
+
if (Array.isArray(value)) return "array";
|
|
145
|
+
return typeof value;
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function isPromise(value) {
|
|
149
|
+
return (
|
|
150
|
+
value !== null &&
|
|
151
|
+
(typeof value === "object" || typeof value === "function") &&
|
|
152
|
+
typeof value.then === "function"
|
|
153
|
+
);
|
|
154
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mint - BigInt Schema
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { Schema } from "../core/Schema.js";
|
|
6
|
+
|
|
7
|
+
export class BigIntSchema extends Schema {
|
|
8
|
+
constructor(checks = []) {
|
|
9
|
+
super({ type: "bigint" });
|
|
10
|
+
this.checks = [...checks];
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
_addCheck(check) {
|
|
14
|
+
return new BigIntSchema([...this.checks, check]);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
_parse(context, value) {
|
|
18
|
+
if (typeof value !== "bigint") {
|
|
19
|
+
context.addIssue({
|
|
20
|
+
code: "invalid_type",
|
|
21
|
+
expected: "bigint",
|
|
22
|
+
received: getType(value),
|
|
23
|
+
message: `Expected bigint, received ${getType(value)}`,
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
return value;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
for (const check of this.checks) {
|
|
30
|
+
if (!check.validate(value)) {
|
|
31
|
+
context.addIssue({
|
|
32
|
+
code: check.code,
|
|
33
|
+
message: check.message,
|
|
34
|
+
...check.metadata,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return value;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
min(value, message) {
|
|
43
|
+
return this._addCheck({
|
|
44
|
+
code: "too_small",
|
|
45
|
+
validate: (input) => input >= value,
|
|
46
|
+
message: message ?? `BigInt must be greater than or equal to ${value}`,
|
|
47
|
+
metadata: {
|
|
48
|
+
minimum: value,
|
|
49
|
+
inclusive: true,
|
|
50
|
+
type: "bigint",
|
|
51
|
+
},
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
max(value, message) {
|
|
56
|
+
return this._addCheck({
|
|
57
|
+
code: "too_big",
|
|
58
|
+
validate: (input) => input <= value,
|
|
59
|
+
message: message ?? `BigInt must be less than or equal to ${value}`,
|
|
60
|
+
metadata: {
|
|
61
|
+
maximum: value,
|
|
62
|
+
inclusive: true,
|
|
63
|
+
type: "bigint",
|
|
64
|
+
},
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
gt(value, message) {
|
|
69
|
+
return this._addCheck({
|
|
70
|
+
code: "too_small",
|
|
71
|
+
validate: (input) => input > value,
|
|
72
|
+
message: message ?? `BigInt must be greater than ${value}`,
|
|
73
|
+
metadata: {
|
|
74
|
+
minimum: value,
|
|
75
|
+
inclusive: false,
|
|
76
|
+
type: "bigint",
|
|
77
|
+
},
|
|
78
|
+
});
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
gte(value, message) {
|
|
82
|
+
return this.min(value, message);
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
lt(value, message) {
|
|
86
|
+
return this._addCheck({
|
|
87
|
+
code: "too_big",
|
|
88
|
+
validate: (input) => input < value,
|
|
89
|
+
message: message ?? `BigInt must be less than ${value}`,
|
|
90
|
+
metadata: {
|
|
91
|
+
maximum: value,
|
|
92
|
+
inclusive: false,
|
|
93
|
+
type: "bigint",
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
lte(value, message) {
|
|
99
|
+
return this.max(value, message);
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
positive(message) {
|
|
103
|
+
return this.gt(0n, message);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
negative(message) {
|
|
107
|
+
return this.lt(0n, message);
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
nonnegative(message) {
|
|
111
|
+
return this.gte(0n, message);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
nonpositive(message) {
|
|
115
|
+
return this.lte(0n, message);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
multipleOf(value, message) {
|
|
119
|
+
if (value === 0n) {
|
|
120
|
+
throw new Error("multipleOf() divisor cannot be zero.");
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
return this._addCheck({
|
|
124
|
+
code: "not_multiple_of",
|
|
125
|
+
validate: (input) => input % value === 0n,
|
|
126
|
+
message: message ?? `BigInt must be a multiple of ${value}`,
|
|
127
|
+
metadata: {
|
|
128
|
+
multipleOf: value,
|
|
129
|
+
},
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function getType(value) {
|
|
135
|
+
if (value === null) return "null";
|
|
136
|
+
if (Array.isArray(value)) return "array";
|
|
137
|
+
return typeof value;
|
|
138
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mint - Boolean Schema
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { Schema } from "../core/Schema.js";
|
|
6
|
+
|
|
7
|
+
export class BooleanSchema extends Schema {
|
|
8
|
+
constructor() {
|
|
9
|
+
super({ type: "boolean" });
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
_parse(context, value) {
|
|
13
|
+
if (typeof value !== "boolean") {
|
|
14
|
+
context.addIssue({
|
|
15
|
+
code: "invalid_type",
|
|
16
|
+
expected: "boolean",
|
|
17
|
+
received: getType(value),
|
|
18
|
+
message: `Expected boolean, received ${getType(value)}`,
|
|
19
|
+
});
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return value;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function getType(value) {
|
|
27
|
+
if (value === null) return "null";
|
|
28
|
+
if (Array.isArray(value)) return "array";
|
|
29
|
+
return typeof value;
|
|
30
|
+
}
|
|
@@ -0,0 +1,144 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mint - Date Schema
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { Schema } from "../core/Schema.js";
|
|
6
|
+
|
|
7
|
+
export class DateSchema extends Schema {
|
|
8
|
+
constructor(checks = []) {
|
|
9
|
+
super({ type: "date" });
|
|
10
|
+
this.checks = [...checks];
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
_addCheck(check) {
|
|
14
|
+
return new DateSchema([...this.checks, check]);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
_parse(context, value) {
|
|
18
|
+
if (!(value instanceof Date) || Number.isNaN(value.getTime())) {
|
|
19
|
+
context.addIssue({
|
|
20
|
+
code: "invalid_type",
|
|
21
|
+
expected: "date",
|
|
22
|
+
received: getType(value),
|
|
23
|
+
message: `Expected valid Date, received ${getType(value)}`,
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
return value;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
for (const check of this.checks) {
|
|
30
|
+
if (!check.validate(value)) {
|
|
31
|
+
context.addIssue({
|
|
32
|
+
code: check.code,
|
|
33
|
+
message: check.message,
|
|
34
|
+
...check.metadata,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
return value;
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
min(minimum, message) {
|
|
43
|
+
const date = normalizeDate(minimum);
|
|
44
|
+
|
|
45
|
+
if (!date) {
|
|
46
|
+
throw new Error("date.min() requires a valid Date.");
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
return this._addCheck({
|
|
50
|
+
code: "too_small",
|
|
51
|
+
validate: (input) => input.getTime() >= date.getTime(),
|
|
52
|
+
message:
|
|
53
|
+
message ??
|
|
54
|
+
`Date must be greater than or equal to ${date.toISOString()}`,
|
|
55
|
+
metadata: {
|
|
56
|
+
minimum: date,
|
|
57
|
+
inclusive: true,
|
|
58
|
+
type: "date",
|
|
59
|
+
},
|
|
60
|
+
});
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
max(maximum, message) {
|
|
64
|
+
const date = normalizeDate(maximum);
|
|
65
|
+
|
|
66
|
+
if (!date) {
|
|
67
|
+
throw new Error("date.max() requires a valid Date.");
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
return this._addCheck({
|
|
71
|
+
code: "too_big",
|
|
72
|
+
validate: (input) => input.getTime() <= date.getTime(),
|
|
73
|
+
message:
|
|
74
|
+
message ?? `Date must be less than or equal to ${date.toISOString()}`,
|
|
75
|
+
metadata: {
|
|
76
|
+
maximum: date,
|
|
77
|
+
inclusive: true,
|
|
78
|
+
type: "date",
|
|
79
|
+
},
|
|
80
|
+
});
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
gt(minimum, message) {
|
|
84
|
+
const date = normalizeDate(minimum);
|
|
85
|
+
|
|
86
|
+
if (!date) {
|
|
87
|
+
throw new Error("date.gt() requires a valid Date.");
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
return this._addCheck({
|
|
91
|
+
code: "too_small",
|
|
92
|
+
validate: (input) => input.getTime() > date.getTime(),
|
|
93
|
+
message: message ?? `Date must be after ${date.toISOString()}`,
|
|
94
|
+
metadata: {
|
|
95
|
+
minimum: date,
|
|
96
|
+
inclusive: false,
|
|
97
|
+
type: "date",
|
|
98
|
+
},
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
lt(maximum, message) {
|
|
103
|
+
const date = normalizeDate(maximum);
|
|
104
|
+
|
|
105
|
+
if (!date) {
|
|
106
|
+
throw new Error("date.lt() requires a valid Date.");
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
return this._addCheck({
|
|
110
|
+
code: "too_big",
|
|
111
|
+
validate: (input) => input.getTime() < date.getTime(),
|
|
112
|
+
message: message ?? `Date must be before ${date.toISOString()}`,
|
|
113
|
+
metadata: {
|
|
114
|
+
maximum: date,
|
|
115
|
+
inclusive: false,
|
|
116
|
+
type: "date",
|
|
117
|
+
},
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
gte(minimum, message) {
|
|
122
|
+
return this.min(minimum, message);
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
lte(maximum, message) {
|
|
126
|
+
return this.max(maximum, message);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function normalizeDate(value) {
|
|
131
|
+
const date =
|
|
132
|
+
value instanceof Date ? new Date(value.getTime()) : new Date(value);
|
|
133
|
+
|
|
134
|
+
return Number.isNaN(date.getTime()) ? null : date;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function getType(value) {
|
|
138
|
+
if (value === null) return "null";
|
|
139
|
+
if (Array.isArray(value)) return "array";
|
|
140
|
+
if (value instanceof Date) {
|
|
141
|
+
return Number.isNaN(value.getTime()) ? "invalid date" : "date";
|
|
142
|
+
}
|
|
143
|
+
return typeof value;
|
|
144
|
+
}
|