typetify 2.0.0 → 2.2.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/CHANGELOG.md +70 -0
- package/README.md +108 -0
- package/dist/async/index.js +1 -49
- package/dist/async/index.mjs +1 -4
- package/dist/chunk-272Z3AXV.js +1 -0
- package/dist/chunk-4HQERWI6.mjs +1 -0
- package/dist/chunk-4JG7CFRK.js +1 -0
- package/dist/chunk-5FBZYPQM.mjs +1 -0
- package/dist/chunk-5LWNMYHB.js +1 -0
- package/dist/chunk-5XQRYI2Q.mjs +1 -0
- package/dist/chunk-6HA2SN23.js +1 -0
- package/dist/chunk-6NWIIOHH.js +1 -0
- package/dist/chunk-7LBL6CYT.js +1 -0
- package/dist/chunk-7V3EDYJD.mjs +1 -0
- package/dist/chunk-B3R73UK5.mjs +1 -0
- package/dist/chunk-EAHFED6V.mjs +1 -0
- package/dist/chunk-EKSHS4DL.mjs +1 -0
- package/dist/chunk-FYQNKDT3.mjs +1 -0
- package/dist/chunk-GKOA3XGQ.js +1 -0
- package/dist/chunk-I4YSQ4HB.js +1 -0
- package/dist/chunk-JZXLCA2E.mjs +1 -0
- package/dist/chunk-M5OMYDI7.mjs +1 -0
- package/dist/chunk-MPY3WRLG.mjs +1 -0
- package/dist/chunk-MSOFO6QN.js +1 -0
- package/dist/chunk-NENRAPOL.mjs +1 -0
- package/dist/chunk-NOW4Q26G.mjs +1 -0
- package/dist/chunk-P4UNM7SF.js +1 -0
- package/dist/chunk-P5E2JDCX.mjs +1 -0
- package/dist/chunk-Q7SUL7MD.js +1 -0
- package/dist/chunk-QKNZICUO.mjs +1 -0
- package/dist/chunk-RHMANSTA.mjs +1 -0
- package/dist/chunk-SF64HETW.js +1 -0
- package/dist/chunk-T66YO63J.js +1 -0
- package/dist/chunk-TNK6EVQK.mjs +1 -0
- package/dist/chunk-TSJFKV3R.js +1 -0
- package/dist/chunk-TVCWWECJ.mjs +1 -0
- package/dist/chunk-W5I352FJ.js +1 -0
- package/dist/chunk-WAT4QJB2.js +1 -0
- package/dist/chunk-WCG5ODRX.mjs +1 -0
- package/dist/chunk-WDFIRO2J.js +1 -0
- package/dist/chunk-WOT6VMZA.js +1 -0
- package/dist/chunk-WQPSXQT5.js +1 -0
- package/dist/chunk-WS743BFA.js +1 -0
- package/dist/chunk-XPY5CWKZ.mjs +1 -0
- package/dist/collection/index.js +1 -125
- package/dist/collection/index.mjs +1 -4
- package/dist/core/index.js +1 -41
- package/dist/core/index.mjs +1 -4
- package/dist/decorator/index.js +1 -57
- package/dist/decorator/index.mjs +1 -4
- package/dist/dx/index.js +1 -53
- package/dist/dx/index.mjs +1 -4
- package/dist/flow/index.js +1 -50
- package/dist/flow/index.mjs +1 -5
- package/dist/fn/index.js +1 -37
- package/dist/fn/index.mjs +1 -4
- package/dist/guards/index.js +1 -233
- package/dist/guards/index.mjs +1 -4
- package/dist/index.d.mts +143 -24
- package/dist/index.d.ts +143 -24
- package/dist/index.js +3 -2498
- package/dist/index.mjs +3 -1537
- package/dist/input/index.js +1 -74
- package/dist/input/index.mjs +1 -5
- package/dist/iterator/index.js +1 -522
- package/dist/iterator/index.mjs +1 -502
- package/dist/logic/index.js +1 -416
- package/dist/logic/index.mjs +1 -367
- package/dist/math/index.js +1 -45
- package/dist/math/index.mjs +1 -4
- package/dist/narrowing/index.js +1 -220
- package/dist/narrowing/index.mjs +1 -186
- package/dist/object/index.js +1 -113
- package/dist/object/index.mjs +1 -4
- package/dist/result/index.js +1 -86
- package/dist/result/index.mjs +1 -5
- package/dist/schema/index.js +1 -410
- package/dist/schema/index.mjs +1 -384
- package/dist/string/index.js +1 -49
- package/dist/string/index.mjs +1 -4
- package/dist/typed/index.js +1 -193
- package/dist/typed/index.mjs +1 -4
- package/package.json +8 -3
- package/dist/async/index.js.map +0 -1
- package/dist/async/index.mjs.map +0 -1
- package/dist/chunk-2LJ6NZ6K.js +0 -108
- package/dist/chunk-2LJ6NZ6K.js.map +0 -1
- package/dist/chunk-4NXETABV.mjs +0 -116
- package/dist/chunk-4NXETABV.mjs.map +0 -1
- package/dist/chunk-6ZBTL74K.js +0 -129
- package/dist/chunk-6ZBTL74K.js.map +0 -1
- package/dist/chunk-CN3GYRJN.mjs +0 -137
- package/dist/chunk-CN3GYRJN.mjs.map +0 -1
- package/dist/chunk-CNTE6ZVH.js +0 -359
- package/dist/chunk-CNTE6ZVH.js.map +0 -1
- package/dist/chunk-DBENOSTA.js +0 -156
- package/dist/chunk-DBENOSTA.js.map +0 -1
- package/dist/chunk-DWIG5GF2.js +0 -135
- package/dist/chunk-DWIG5GF2.js.map +0 -1
- package/dist/chunk-EAUTTWTQ.mjs +0 -231
- package/dist/chunk-EAUTTWTQ.mjs.map +0 -1
- package/dist/chunk-FXWYPHA3.mjs +0 -13
- package/dist/chunk-FXWYPHA3.mjs.map +0 -1
- package/dist/chunk-GS3PP67B.js +0 -200
- package/dist/chunk-GS3PP67B.js.map +0 -1
- package/dist/chunk-IGA3VEZM.mjs +0 -503
- package/dist/chunk-IGA3VEZM.mjs.map +0 -1
- package/dist/chunk-J5LGTIGS.mjs +0 -9
- package/dist/chunk-J5LGTIGS.mjs.map +0 -1
- package/dist/chunk-JAOGY4JO.mjs +0 -1007
- package/dist/chunk-JAOGY4JO.mjs.map +0 -1
- package/dist/chunk-JB6UXRKD.mjs +0 -97
- package/dist/chunk-JB6UXRKD.mjs.map +0 -1
- package/dist/chunk-JQAQV75V.js +0 -561
- package/dist/chunk-JQAQV75V.js.map +0 -1
- package/dist/chunk-JQHUBZ4M.js +0 -88
- package/dist/chunk-JQHUBZ4M.js.map +0 -1
- package/dist/chunk-LT7JK7RJ.js +0 -87
- package/dist/chunk-LT7JK7RJ.js.map +0 -1
- package/dist/chunk-OEJK37LO.mjs +0 -328
- package/dist/chunk-OEJK37LO.mjs.map +0 -1
- package/dist/chunk-OPVES6W2.js +0 -16
- package/dist/chunk-OPVES6W2.js.map +0 -1
- package/dist/chunk-OWNUKWXV.js +0 -291
- package/dist/chunk-OWNUKWXV.js.map +0 -1
- package/dist/chunk-PQTXSQ4P.js +0 -369
- package/dist/chunk-PQTXSQ4P.js.map +0 -1
- package/dist/chunk-PZ5AY32C.js +0 -11
- package/dist/chunk-PZ5AY32C.js.map +0 -1
- package/dist/chunk-QFR7DVAJ.mjs +0 -63
- package/dist/chunk-QFR7DVAJ.mjs.map +0 -1
- package/dist/chunk-SGQNLTRK.js +0 -73
- package/dist/chunk-SGQNLTRK.js.map +0 -1
- package/dist/chunk-SIA5BSVY.js +0 -1054
- package/dist/chunk-SIA5BSVY.js.map +0 -1
- package/dist/chunk-SRDWUHDY.mjs +0 -188
- package/dist/chunk-SRDWUHDY.mjs.map +0 -1
- package/dist/chunk-TXU7NTT4.js +0 -249
- package/dist/chunk-TXU7NTT4.js.map +0 -1
- package/dist/chunk-TZEWREAC.mjs +0 -277
- package/dist/chunk-TZEWREAC.mjs.map +0 -1
- package/dist/chunk-V6CWFDIJ.mjs +0 -123
- package/dist/chunk-V6CWFDIJ.mjs.map +0 -1
- package/dist/chunk-YBJC5WMX.mjs +0 -341
- package/dist/chunk-YBJC5WMX.mjs.map +0 -1
- package/dist/chunk-YOPAXITF.mjs +0 -75
- package/dist/chunk-YOPAXITF.mjs.map +0 -1
- package/dist/chunk-ZE4FDBRI.mjs +0 -79
- package/dist/chunk-ZE4FDBRI.mjs.map +0 -1
- package/dist/collection/index.js.map +0 -1
- package/dist/collection/index.mjs.map +0 -1
- package/dist/core/index.js.map +0 -1
- package/dist/core/index.mjs.map +0 -1
- package/dist/decorator/index.js.map +0 -1
- package/dist/decorator/index.mjs.map +0 -1
- package/dist/dx/index.js.map +0 -1
- package/dist/dx/index.mjs.map +0 -1
- package/dist/flow/index.js.map +0 -1
- package/dist/flow/index.mjs.map +0 -1
- package/dist/fn/index.js.map +0 -1
- package/dist/fn/index.mjs.map +0 -1
- package/dist/guards/index.js.map +0 -1
- package/dist/guards/index.mjs.map +0 -1
- package/dist/index.js.map +0 -1
- package/dist/index.mjs.map +0 -1
- package/dist/input/index.js.map +0 -1
- package/dist/input/index.mjs.map +0 -1
- package/dist/iterator/index.js.map +0 -1
- package/dist/iterator/index.mjs.map +0 -1
- package/dist/logic/index.js.map +0 -1
- package/dist/logic/index.mjs.map +0 -1
- package/dist/math/index.js.map +0 -1
- package/dist/math/index.mjs.map +0 -1
- package/dist/narrowing/index.js.map +0 -1
- package/dist/narrowing/index.mjs.map +0 -1
- package/dist/object/index.js.map +0 -1
- package/dist/object/index.mjs.map +0 -1
- package/dist/result/index.js.map +0 -1
- package/dist/result/index.mjs.map +0 -1
- package/dist/schema/index.js.map +0 -1
- package/dist/schema/index.mjs.map +0 -1
- package/dist/string/index.js.map +0 -1
- package/dist/string/index.mjs.map +0 -1
- package/dist/typed/index.js.map +0 -1
- package/dist/typed/index.mjs.map +0 -1
package/dist/schema/index.js
CHANGED
|
@@ -1,410 +1 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
var chunkOPVES6W2_js = require('../chunk-OPVES6W2.js');
|
|
4
|
-
require('../chunk-PZ5AY32C.js');
|
|
5
|
-
|
|
6
|
-
// src/schema/base.ts
|
|
7
|
-
function createSchema(tag, validate2) {
|
|
8
|
-
const schema = {
|
|
9
|
-
_type: void 0,
|
|
10
|
-
_tag: tag,
|
|
11
|
-
_validate: validate2,
|
|
12
|
-
parse(value) {
|
|
13
|
-
const result = this._validate(value, []);
|
|
14
|
-
if (result.ok) {
|
|
15
|
-
return result.value;
|
|
16
|
-
}
|
|
17
|
-
const errors = result.error;
|
|
18
|
-
const message = errors.map(
|
|
19
|
-
(e) => e.path.length > 0 ? `${e.path.join(".")}: ${e.message}` : e.message
|
|
20
|
-
).join("; ");
|
|
21
|
-
throw new TypeError(`Validation failed: ${message}`);
|
|
22
|
-
},
|
|
23
|
-
safeParse(value) {
|
|
24
|
-
return this._validate(value, []);
|
|
25
|
-
},
|
|
26
|
-
is(value) {
|
|
27
|
-
return this._validate(value, []).ok;
|
|
28
|
-
},
|
|
29
|
-
optional() {
|
|
30
|
-
return createSchema(
|
|
31
|
-
`${tag} | undefined`,
|
|
32
|
-
(value, path) => {
|
|
33
|
-
if (value === void 0) {
|
|
34
|
-
return chunkOPVES6W2_js.ok(void 0);
|
|
35
|
-
}
|
|
36
|
-
return validate2(value, path);
|
|
37
|
-
}
|
|
38
|
-
);
|
|
39
|
-
},
|
|
40
|
-
nullable() {
|
|
41
|
-
return createSchema(
|
|
42
|
-
`${tag} | null`,
|
|
43
|
-
(value, path) => {
|
|
44
|
-
if (value === null) {
|
|
45
|
-
return chunkOPVES6W2_js.ok(null);
|
|
46
|
-
}
|
|
47
|
-
return validate2(value, path);
|
|
48
|
-
}
|
|
49
|
-
);
|
|
50
|
-
},
|
|
51
|
-
default(defaultValue) {
|
|
52
|
-
return createSchema(
|
|
53
|
-
tag,
|
|
54
|
-
(value, path) => {
|
|
55
|
-
if (value === void 0) {
|
|
56
|
-
return chunkOPVES6W2_js.ok(defaultValue);
|
|
57
|
-
}
|
|
58
|
-
return validate2(value, path);
|
|
59
|
-
}
|
|
60
|
-
);
|
|
61
|
-
},
|
|
62
|
-
transform(fn) {
|
|
63
|
-
return createSchema(
|
|
64
|
-
`transform(${tag})`,
|
|
65
|
-
(value, path) => {
|
|
66
|
-
const result = validate2(value, path);
|
|
67
|
-
if (!result.ok) {
|
|
68
|
-
return result;
|
|
69
|
-
}
|
|
70
|
-
try {
|
|
71
|
-
return chunkOPVES6W2_js.ok(fn(result.value));
|
|
72
|
-
} catch (e) {
|
|
73
|
-
return chunkOPVES6W2_js.err([{
|
|
74
|
-
path,
|
|
75
|
-
message: e instanceof Error ? e.message : "Transform failed",
|
|
76
|
-
expected: "valid transformation",
|
|
77
|
-
received: String(value)
|
|
78
|
-
}]);
|
|
79
|
-
}
|
|
80
|
-
}
|
|
81
|
-
);
|
|
82
|
-
},
|
|
83
|
-
refine(predicate, message = "Refinement failed") {
|
|
84
|
-
return createSchema(
|
|
85
|
-
`refine(${tag})`,
|
|
86
|
-
(value, path) => {
|
|
87
|
-
const result = validate2(value, path);
|
|
88
|
-
if (!result.ok) {
|
|
89
|
-
return result;
|
|
90
|
-
}
|
|
91
|
-
if (!predicate(result.value)) {
|
|
92
|
-
return chunkOPVES6W2_js.err([{
|
|
93
|
-
path,
|
|
94
|
-
message,
|
|
95
|
-
expected: "value passing refinement",
|
|
96
|
-
received: String(value)
|
|
97
|
-
}]);
|
|
98
|
-
}
|
|
99
|
-
return result;
|
|
100
|
-
}
|
|
101
|
-
);
|
|
102
|
-
}
|
|
103
|
-
};
|
|
104
|
-
return schema;
|
|
105
|
-
}
|
|
106
|
-
function validationError(path, message, expected, received) {
|
|
107
|
-
return {
|
|
108
|
-
path,
|
|
109
|
-
message,
|
|
110
|
-
expected,
|
|
111
|
-
received: typeof received === "object" ? JSON.stringify(received)?.slice(0, 50) ?? "object" : String(received)
|
|
112
|
-
};
|
|
113
|
-
}
|
|
114
|
-
|
|
115
|
-
// src/schema/schemas.ts
|
|
116
|
-
function string() {
|
|
117
|
-
return createSchema("string", (value, path) => {
|
|
118
|
-
if (typeof value === "string") {
|
|
119
|
-
return chunkOPVES6W2_js.ok(value);
|
|
120
|
-
}
|
|
121
|
-
return chunkOPVES6W2_js.err([validationError(path, "Expected string", "string", value)]);
|
|
122
|
-
});
|
|
123
|
-
}
|
|
124
|
-
function number() {
|
|
125
|
-
return createSchema("number", (value, path) => {
|
|
126
|
-
if (typeof value === "number" && !Number.isNaN(value)) {
|
|
127
|
-
return chunkOPVES6W2_js.ok(value);
|
|
128
|
-
}
|
|
129
|
-
return chunkOPVES6W2_js.err([validationError(path, "Expected number", "number", value)]);
|
|
130
|
-
});
|
|
131
|
-
}
|
|
132
|
-
function boolean() {
|
|
133
|
-
return createSchema("boolean", (value, path) => {
|
|
134
|
-
if (typeof value === "boolean") {
|
|
135
|
-
return chunkOPVES6W2_js.ok(value);
|
|
136
|
-
}
|
|
137
|
-
return chunkOPVES6W2_js.err([validationError(path, "Expected boolean", "boolean", value)]);
|
|
138
|
-
});
|
|
139
|
-
}
|
|
140
|
-
function literal(expected) {
|
|
141
|
-
return createSchema(`literal(${JSON.stringify(expected)})`, (value, path) => {
|
|
142
|
-
if (value === expected) {
|
|
143
|
-
return chunkOPVES6W2_js.ok(expected);
|
|
144
|
-
}
|
|
145
|
-
return chunkOPVES6W2_js.err([validationError(path, `Expected ${JSON.stringify(expected)}`, String(expected), value)]);
|
|
146
|
-
});
|
|
147
|
-
}
|
|
148
|
-
function array(itemSchema) {
|
|
149
|
-
const impl = itemSchema;
|
|
150
|
-
return createSchema(`array(${impl._tag})`, (value, path) => {
|
|
151
|
-
if (!Array.isArray(value)) {
|
|
152
|
-
return chunkOPVES6W2_js.err([validationError(path, "Expected array", "array", value)]);
|
|
153
|
-
}
|
|
154
|
-
const results = [];
|
|
155
|
-
const errors = [];
|
|
156
|
-
for (let i = 0; i < value.length; i++) {
|
|
157
|
-
const result = impl._validate(value[i], [...path, i]);
|
|
158
|
-
if (result.ok) {
|
|
159
|
-
results.push(result.value);
|
|
160
|
-
} else {
|
|
161
|
-
errors.push(...result.error);
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
if (errors.length > 0) {
|
|
165
|
-
return chunkOPVES6W2_js.err(errors);
|
|
166
|
-
}
|
|
167
|
-
return chunkOPVES6W2_js.ok(results);
|
|
168
|
-
});
|
|
169
|
-
}
|
|
170
|
-
function object(shape) {
|
|
171
|
-
const shapeEntries = Object.entries(shape);
|
|
172
|
-
const tag = `object({ ${shapeEntries.map(([k, v]) => `${k}: ${v._tag}`).join(", ")} })`;
|
|
173
|
-
return createSchema(tag, (value, path) => {
|
|
174
|
-
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
175
|
-
return chunkOPVES6W2_js.err([validationError(path, "Expected object", "object", value)]);
|
|
176
|
-
}
|
|
177
|
-
const obj = value;
|
|
178
|
-
const result = {};
|
|
179
|
-
const errors = [];
|
|
180
|
-
for (const [key, schema] of shapeEntries) {
|
|
181
|
-
const fieldResult = schema._validate(obj[key], [...path, key]);
|
|
182
|
-
if (fieldResult.ok) {
|
|
183
|
-
result[key] = fieldResult.value;
|
|
184
|
-
} else {
|
|
185
|
-
errors.push(...fieldResult.error);
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
if (errors.length > 0) {
|
|
189
|
-
return chunkOPVES6W2_js.err(errors);
|
|
190
|
-
}
|
|
191
|
-
return chunkOPVES6W2_js.ok(result);
|
|
192
|
-
});
|
|
193
|
-
}
|
|
194
|
-
function optional(schema) {
|
|
195
|
-
return schema.optional();
|
|
196
|
-
}
|
|
197
|
-
function nullable(schema) {
|
|
198
|
-
return schema.nullable();
|
|
199
|
-
}
|
|
200
|
-
function union(schemas) {
|
|
201
|
-
const impls = schemas;
|
|
202
|
-
const tag = `union(${impls.map((s) => s._tag).join(" | ")})`;
|
|
203
|
-
return createSchema(tag, (value, path) => {
|
|
204
|
-
for (const schema of impls) {
|
|
205
|
-
const result = schema._validate(value, path);
|
|
206
|
-
if (result.ok) {
|
|
207
|
-
return chunkOPVES6W2_js.ok(result.value);
|
|
208
|
-
}
|
|
209
|
-
}
|
|
210
|
-
return chunkOPVES6W2_js.err([validationError(
|
|
211
|
-
path,
|
|
212
|
-
`Expected one of: ${impls.map((s) => s._tag).join(", ")}`,
|
|
213
|
-
tag,
|
|
214
|
-
value
|
|
215
|
-
)]);
|
|
216
|
-
});
|
|
217
|
-
}
|
|
218
|
-
function intersection(schemaA, schemaB) {
|
|
219
|
-
const implA = schemaA;
|
|
220
|
-
const implB = schemaB;
|
|
221
|
-
const tag = `intersection(${implA._tag} & ${implB._tag})`;
|
|
222
|
-
return createSchema(tag, (value, path) => {
|
|
223
|
-
const resultA = implA._validate(value, path);
|
|
224
|
-
if (!resultA.ok) {
|
|
225
|
-
return resultA;
|
|
226
|
-
}
|
|
227
|
-
const resultB = implB._validate(value, path);
|
|
228
|
-
if (!resultB.ok) {
|
|
229
|
-
return resultB;
|
|
230
|
-
}
|
|
231
|
-
return chunkOPVES6W2_js.ok({ ...resultA.value, ...resultB.value });
|
|
232
|
-
});
|
|
233
|
-
}
|
|
234
|
-
function record(valueSchema) {
|
|
235
|
-
const impl = valueSchema;
|
|
236
|
-
const tag = `record(${impl._tag})`;
|
|
237
|
-
return createSchema(tag, (value, path) => {
|
|
238
|
-
if (typeof value !== "object" || value === null || Array.isArray(value)) {
|
|
239
|
-
return chunkOPVES6W2_js.err([validationError(path, "Expected object", "object", value)]);
|
|
240
|
-
}
|
|
241
|
-
const obj = value;
|
|
242
|
-
const result = {};
|
|
243
|
-
const errors = [];
|
|
244
|
-
for (const [key, val] of Object.entries(obj)) {
|
|
245
|
-
const fieldResult = impl._validate(val, [...path, key]);
|
|
246
|
-
if (fieldResult.ok) {
|
|
247
|
-
result[key] = fieldResult.value;
|
|
248
|
-
} else {
|
|
249
|
-
errors.push(...fieldResult.error);
|
|
250
|
-
}
|
|
251
|
-
}
|
|
252
|
-
if (errors.length > 0) {
|
|
253
|
-
return chunkOPVES6W2_js.err(errors);
|
|
254
|
-
}
|
|
255
|
-
return chunkOPVES6W2_js.ok(result);
|
|
256
|
-
});
|
|
257
|
-
}
|
|
258
|
-
function tuple(schemas) {
|
|
259
|
-
const impls = schemas;
|
|
260
|
-
const tag = `tuple([${impls.map((s) => s._tag).join(", ")}])`;
|
|
261
|
-
return createSchema(tag, (value, path) => {
|
|
262
|
-
if (!Array.isArray(value)) {
|
|
263
|
-
return chunkOPVES6W2_js.err([validationError(path, "Expected array", "array", value)]);
|
|
264
|
-
}
|
|
265
|
-
if (value.length !== impls.length) {
|
|
266
|
-
return chunkOPVES6W2_js.err([validationError(
|
|
267
|
-
path,
|
|
268
|
-
`Expected tuple of length ${impls.length}`,
|
|
269
|
-
`[${impls.map((s) => s._tag).join(", ")}]`,
|
|
270
|
-
value
|
|
271
|
-
)]);
|
|
272
|
-
}
|
|
273
|
-
const results = [];
|
|
274
|
-
const errors = [];
|
|
275
|
-
for (let i = 0; i < impls.length; i++) {
|
|
276
|
-
const result = impls[i]._validate(value[i], [...path, i]);
|
|
277
|
-
if (result.ok) {
|
|
278
|
-
results.push(result.value);
|
|
279
|
-
} else {
|
|
280
|
-
errors.push(...result.error);
|
|
281
|
-
}
|
|
282
|
-
}
|
|
283
|
-
if (errors.length > 0) {
|
|
284
|
-
return chunkOPVES6W2_js.err(errors);
|
|
285
|
-
}
|
|
286
|
-
return chunkOPVES6W2_js.ok(results);
|
|
287
|
-
});
|
|
288
|
-
}
|
|
289
|
-
function unknown() {
|
|
290
|
-
return createSchema("unknown", (value) => chunkOPVES6W2_js.ok(value));
|
|
291
|
-
}
|
|
292
|
-
function any() {
|
|
293
|
-
return createSchema("any", (value) => chunkOPVES6W2_js.ok(value));
|
|
294
|
-
}
|
|
295
|
-
function date() {
|
|
296
|
-
return createSchema("date", (value, path) => {
|
|
297
|
-
if (value instanceof Date && !Number.isNaN(value.getTime())) {
|
|
298
|
-
return chunkOPVES6W2_js.ok(value);
|
|
299
|
-
}
|
|
300
|
-
if (typeof value === "string") {
|
|
301
|
-
const parsed = new Date(value);
|
|
302
|
-
if (!Number.isNaN(parsed.getTime())) {
|
|
303
|
-
return chunkOPVES6W2_js.ok(parsed);
|
|
304
|
-
}
|
|
305
|
-
}
|
|
306
|
-
return chunkOPVES6W2_js.err([validationError(path, "Expected valid date", "Date", value)]);
|
|
307
|
-
});
|
|
308
|
-
}
|
|
309
|
-
function email() {
|
|
310
|
-
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
|
|
311
|
-
return createSchema("email", (value, path) => {
|
|
312
|
-
if (typeof value !== "string") {
|
|
313
|
-
return chunkOPVES6W2_js.err([validationError(path, "Expected string", "string", value)]);
|
|
314
|
-
}
|
|
315
|
-
if (!emailRegex.test(value)) {
|
|
316
|
-
return chunkOPVES6W2_js.err([validationError(path, "Expected valid email", "email", value)]);
|
|
317
|
-
}
|
|
318
|
-
return chunkOPVES6W2_js.ok(value);
|
|
319
|
-
});
|
|
320
|
-
}
|
|
321
|
-
function url() {
|
|
322
|
-
return createSchema("url", (value, path) => {
|
|
323
|
-
if (typeof value !== "string") {
|
|
324
|
-
return chunkOPVES6W2_js.err([validationError(path, "Expected string", "string", value)]);
|
|
325
|
-
}
|
|
326
|
-
try {
|
|
327
|
-
new URL(value);
|
|
328
|
-
return chunkOPVES6W2_js.ok(value);
|
|
329
|
-
} catch {
|
|
330
|
-
return chunkOPVES6W2_js.err([validationError(path, "Expected valid URL", "url", value)]);
|
|
331
|
-
}
|
|
332
|
-
});
|
|
333
|
-
}
|
|
334
|
-
function uuid() {
|
|
335
|
-
const uuidRegex = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i;
|
|
336
|
-
return createSchema("uuid", (value, path) => {
|
|
337
|
-
if (typeof value !== "string") {
|
|
338
|
-
return chunkOPVES6W2_js.err([validationError(path, "Expected string", "string", value)]);
|
|
339
|
-
}
|
|
340
|
-
if (!uuidRegex.test(value)) {
|
|
341
|
-
return chunkOPVES6W2_js.err([validationError(path, "Expected valid UUID", "uuid", value)]);
|
|
342
|
-
}
|
|
343
|
-
return chunkOPVES6W2_js.ok(value);
|
|
344
|
-
});
|
|
345
|
-
}
|
|
346
|
-
|
|
347
|
-
// src/schema/parse.ts
|
|
348
|
-
function parse(schema, value) {
|
|
349
|
-
return schema.parse(value);
|
|
350
|
-
}
|
|
351
|
-
function safeParse(schema, value) {
|
|
352
|
-
return schema.safeParse(value);
|
|
353
|
-
}
|
|
354
|
-
function validate(schema, value) {
|
|
355
|
-
const result = schema.safeParse(value);
|
|
356
|
-
return result.ok ? [] : result.error;
|
|
357
|
-
}
|
|
358
|
-
function is(schema, value) {
|
|
359
|
-
return schema.is(value);
|
|
360
|
-
}
|
|
361
|
-
function parseJson(schema, json) {
|
|
362
|
-
try {
|
|
363
|
-
const parsed = JSON.parse(json);
|
|
364
|
-
return safeParse(schema, parsed);
|
|
365
|
-
} catch (e) {
|
|
366
|
-
return {
|
|
367
|
-
ok: false,
|
|
368
|
-
error: [{
|
|
369
|
-
path: [],
|
|
370
|
-
message: e instanceof Error ? e.message : "Invalid JSON",
|
|
371
|
-
expected: "valid JSON",
|
|
372
|
-
received: json.slice(0, 50)
|
|
373
|
-
}]
|
|
374
|
-
};
|
|
375
|
-
}
|
|
376
|
-
}
|
|
377
|
-
function createGuard(schema) {
|
|
378
|
-
return (value) => schema.is(value);
|
|
379
|
-
}
|
|
380
|
-
function createParser(schema) {
|
|
381
|
-
return (value) => parse(schema, value);
|
|
382
|
-
}
|
|
383
|
-
|
|
384
|
-
exports.anySchema = any;
|
|
385
|
-
exports.array = array;
|
|
386
|
-
exports.boolean = boolean;
|
|
387
|
-
exports.createGuard = createGuard;
|
|
388
|
-
exports.createParser = createParser;
|
|
389
|
-
exports.date = date;
|
|
390
|
-
exports.email = email;
|
|
391
|
-
exports.intersection = intersection;
|
|
392
|
-
exports.is = is;
|
|
393
|
-
exports.literal = literal;
|
|
394
|
-
exports.nullable = nullable;
|
|
395
|
-
exports.number = number;
|
|
396
|
-
exports.object = object;
|
|
397
|
-
exports.optional = optional;
|
|
398
|
-
exports.parse = parse;
|
|
399
|
-
exports.parseJson = parseJson;
|
|
400
|
-
exports.record = record;
|
|
401
|
-
exports.safeParse = safeParse;
|
|
402
|
-
exports.string = string;
|
|
403
|
-
exports.tuple = tuple;
|
|
404
|
-
exports.union = union;
|
|
405
|
-
exports.unknownSchema = unknown;
|
|
406
|
-
exports.url = url;
|
|
407
|
-
exports.uuid = uuid;
|
|
408
|
-
exports.validate = validate;
|
|
409
|
-
//# sourceMappingURL=index.js.map
|
|
410
|
-
//# sourceMappingURL=index.js.map
|
|
1
|
+
'use strict';var chunkWDFIRO2J_js=require('../chunk-WDFIRO2J.js');require('../chunk-P4UNM7SF.js'),require('../chunk-WOT6VMZA.js');Object.defineProperty(exports,"anySchema",{enumerable:true,get:function(){return chunkWDFIRO2J_js.n}});Object.defineProperty(exports,"array",{enumerable:true,get:function(){return chunkWDFIRO2J_js.e}});Object.defineProperty(exports,"boolean",{enumerable:true,get:function(){return chunkWDFIRO2J_js.c}});Object.defineProperty(exports,"createGuard",{enumerable:true,get:function(){return chunkWDFIRO2J_js.x}});Object.defineProperty(exports,"createParser",{enumerable:true,get:function(){return chunkWDFIRO2J_js.y}});Object.defineProperty(exports,"date",{enumerable:true,get:function(){return chunkWDFIRO2J_js.o}});Object.defineProperty(exports,"email",{enumerable:true,get:function(){return chunkWDFIRO2J_js.p}});Object.defineProperty(exports,"intersection",{enumerable:true,get:function(){return chunkWDFIRO2J_js.j}});Object.defineProperty(exports,"is",{enumerable:true,get:function(){return chunkWDFIRO2J_js.v}});Object.defineProperty(exports,"literal",{enumerable:true,get:function(){return chunkWDFIRO2J_js.d}});Object.defineProperty(exports,"nullable",{enumerable:true,get:function(){return chunkWDFIRO2J_js.h}});Object.defineProperty(exports,"number",{enumerable:true,get:function(){return chunkWDFIRO2J_js.b}});Object.defineProperty(exports,"object",{enumerable:true,get:function(){return chunkWDFIRO2J_js.f}});Object.defineProperty(exports,"optional",{enumerable:true,get:function(){return chunkWDFIRO2J_js.g}});Object.defineProperty(exports,"parse",{enumerable:true,get:function(){return chunkWDFIRO2J_js.s}});Object.defineProperty(exports,"parseJson",{enumerable:true,get:function(){return chunkWDFIRO2J_js.w}});Object.defineProperty(exports,"record",{enumerable:true,get:function(){return chunkWDFIRO2J_js.k}});Object.defineProperty(exports,"safeParse",{enumerable:true,get:function(){return chunkWDFIRO2J_js.t}});Object.defineProperty(exports,"string",{enumerable:true,get:function(){return chunkWDFIRO2J_js.a}});Object.defineProperty(exports,"tuple",{enumerable:true,get:function(){return chunkWDFIRO2J_js.l}});Object.defineProperty(exports,"union",{enumerable:true,get:function(){return chunkWDFIRO2J_js.i}});Object.defineProperty(exports,"unknownSchema",{enumerable:true,get:function(){return chunkWDFIRO2J_js.m}});Object.defineProperty(exports,"url",{enumerable:true,get:function(){return chunkWDFIRO2J_js.q}});Object.defineProperty(exports,"uuid",{enumerable:true,get:function(){return chunkWDFIRO2J_js.r}});Object.defineProperty(exports,"validate",{enumerable:true,get:function(){return chunkWDFIRO2J_js.u}});
|