@esmj/schema 0.7.3 → 0.8.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/README.md +127 -21
- package/dist/array.cjs +1 -1
- package/dist/array.d.mts +1 -1
- package/dist/array.d.ts +1 -1
- package/dist/array.mjs +1 -1
- package/dist/{chunk-YUL3WJPU.mjs → chunk-7VTVE334.mjs} +1 -1
- package/dist/chunk-AKXLQP64.mjs +1 -0
- package/dist/{chunk-YYPP27RX.mjs → chunk-AQFELD23.mjs} +1 -1
- package/dist/{chunk-CLEFU3BA.mjs → chunk-HVQY2UKM.mjs} +1 -1
- package/dist/chunk-SR7R5QYC.mjs +1 -0
- package/dist/coerce.cjs +1 -0
- package/dist/coerce.d.mts +27 -0
- package/dist/coerce.d.ts +27 -0
- package/dist/coerce.mjs +1 -0
- package/dist/full.cjs +1 -1
- package/dist/full.d.mts +2 -1
- package/dist/full.d.ts +2 -1
- package/dist/full.mjs +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.mts +36 -270
- package/dist/index.d.ts +36 -270
- package/dist/index.mjs +1 -1
- package/dist/number.cjs +1 -1
- package/dist/number.d.mts +1 -1
- package/dist/number.d.ts +1 -1
- package/dist/number.mjs +1 -1
- package/dist/string.cjs +1 -1
- package/dist/string.d.mts +1 -1
- package/dist/string.d.ts +1 -1
- package/dist/string.mjs +1 -1
- package/package.json +23 -5
- package/dist/chunk-QQOSS5DZ.mjs +0 -1
package/README.md
CHANGED
|
@@ -14,7 +14,9 @@ This small library provides a simple schema validation system for JavaScript/Typ
|
|
|
14
14
|
- [String Extensions](#string-extensions-esmjschemastring)
|
|
15
15
|
- [Number Extensions](#number-extensions-esmjschemanumber)
|
|
16
16
|
- [Array Extensions](#array-extensions-esmjschemaarray)
|
|
17
|
+
- [Coerce Extensions](#coerce-extensions-esmjschemacoerce)
|
|
17
18
|
- [Full Extensions](#full-extensions-esmjschemafull)
|
|
19
|
+
- [Named Exports & Tree-Shaking](#named-exports--tree-shaking)
|
|
18
20
|
- [API Reference Summary](#api-reference-summary)
|
|
19
21
|
- [Schema Types](#schema-types)
|
|
20
22
|
- [s.coerce](#scoerce)
|
|
@@ -107,11 +109,13 @@ const result = schema.parse({
|
|
|
107
109
|
|
|
108
110
|
### Performance Highlights
|
|
109
111
|
|
|
110
|
-
- **Schema Creation**: Create schemas at up to 4
|
|
111
|
-
- **Parsing**: Parse data at up to
|
|
112
|
-
- **Error Handling**:
|
|
112
|
+
- **Schema Creation**: Create schemas at up to 4,370,618 ops/s (0.23 μs latency) with @sinclair/typebox, or 736,810 ops/s (1.36 μs latency) with @esmj/schema. Superstruct is also competitive for schema creation performance.
|
|
113
|
+
- **Parsing**: Parse data at up to **33,620,146 ops/s** (**0.03 μs** latency) with **AJV** (best result in this benchmark). For comparison, `@zod/mini` reached **4,627,714 ops/s** (**0.22 μs**, observed at 200% CPU), and `@esmj/schema` reached **3,142,587 ops/s** (**0.32 μs**), with ArkType and effect/Schema also performing strongly.
|
|
114
|
+
- **Error Handling**: Handle validation errors at up to **19,693,821 ops/s** (**0.05 μs** latency) with **AJV** (best result in this benchmark), while `@esmj/schema` delivered **2,428,049 ops/s** (**0.41 μs**) with a developer-friendly API.
|
|
113
115
|
|
|
114
|
-
|
|
116
|
+
Across the benchmark tables, `@esmj/schema` shows strong all-around results: fast schema creation, very competitive parsing and error-handling throughput, and a very small bundle size (`~1.6 KB` core).
|
|
117
|
+
|
|
118
|
+
For most TypeScript applications, it offers a practical balance of performance, size, and developer ergonomics. If absolute peak throughput in a single category is the only goal, some specialized options (for example, AJV or TypeBox in specific tests) can be faster.
|
|
115
119
|
|
|
116
120
|
## Comparison with Similar Libraries
|
|
117
121
|
|
|
@@ -125,6 +129,7 @@ When choosing a schema validation library, bundle size can be an important facto
|
|
|
125
129
|
| Yup | ~12.2 KB |
|
|
126
130
|
| Zod@3 | ~13 KB |
|
|
127
131
|
| @zod/mini | ~20.5 KB |
|
|
132
|
+
| AJV | ~31.4 KB |
|
|
128
133
|
| Joi | ~40.4 KB |
|
|
129
134
|
| Zod@4 | ~40.8 KB |
|
|
130
135
|
| ArkType | ~41.8 KB |
|
|
@@ -147,6 +152,7 @@ When choosing a schema validation library, bundle size can be an important facto
|
|
|
147
152
|
| `@sinclair/typebox` | `4 370 618.49 ± 1.23%` | `0.23 ± 1.23%` |
|
|
148
153
|
| ArkType | 16 282.69 ± 4.14% | 61.61 ± 4.38% |
|
|
149
154
|
| effect/Schema | 24 919.15 ± 4.31% | 40.31 ± 4.78% |
|
|
155
|
+
| AJV | 240.32 ± 4.67% | 4 164.31 ± 2.04% |
|
|
150
156
|
|
|
151
157
|
#### Parsing Performance
|
|
152
158
|
|
|
@@ -154,19 +160,20 @@ When choosing a schema validation library, bundle size can be an important facto
|
|
|
154
160
|
|-------------------|-------------------------------:|-------------------------:|
|
|
155
161
|
| @esmj/schema | 3 142 587.31 ± 0.97% | 0.32 ± 0.99% |
|
|
156
162
|
| zod@3 | 1 018 777.24 ± 0.64% | 0.98 ± 0.65% |
|
|
157
|
-
|
|
|
163
|
+
| @zod/mini. | 4 627 714.90 ± 2.23% | 0.22 ± 2.36% |
|
|
158
164
|
| Yup | 108 361.49 ± 0.50% | 9.23 ± 0.51% |
|
|
159
165
|
| Superstruct | 252 904.42 ± 2.20% | 3.96 ± 2.44% |
|
|
160
166
|
| Joi | 346 094.49 ± 0.65% | 2.89 ± 0.65% |
|
|
161
167
|
| @sinclair/typebox | 228 711.62 ± 2.03% | 4.38 ± 2.23% |
|
|
162
168
|
| ArkType | 1 677 066.00 ± 0.58% | 0.60 ± 0.59% |
|
|
163
169
|
| effect/Schema | 1 060 056.14 ± 0.61% | 0.94 ± 0.61% |
|
|
170
|
+
| `AJV` | `33 620 146.24 ± 1.19%` | `0.03 ± 1.26%` |
|
|
164
171
|
|
|
165
172
|
#### Error Handling Performance
|
|
166
173
|
|
|
167
174
|
| Library | Throughput average (ops/s) | Latency average (μs) |
|
|
168
175
|
|-------------------|-------------------------------:|-------------------------:|
|
|
169
|
-
|
|
|
176
|
+
| @esmj/schema | 2 428 049.34 ± 0.54% | 0.41 ± 0.53% |
|
|
170
177
|
| zod@3 | 641 504.22 ± 3.67% | 1.57 ± 4.38% |
|
|
171
178
|
| @zod/mini | 1 386 616.61 ± 0.60% | 0.72 ± 0.60% |
|
|
172
179
|
| Yup | 98 904.30 ± 0.61% | 10.11 ± 0.61% |
|
|
@@ -175,6 +182,7 @@ When choosing a schema validation library, bundle size can be an important facto
|
|
|
175
182
|
| @sinclair/typebox | 228 734.49 ± 0.55% | 4.37 ± 0.56% |
|
|
176
183
|
| ArkType | 258 685.33 ± 1.23% | 3.87 ± 1.23% |
|
|
177
184
|
| effect/Schema | 165 753.69 ± 0.99% | 6.03 ± 1.00% |
|
|
185
|
+
| `AJV` | `19 693 821.79 ± 1.81%` | `0.05 ± 1.83%` |
|
|
178
186
|
|
|
179
187
|
**Note:** During the performance tests, `@zod/mini` was observed to consume 200% CPU, while other libraries used only 100% CPU. This may affect the interpretation of the results, especially in multi-threaded environments.
|
|
180
188
|
|
|
@@ -246,10 +254,21 @@ import { s } from '@esmj/schema/number';
|
|
|
246
254
|
// Array extensions only
|
|
247
255
|
import { s } from '@esmj/schema/array';
|
|
248
256
|
|
|
257
|
+
// Coerce extensions only
|
|
258
|
+
import { s } from '@esmj/schema/coerce';
|
|
259
|
+
|
|
249
260
|
// Mix and match (side-effect imports)
|
|
250
261
|
import '@esmj/schema/string';
|
|
251
262
|
import '@esmj/schema/number';
|
|
263
|
+
import '@esmj/schema/coerce';
|
|
252
264
|
import { s } from '@esmj/schema';
|
|
265
|
+
|
|
266
|
+
// Tree-shakeable named exports — bundle only what you use
|
|
267
|
+
import { string, number, object, array } from '@esmj/schema';
|
|
268
|
+
import { coerce, cast } from '@esmj/schema';
|
|
269
|
+
|
|
270
|
+
// Named exports for reserved-word factories
|
|
271
|
+
import { functionSchema, enumSchema } from '@esmj/schema';
|
|
253
272
|
```
|
|
254
273
|
|
|
255
274
|
### Bundle Size Impact
|
|
@@ -258,11 +277,62 @@ import { s } from '@esmj/schema';
|
|
|
258
277
|
- **String extensions** (`@esmj/schema/string`): +~0.8 KB
|
|
259
278
|
- **Number extensions** (`@esmj/schema/number`): +~0.6 KB
|
|
260
279
|
- **Array extensions** (`@esmj/schema/array`): +~0.5 KB
|
|
280
|
+
- **Coerce extensions** (`@esmj/schema/coerce`): +~0.3 KB
|
|
261
281
|
- **Full** (`@esmj/schema/full`): ~4 KB gzipped (all extensions)
|
|
262
282
|
|
|
263
283
|
**Recommendation:** Import only the extensions you need to minimize bundle size.
|
|
264
284
|
|
|
265
|
-
###
|
|
285
|
+
### Named Exports & Tree-Shaking
|
|
286
|
+
|
|
287
|
+
Every factory in the core module is exported both as a named function **and** as a property on `s`. The two references are identical — no extra wrapper, no overhead.
|
|
288
|
+
|
|
289
|
+
```typescript
|
|
290
|
+
import { s } from '@esmj/schema';
|
|
291
|
+
import { string, number } from '@esmj/schema';
|
|
292
|
+
|
|
293
|
+
s.string === string; // true
|
|
294
|
+
```
|
|
295
|
+
|
|
296
|
+
When you import individual factories, bundlers (webpack, Rollup, Vite, esbuild) tree-shake everything else out:
|
|
297
|
+
|
|
298
|
+
```typescript
|
|
299
|
+
// Only `string` and `number` end up in the final bundle — object, array, coerce, cast, etc. are excluded
|
|
300
|
+
import { string, number } from '@esmj/schema';
|
|
301
|
+
|
|
302
|
+
const nameSchema = string().optional();
|
|
303
|
+
const ageSchema = number();
|
|
304
|
+
```
|
|
305
|
+
|
|
306
|
+
The `coerce` and `cast` namespaces are also individually exported:
|
|
307
|
+
|
|
308
|
+
```typescript
|
|
309
|
+
// coerce is a named export from @esmj/schema but s.coerce methods
|
|
310
|
+
// require importing '@esmj/schema/coerce' (or '@esmj/schema/full') first:
|
|
311
|
+
import '@esmj/schema/coerce';
|
|
312
|
+
import { s } from '@esmj/schema';
|
|
313
|
+
|
|
314
|
+
const schema = s.coerce.number();
|
|
315
|
+
schema.parse('42'); // 42
|
|
316
|
+
|
|
317
|
+
// Or import the standalone coerce object directly:
|
|
318
|
+
import { coerce } from '@esmj/schema/coerce';
|
|
319
|
+
|
|
320
|
+
const schema2 = coerce.number();
|
|
321
|
+
schema2.parse('42'); // 42
|
|
322
|
+
```
|
|
323
|
+
|
|
324
|
+
Because `function` and `enum` are reserved words in JavaScript, their standalone names are `functionSchema` and `enumSchema`:
|
|
325
|
+
|
|
326
|
+
```typescript
|
|
327
|
+
import { functionSchema, enumSchema } from '@esmj/schema';
|
|
328
|
+
|
|
329
|
+
const cb = functionSchema();
|
|
330
|
+
const role = enumSchema(['admin', 'user', 'guest']);
|
|
331
|
+
```
|
|
332
|
+
|
|
333
|
+
They are still accessible as `s.function()` and `s.enum()` for full API compatibility.
|
|
334
|
+
|
|
335
|
+
|
|
266
336
|
|
|
267
337
|
String extensions provide common validation and transformation methods for string schemas.
|
|
268
338
|
|
|
@@ -360,6 +430,36 @@ tagsSchema.parse({
|
|
|
360
430
|
- **Content validations**: `unique()`
|
|
361
431
|
- **Transformations**: `sort()`, `reverse()`
|
|
362
432
|
|
|
433
|
+
### Coerce Extensions (`@esmj/schema/coerce`)
|
|
434
|
+
|
|
435
|
+
Coerce extensions add `s.coerce` methods that apply native JS constructors before validation, providing convenient type coercion with clear error messages.
|
|
436
|
+
|
|
437
|
+
> **Note:** `s.coerce` is opt-in. Import `@esmj/schema/coerce` (or `@esmj/schema/full`) to activate it. Importing only the core `@esmj/schema` leaves `s.coerce` empty.
|
|
438
|
+
|
|
439
|
+
```typescript
|
|
440
|
+
import { s } from '@esmj/schema/coerce';
|
|
441
|
+
|
|
442
|
+
s.coerce.number().parse('42'); // 42
|
|
443
|
+
s.coerce.string().parse(123); // '123'
|
|
444
|
+
s.coerce.boolean().parse(0); // false
|
|
445
|
+
s.coerce.date().parse('2024-01-01'); // Date object
|
|
446
|
+
|
|
447
|
+
// Coerce then chain schema methods:
|
|
448
|
+
s.coerce.number().refine((v) => v > 0, { message: 'Must be positive' }).parse('5'); // 5
|
|
449
|
+
|
|
450
|
+
// Custom error message:
|
|
451
|
+
s.coerce.number({ message: 'Expected a numeric value' }).parse('bad'); // throws: Expected a numeric value
|
|
452
|
+
```
|
|
453
|
+
|
|
454
|
+
**Available Coerce Methods:**
|
|
455
|
+
|
|
456
|
+
| Method | Coercion | Fails when |
|
|
457
|
+
|---|---|---|
|
|
458
|
+
| `s.coerce.string(options?)` | `String(v)` | Never |
|
|
459
|
+
| `s.coerce.number(options?)` | `Number(v)` | Result is `NaN` |
|
|
460
|
+
| `s.coerce.boolean(options?)` | `Boolean(v)` | Never |
|
|
461
|
+
| `s.coerce.date(options?)` | `new Date(v)` | Result is an invalid Date |
|
|
462
|
+
|
|
363
463
|
### Full Extensions (`@esmj/schema/full`)
|
|
364
464
|
|
|
365
465
|
The full version includes all string, number, and array extensions in a single import.
|
|
@@ -422,20 +522,22 @@ const schema = s.object({
|
|
|
422
522
|
|
|
423
523
|
### Core Types
|
|
424
524
|
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
- `s.
|
|
428
|
-
- `s.
|
|
429
|
-
- `s.
|
|
430
|
-
- `s.
|
|
431
|
-
- `s.
|
|
432
|
-
- `s.
|
|
433
|
-
- `s.
|
|
434
|
-
- `s.
|
|
435
|
-
- `s.
|
|
436
|
-
- `s.
|
|
437
|
-
- `s.
|
|
438
|
-
- `s.
|
|
525
|
+
All factory functions below are available both as methods on `s` **and** as individual named exports for tree-shaking:
|
|
526
|
+
|
|
527
|
+
- `s.string()` / `import { string }` - String validation
|
|
528
|
+
- `s.number()` / `import { number }` - Number validation
|
|
529
|
+
- `s.boolean()` / `import { boolean }` - Boolean validation
|
|
530
|
+
- `s.date()` / `import { date }` - Date validation
|
|
531
|
+
- `s.function()` / `import { functionSchema }` - Function validation
|
|
532
|
+
- `s.object(def)` / `import { object }` - Object validation
|
|
533
|
+
- `s.array(def)` / `import { array }` - Array validation
|
|
534
|
+
- `s.literal(value)` / `import { literal }` - Literal value validation
|
|
535
|
+
- `s.enum(values)` / `import { enumSchema }` - Enum validation
|
|
536
|
+
- `s.union(schemas)` / `import { union }` - Union validation
|
|
537
|
+
- `s.any()` / `import { any }` - Any type
|
|
538
|
+
- `s.preprocess(fn, schema)` / `import { preprocess }` - Preprocess before validation
|
|
539
|
+
- `s.coerce` / `import { coerce } from '@esmj/schema/coerce'` - Coerce namespace (requires `@esmj/schema/coerce` import)
|
|
540
|
+
- `s.cast` / `import { cast }` - Cast namespace
|
|
439
541
|
|
|
440
542
|
### Modifiers
|
|
441
543
|
|
|
@@ -447,6 +549,8 @@ const schema = s.object({
|
|
|
447
549
|
|
|
448
550
|
### Coerce
|
|
449
551
|
|
|
552
|
+
> Requires `import '@esmj/schema/coerce'` or `import '@esmj/schema/full'`. `s.coerce` is empty without this import.
|
|
553
|
+
|
|
450
554
|
- `s.coerce.string()` - Coerce any value to string, then validate
|
|
451
555
|
- `s.coerce.number()` - Coerce any value to number, then validate (fails for NaN)
|
|
452
556
|
- `s.coerce.boolean()` - Coerce any value to boolean, then validate
|
|
@@ -773,6 +877,8 @@ const preprocessSchema = s.preprocess((value) => new Date(value), s.date());
|
|
|
773
877
|
|
|
774
878
|
#### `s.coerce`
|
|
775
879
|
|
|
880
|
+
> **Requires `import '@esmj/schema/coerce'`** (or `'@esmj/schema/full'`) as a side-effect. Importing only the core `@esmj/schema` leaves `s.coerce` empty — calling `s.coerce.string()` will throw at runtime and produce a TypeScript error.
|
|
881
|
+
|
|
776
882
|
The `coerce` namespace applies a native JS constructor to the input **before** validation.
|
|
777
883
|
Unlike `s.preprocess`, you don't need to write the conversion yourself, and coerce methods
|
|
778
884
|
provide clear, specific error messages when coercion produces an invalid result.
|
package/dist/array.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';var
|
|
1
|
+
'use strict';var N={abortEarly:true};function b(e,c){if(!c)return e;let n=e?.cause?.key?`${c}.${e.cause.key}`:`${c}`;return {message:`Error parsing key "${n}": ${e.message}`,cause:{key:n}}}function x(e,c,n){if(e.errors?.length)for(let r=0;r<e.errors.length;r++)c.push(b(e.errors[r],n));}function O(e){return e?.abortEarly!==void 0?e:N}var I=e=>typeof e=="string"||e instanceof String,d=e=>(typeof e=="number"||e instanceof Number)&&!Number.isNaN(e),g=e=>e===true||e===false,R=e=>e instanceof Date&&!Number.isNaN(e.getTime()),j=e=>typeof e=="function",F=e=>Array.isArray(e),A=e=>typeof e=="object"&&e!==null&&!Array.isArray(e);function C(e,c){let n=h(A,{...c,type:"object"});return n._getDescription=()=>`object({ ${Object.entries(e).map(([u,s])=>`${u}: ${s._getDescription()}`).join(", ")} })`,m(n,"_parse",(r,u,s)=>{let t=r(u,s),{abortEarly:a}=O(s);if(t.success===false)return t;let o={},i=[];for(let f in e){let p=e[f]._parse(t.data[f],s);if(p.success)o[f]=p.data;else {if(p=p,a!==false){let S=b(p.error,f);return {success:false,error:S,errors:[S]}}x(p,i,f);}}return i.length>0?{success:false,error:i[0],errors:i}:{success:true,data:o}}),n}function P(e){return h(I,{...e,type:"string"})}function E(e){return h(d,{...e,type:"number"})}function _(e){return h(g,{...e,type:"boolean"})}function $(e){return h(R,{...e,type:"date"})}function V(e){return h(j,{...e,type:"function"})}function L(e,c){let n=t=>e.includes(t),r=t=>`Invalid ${u} value. Expected ${e.map(a=>`"${a}"`).join(" | ")}, received "${t}".`,u="enum",s=h(n,{message:r,...c,type:u});return s._getDescription=()=>`enum(${e.map(t=>`"${t}"`).join(" | ")})`,s}function M(e,c){let n=h(F,{...c,type:"array"});return n._getDescription=()=>`array(${e._getDescription()})`,m(n,"_parse",(r,u,s)=>{let t=r(u,s),{abortEarly:a}=O(s);if(t.success===false)return t;let o=[],i=[];for(let f=0;f<t.data.length;f++){let p=e._parse(t.data[f],s);if(p.success)o.push(p.data);else {if(p=p,a!==false){let S=b(p.error,f);return {success:false,error:S,errors:[S]}}x(p,i,f);}}return i.length>0?{success:false,error:i[0],errors:i}:{success:true,data:o}}),n}function B(){return h(()=>true)}function y(e,c){return m(c,"_parse",(n,r)=>(r=e(r),n(r))),c}function q(e,c){return h(s=>{for(let t=0;t<e.length;t++){let a=e[t]._parse(s);if(a.success)return a}return false},{message:s=>`Invalid union value. Expected the value to match one of the schemas:${e.map((t,a)=>` ${a+1}. ${t._getDescription()}`).join(",")} but received "${typeof s}" with value: ${A(s)?JSON.stringify(s):`"${s}"`}`,...c,type:"union"})}function J(e,c){let u=h(s=>s===e,{message:s=>c?.message?typeof c.message=="function"?c.message(s):c.message:`Expected literal value "${e}", received "${s}"`,name:c?.name,type:"literal"});return u._getDescription=()=>`literal("${e}")`,u}var U={boolean(e){let c=e?.message??(n=>`Cannot cast "${n}" to boolean. Accepted: true/false, 1/0, yes/no, on/off.`);return y(n=>{let r;return I(n)&&(r=n.toLowerCase()),r==="true"||r==="yes"||r==="on"||r==="1"||n===1?true:r==="false"||r==="no"||r==="off"||r==="0"||n===0?false:n},_({...e,message:c}))},number(e){let c=e?.message??(n=>`Cannot cast "${n}" to a number. Expected a numeric string or number.`);return y(n=>{if(g(n))return Number(n);if(I(n)){let r=n.trim();if(r==="")return n;let u=Number(r);if(Number.isFinite(u))return u}return n},E({...e,message:c}))},string(e){let c=e?.message??(n=>`Cannot cast "${n}" to string. Expected a string, number, or boolean.`);return y(n=>g(n)||d(n)&&Number.isFinite(n)?String(n):n,P({...e,message:c}))},date(e){let c=e?.message??(n=>`Cannot cast "${n}" to a valid date.`);return y(n=>{let r;return I(n)&&(r=n.trim()),d(n)&&Number.isFinite(n)||r?new Date(r??n):n},$({...e,message:c}))},json(e,c){let n=c?.message??(r=>`Cannot parse "${r}" as JSON.`);return m(e,"_parse",(r,u)=>{if(I(u))try{u=JSON.parse(u);}catch{let s={message:typeof n=="function"?n(u):n};return {success:false,error:s,errors:[s]}}return r(u)}),e}},G={object:C,string:P,number:E,boolean:_,date:$,function:V,enum:L,array:M,any:B,preprocess:y,union:q,literal:J,coerce:{},cast:U};function w(e){return c=>`The value "${c}" must be type of ${e} but is type of "${typeof c}".`}function m(e,c,n){let r=e[c];e[c]=(...u)=>n(r,...u);}function h(e,{type:c="any",name:n,message:r}={}){r=r||w(c);let u={name:n,message:r,type:c},s={_getName(){return n},_getType(){return c},_getDescription(){return this._getName()??this._getType()},_parse(t,a){let o=e(t);if(o===true)return {success:true,data:t};if(typeof o=="object"&&o?.success===true)return o;let i={message:typeof r=="function"?r(t):r};return {success:false,error:i,errors:[i]}},parse(t,a){let o=s._parse(t,a);if(!o.success)throw o=o,new Error(o.error.message,{cause:o.error.cause});return o.data},safeParse(t,a){return s._parse(t,a)},transform(t){return m(this,"_parse",(a,o,i)=>{let f=a(o,i);return f.success&&(f.data=t(f.data)),f}),this},optional(){return m(this,"_parse",(t,a,o)=>{let i=t(a,o);return !i.success&&a===void 0&&(i.data=void 0,i.success=true),i}),this},nullable(){return m(this,"_parse",(t,a,o)=>{let i=t(a,o);return !i.success&&a===null&&(i.data=null,i.success=true),i}),this},nullish(){return m(this,"_parse",(t,a,o)=>{let i=t(a,o);return !i.success&&a==null&&(i.success=true,i.data=a),i}),this},default(t){return m(this,"_parse",(a,o,i)=>(o===void 0&&(o=typeof t=="function"?t():t),a(o,i))),this},catch(t){return m(this,"_parse",(a,o,i)=>{let f=a(o,i);return f.success?f:{success:true,data:typeof t=="function"?t({input:o,error:f.error}):t}}),this},pipe(t){return m(this,"_parse",(a,o,i)=>{let f=a(o,i);return f.success?t._parse(f.data,i):f}),this},refine(t,{message:a,type:o}={}){return o&&(a=a||w(o),c=o),m(this,"_parse",(i,f,p)=>{let S=i(f,p),{abortEarly:z}=O(p);if(!S.success)return S;let l=t(S.data);if(l===true||typeof l=="object"&&l?.success===true)return S;let k={message:typeof a=="function"?a(f):a};return {success:false,error:k,errors:[k]}}),this}};return T.length>0?T.reduce((t,a)=>a(t,e,u)??t,s):s}var T=[];function D(e){if(typeof e!="function")throw new TypeError("extend() requires a function argument");T.push(e);}D((e,c,n)=>{if(n?.type==="array"){let r=e;r.min=function(u,{message:s}={}){return this.refine(t=>t.length>=u,{message:s||`Array must contain at least ${u} items.`})},r.max=function(u,{message:s}={}){return this.refine(t=>t.length<=u,{message:s||`Array must contain at most ${u} items.`})},r.length=function(u,{message:s}={}){return this.refine(t=>t.length===u,{message:s||`Array must contain exactly ${u} items.`})},r.nonEmpty=function({message:u}={}){return this.refine(s=>s.length>0,{message:u||"Array must not be empty."})},r.unique=function({message:u}={}){return this.refine(s=>{let t=new Set;try{return s.every(a=>{let o=JSON.stringify(a);return t.has(o)?!1:(t.add(o),!0)})}catch{return new Set(s).size===s.length}},{message:u||"Array items must be unique."})},r.sort=function(){return this.transform(u=>[...u].sort())},r.reverse=function(){return this.transform(u=>[...u].reverse())};}return e});exports.any=B;exports.array=M;exports.boolean=_;exports.cast=U;exports.date=$;exports.enumSchema=L;exports.extend=D;exports.functionSchema=V;exports.hookOriginal=m;exports.literal=J;exports.number=E;exports.object=C;exports.preprocess=y;exports.s=G;exports.schema=G;exports.string=P;exports.union=q;
|
package/dist/array.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, FunctionSchemaInterface, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s, s as schema } from './index.mjs';
|
|
1
|
+
export { ArraySchemaInterface, BooleanSchemaInterface, CoerceInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, FunctionSchemaInterface, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, any, array, boolean, cast, date, enumSchema, extend, functionSchema, hookOriginal, literal, number, object, preprocess, s, s as schema, string, union } from './index.mjs';
|
|
2
2
|
|
|
3
3
|
declare module './index.ts' {
|
|
4
4
|
interface ArraySchemaInterface<T extends SchemaType> {
|
package/dist/array.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, FunctionSchemaInterface, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s, s as schema } from './index.js';
|
|
1
|
+
export { ArraySchemaInterface, BooleanSchemaInterface, CoerceInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, FunctionSchemaInterface, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, any, array, boolean, cast, date, enumSchema, extend, functionSchema, hookOriginal, literal, number, object, preprocess, s, s as schema, string, union } from './index.js';
|
|
2
2
|
|
|
3
3
|
declare module './index.ts' {
|
|
4
4
|
interface ArraySchemaInterface<T extends SchemaType> {
|
package/dist/array.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import'./chunk-
|
|
1
|
+
import'./chunk-AQFELD23.mjs';export{i as any,h as array,d as boolean,m as cast,e as date,g as enumSchema,p as extend,f as functionSchema,o as hookOriginal,l as literal,c as number,a as object,j as preprocess,n as s,n as schema,b as string,k as union}from'./chunk-AKXLQP64.mjs';
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {p}from'./chunk-AKXLQP64.mjs';p((a,s,i)=>{if(i?.type==="string"){let r=a;r.min=function(n,{message:t}={}){return this.refine(e=>e.length>=n,{message:t||(e=>`String must be at least ${n} characters long (received ${e.length} characters: "${e}")`)})},r.max=function(n,{message:t}={}){return this.refine(e=>e.length<=n,{message:t||(e=>`String must be at most ${n} characters long (received ${e.length} characters: "${e}")`)})},r.length=function(n,{message:t}={}){return this.refine(e=>e.length===n,{message:t||(e=>`String must be exactly ${n} characters long (received ${e.length} characters: "${e}")`)})},r.nonEmpty=function({message:n}={}){return this.refine(t=>t.length>0,{message:n||"String must not be empty (received empty string)"})},r.startsWith=function(n,{message:t}={}){return this.refine(e=>e.startsWith(n),{message:t||(e=>`String must start with "${n}" (received: "${e}")`)})},r.endsWith=function(n,{message:t}={}){return this.refine(e=>e.endsWith(n),{message:t||(e=>`String must end with "${n}" (received: "${e}")`)})},r.includes=function(n,{message:t}={}){return this.refine(e=>e.includes(n),{message:t||(e=>`String must include "${n}" (received: "${e}")`)})},r.toLowerCase=function(){return this.transform(n=>n.toLowerCase())},r.toUpperCase=function(){return this.transform(n=>n.toUpperCase())},r.trim=function(){return this.transform(n=>n.trim())},r.padStart=function(n,t=" "){return this.transform(e=>e.padStart(n,t))},r.padEnd=function(n,t=" "){return this.transform(e=>e.padEnd(n,t))},r.replace=function(n,t){return this.transform(e=>e.replace(n,t))};}return a});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var N={abortEarly:true};function T(e,a){if(!a)return e;let n=e?.cause?.key?`${a}.${e.cause.key}`:`${a}`;return {message:`Error parsing key "${n}": ${e.message}`,cause:{key:n}}}function w(e,a,n){if(e.errors?.length)for(let r=0;r<e.errors.length;r++)a.push(T(e.errors[r],n));}function O(e){return e?.abortEarly!==void 0?e:N}var l=e=>typeof e=="string"||e instanceof String,y=e=>(typeof e=="number"||e instanceof Number)&&!Number.isNaN(e),g=e=>e===true||e===false,R=e=>e instanceof Date&&!Number.isNaN(e.getTime()),A=e=>typeof e=="function",j=e=>Array.isArray(e),P=e=>typeof e=="object"&&e!==null&&!Array.isArray(e);function F(e,a){let n=h(P,{...a,type:"object"});return n._getDescription=()=>`object({ ${Object.entries(e).map(([p,o])=>`${p}: ${o._getDescription()}`).join(", ")} })`,m(n,"_parse",(r,p,o)=>{let t=r(p,o),{abortEarly:s}=O(o);if(t.success===false)return t;let u={},c=[];for(let i in e){let f=e[i]._parse(t.data[i],o);if(f.success)u[i]=f.data;else {if(f=f,s!==false){let I=T(f.error,i);return {success:false,error:I,errors:[I]}}w(f,c,i);}}return c.length>0?{success:false,error:c[0],errors:c}:{success:true,data:u}}),n}function E(e){return h(l,{...e,type:"string"})}function _(e){return h(y,{...e,type:"number"})}function D(e){return h(g,{...e,type:"boolean"})}function $(e){return h(R,{...e,type:"date"})}function C(e){return h(A,{...e,type:"function"})}function V(e,a){let n=t=>e.includes(t),r=t=>`Invalid ${p} value. Expected ${e.map(s=>`"${s}"`).join(" | ")}, received "${t}".`,p="enum",o=h(n,{message:r,...a,type:p});return o._getDescription=()=>`enum(${e.map(t=>`"${t}"`).join(" | ")})`,o}function L(e,a){let n=h(j,{...a,type:"array"});return n._getDescription=()=>`array(${e._getDescription()})`,m(n,"_parse",(r,p,o)=>{let t=r(p,o),{abortEarly:s}=O(o);if(t.success===false)return t;let u=[],c=[];for(let i=0;i<t.data.length;i++){let f=e._parse(t.data[i],o);if(f.success)u.push(f.data);else {if(f=f,s!==false){let I=T(f.error,i);return {success:false,error:I,errors:[I]}}w(f,c,i);}}return c.length>0?{success:false,error:c[0],errors:c}:{success:true,data:u}}),n}function M(){return h(()=>true)}function d(e,a){return m(a,"_parse",(n,r)=>(r=e(r),n(r))),a}function B(e,a){return h(o=>{for(let t=0;t<e.length;t++){let s=e[t]._parse(o);if(s.success)return s}return false},{message:o=>`Invalid union value. Expected the value to match one of the schemas:${e.map((t,s)=>` ${s+1}. ${t._getDescription()}`).join(",")} but received "${typeof o}" with value: ${P(o)?JSON.stringify(o):`"${o}"`}`,...a,type:"union"})}function U(e,a){let p=h(o=>o===e,{message:o=>a?.message?typeof a.message=="function"?a.message(o):a.message:`Expected literal value "${e}", received "${o}"`,name:a?.name,type:"literal"});return p._getDescription=()=>`literal("${e}")`,p}var J={boolean(e){let a=e?.message??(n=>`Cannot cast "${n}" to boolean. Accepted: true/false, 1/0, yes/no, on/off.`);return d(n=>{let r;return l(n)&&(r=n.toLowerCase()),r==="true"||r==="yes"||r==="on"||r==="1"||n===1?true:r==="false"||r==="no"||r==="off"||r==="0"||n===0?false:n},D({...e,message:a}))},number(e){let a=e?.message??(n=>`Cannot cast "${n}" to a number. Expected a numeric string or number.`);return d(n=>{if(g(n))return Number(n);if(l(n)){let r=n.trim();if(r==="")return n;let p=Number(r);if(Number.isFinite(p))return p}return n},_({...e,message:a}))},string(e){let a=e?.message??(n=>`Cannot cast "${n}" to string. Expected a string, number, or boolean.`);return d(n=>g(n)||y(n)&&Number.isFinite(n)?String(n):n,E({...e,message:a}))},date(e){let a=e?.message??(n=>`Cannot cast "${n}" to a valid date.`);return d(n=>{let r;return l(n)&&(r=n.trim()),y(n)&&Number.isFinite(n)||r?new Date(r??n):n},$({...e,message:a}))},json(e,a){let n=a?.message??(r=>`Cannot parse "${r}" as JSON.`);return m(e,"_parse",(r,p)=>{if(l(p))try{p=JSON.parse(p);}catch{let o={message:typeof n=="function"?n(p):n};return {success:false,error:o,errors:[o]}}return r(p)}),e}},G={object:F,string:E,number:_,boolean:D,date:$,function:C,enum:V,array:L,any:M,preprocess:d,union:B,literal:U,coerce:{},cast:J};function x(e){return a=>`The value "${a}" must be type of ${e} but is type of "${typeof a}".`}function m(e,a,n){let r=e[a];e[a]=(...p)=>n(r,...p);}function h(e,{type:a="any",name:n,message:r}={}){r=r||x(a);let p={name:n,message:r,type:a},o={_getName(){return n},_getType(){return a},_getDescription(){return this._getName()??this._getType()},_parse(t,s){let u=e(t);if(u===true)return {success:true,data:t};if(typeof u=="object"&&u?.success===true)return u;let c={message:typeof r=="function"?r(t):r};return {success:false,error:c,errors:[c]}},parse(t,s){let u=o._parse(t,s);if(!u.success)throw u=u,new Error(u.error.message,{cause:u.error.cause});return u.data},safeParse(t,s){return o._parse(t,s)},transform(t){return m(this,"_parse",(s,u,c)=>{let i=s(u,c);return i.success&&(i.data=t(i.data)),i}),this},optional(){return m(this,"_parse",(t,s,u)=>{let c=t(s,u);return !c.success&&s===void 0&&(c.data=void 0,c.success=true),c}),this},nullable(){return m(this,"_parse",(t,s,u)=>{let c=t(s,u);return !c.success&&s===null&&(c.data=null,c.success=true),c}),this},nullish(){return m(this,"_parse",(t,s,u)=>{let c=t(s,u);return !c.success&&s==null&&(c.success=true,c.data=s),c}),this},default(t){return m(this,"_parse",(s,u,c)=>(u===void 0&&(u=typeof t=="function"?t():t),s(u,c))),this},catch(t){return m(this,"_parse",(s,u,c)=>{let i=s(u,c);return i.success?i:{success:true,data:typeof t=="function"?t({input:u,error:i.error}):t}}),this},pipe(t){return m(this,"_parse",(s,u,c)=>{let i=s(u,c);return i.success?t._parse(i.data,c):i}),this},refine(t,{message:s,type:u}={}){return u&&(s=s||x(u),a=u),m(this,"_parse",(c,i,f)=>{let I=c(i,f),{abortEarly:q}=O(f);if(!I.success)return I;let S=t(I.data);if(S===true||typeof S=="object"&&S?.success===true)return I;let k={message:typeof s=="function"?s(i):s};return {success:false,error:k,errors:[k]}}),this}};return b.length>0?b.reduce((t,s)=>s(t,e,p)??t,o):o}var b=[];function H(e){if(typeof e!="function")throw new TypeError("extend() requires a function argument");b.push(e);}export{F as a,E as b,_ as c,D as d,$ as e,C as f,V as g,L as h,M as i,d as j,B as k,U as l,J as m,G as n,m as o,H as p};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {p}from'./chunk-AKXLQP64.mjs';p((t,h,i)=>{if(i?.type==="array"){let r=t;r.min=function(e,{message:n}={}){return this.refine(a=>a.length>=e,{message:n||`Array must contain at least ${e} items.`})},r.max=function(e,{message:n}={}){return this.refine(a=>a.length<=e,{message:n||`Array must contain at most ${e} items.`})},r.length=function(e,{message:n}={}){return this.refine(a=>a.length===e,{message:n||`Array must contain exactly ${e} items.`})},r.nonEmpty=function({message:e}={}){return this.refine(n=>n.length>0,{message:e||"Array must not be empty."})},r.unique=function({message:e}={}){return this.refine(n=>{let a=new Set;try{return n.every(c=>{let s=JSON.stringify(c);return a.has(s)?!1:(a.add(s),!0)})}catch{return new Set(n).size===n.length}},{message:e||"Array items must be unique."})},r.sort=function(){return this.transform(e=>[...e].sort())},r.reverse=function(){return this.transform(e=>[...e].reverse())};}return t});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {p}from'./chunk-AKXLQP64.mjs';p((r,c,i)=>{if(i?.type==="number"){let t=r;t.min=function(e,{message:n}={}){return this.refine(a=>a>=e,{message:n||`Number must be greater than or equal to ${e}.`})},t.max=function(e,{message:n}={}){return this.refine(a=>a<=e,{message:n||`Number must be less than or equal to ${e}.`})},t.positive=function({message:e}={}){return this.refine(n=>n>0,{message:e||"Number must be positive."})},t.negative=function({message:e}={}){return this.refine(n=>n<0,{message:e||"Number must be negative."})},t.int=function({message:e}={}){return this.refine(n=>Number.isInteger(n),{message:e||"Number must be an integer."})},t.float=function({message:e}={}){return this.refine(n=>Number.isFinite(n)&&!Number.isInteger(n),{message:e||"Number must be a floating point (non-integer)."})},t.multipleOf=function(e,{message:n}={}){return this.refine(a=>a%e===0,{message:n||`Number must be a multiple of ${e}.`})},t.finite=function({message:e}={}){return this.refine(n=>Number.isFinite(n),{message:e||"Number must be finite."})};}return r});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import {n,j,e,d,c,b}from'./chunk-AKXLQP64.mjs';Object.assign(n.coerce,{string(e){return j(n=>String(n),b(e))},number(e){let n=e?.message??(a=>`Cannot coerce "${a}" to a valid number.`);return j(a=>Number(a),c({...e,message:n}))},boolean(e){return j(n=>!!n,d(e))},date(e$1){let n=e$1?.message??(a=>`Cannot coerce "${a}" to a valid date.`);return j(a=>new Date(a),e({...e$1,message:n}))}});
|
package/dist/coerce.cjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
'use strict';var R={abortEarly:true};function x(e,t){if(!t)return e;let n=e?.cause?.key?`${t}.${e.cause.key}`:`${t}`;return {message:`Error parsing key "${n}": ${e.message}`,cause:{key:n}}}function N(e,t,n){if(e.errors?.length)for(let a=0;a<e.errors.length;a++)t.push(x(e.errors[a],n));}function P(e){return e?.abortEarly!==void 0?e:R}var l=e=>typeof e=="string"||e instanceof String,T=e=>(typeof e=="number"||e instanceof Number)&&!Number.isNaN(e),k=e=>e===true||e===false,A=e=>e instanceof Date&&!Number.isNaN(e.getTime()),j=e=>typeof e=="function",F=e=>Array.isArray(e),_=e=>typeof e=="object"&&e!==null&&!Array.isArray(e);function C(e,t){let n=h(_,{...t,type:"object"});return n._getDescription=()=>`object({ ${Object.entries(e).map(([f,u])=>`${f}: ${u._getDescription()}`).join(", ")} })`,m(n,"_parse",(a,f,u)=>{let r=a(f,u),{abortEarly:c}=P(u);if(r.success===false)return r;let o={},s=[];for(let i in e){let p=e[i]._parse(r.data[i],u);if(p.success)o[i]=p.data;else {if(p=p,c!==false){let I=x(p.error,i);return {success:false,error:I,errors:[I]}}N(p,s,i);}}return s.length>0?{success:false,error:s[0],errors:s}:{success:true,data:o}}),n}function d(e){return h(l,{...e,type:"string"})}function y(e){return h(T,{...e,type:"number"})}function g(e){return h(k,{...e,type:"boolean"})}function b(e){return h(A,{...e,type:"date"})}function V(e){return h(j,{...e,type:"function"})}function B(e,t){let n=r=>e.includes(r),a=r=>`Invalid ${f} value. Expected ${e.map(c=>`"${c}"`).join(" | ")}, received "${r}".`,f="enum",u=h(n,{message:a,...t,type:f});return u._getDescription=()=>`enum(${e.map(r=>`"${r}"`).join(" | ")})`,u}function L(e,t){let n=h(F,{...t,type:"array"});return n._getDescription=()=>`array(${e._getDescription()})`,m(n,"_parse",(a,f,u)=>{let r=a(f,u),{abortEarly:c}=P(u);if(r.success===false)return r;let o=[],s=[];for(let i=0;i<r.data.length;i++){let p=e._parse(r.data[i],u);if(p.success)o.push(p.data);else {if(p=p,c!==false){let I=x(p.error,i);return {success:false,error:I,errors:[I]}}N(p,s,i);}}return s.length>0?{success:false,error:s[0],errors:s}:{success:true,data:o}}),n}function M(){return h(()=>true)}function S(e,t){return m(t,"_parse",(n,a)=>(a=e(a),n(a))),t}function U(e,t){return h(u=>{for(let r=0;r<e.length;r++){let c=e[r]._parse(u);if(c.success)return c}return false},{message:u=>`Invalid union value. Expected the value to match one of the schemas:${e.map((r,c)=>` ${c+1}. ${r._getDescription()}`).join(",")} but received "${typeof u}" with value: ${_(u)?JSON.stringify(u):`"${u}"`}`,...t,type:"union"})}function J(e,t){let f=h(u=>u===e,{message:u=>t?.message?typeof t.message=="function"?t.message(u):t.message:`Expected literal value "${e}", received "${u}"`,name:t?.name,type:"literal"});return f._getDescription=()=>`literal("${e}")`,f}var q={boolean(e){let t=e?.message??(n=>`Cannot cast "${n}" to boolean. Accepted: true/false, 1/0, yes/no, on/off.`);return S(n=>{let a;return l(n)&&(a=n.toLowerCase()),a==="true"||a==="yes"||a==="on"||a==="1"||n===1?true:a==="false"||a==="no"||a==="off"||a==="0"||n===0?false:n},g({...e,message:t}))},number(e){let t=e?.message??(n=>`Cannot cast "${n}" to a number. Expected a numeric string or number.`);return S(n=>{if(k(n))return Number(n);if(l(n)){let a=n.trim();if(a==="")return n;let f=Number(a);if(Number.isFinite(f))return f}return n},y({...e,message:t}))},string(e){let t=e?.message??(n=>`Cannot cast "${n}" to string. Expected a string, number, or boolean.`);return S(n=>k(n)||T(n)&&Number.isFinite(n)?String(n):n,d({...e,message:t}))},date(e){let t=e?.message??(n=>`Cannot cast "${n}" to a valid date.`);return S(n=>{let a;return l(n)&&(a=n.trim()),T(n)&&Number.isFinite(n)||a?new Date(a??n):n},b({...e,message:t}))},json(e,t){let n=t?.message??(a=>`Cannot parse "${a}" as JSON.`);return m(e,"_parse",(a,f)=>{if(l(f))try{f=JSON.parse(f);}catch{let u={message:typeof n=="function"?n(f):n};return {success:false,error:u,errors:[u]}}return a(f)}),e}},$={object:C,string:d,number:y,boolean:g,date:b,function:V,enum:B,array:L,any:M,preprocess:S,union:U,literal:J,coerce:{},cast:q};function D(e){return t=>`The value "${t}" must be type of ${e} but is type of "${typeof t}".`}function m(e,t,n){let a=e[t];e[t]=(...f)=>n(a,...f);}function h(e,{type:t="any",name:n,message:a}={}){a=a||D(t);let f={name:n,message:a,type:t},u={_getName(){return n},_getType(){return t},_getDescription(){return this._getName()??this._getType()},_parse(r,c){let o=e(r);if(o===true)return {success:true,data:r};if(typeof o=="object"&&o?.success===true)return o;let s={message:typeof a=="function"?a(r):a};return {success:false,error:s,errors:[s]}},parse(r,c){let o=u._parse(r,c);if(!o.success)throw o=o,new Error(o.error.message,{cause:o.error.cause});return o.data},safeParse(r,c){return u._parse(r,c)},transform(r){return m(this,"_parse",(c,o,s)=>{let i=c(o,s);return i.success&&(i.data=r(i.data)),i}),this},optional(){return m(this,"_parse",(r,c,o)=>{let s=r(c,o);return !s.success&&c===void 0&&(s.data=void 0,s.success=true),s}),this},nullable(){return m(this,"_parse",(r,c,o)=>{let s=r(c,o);return !s.success&&c===null&&(s.data=null,s.success=true),s}),this},nullish(){return m(this,"_parse",(r,c,o)=>{let s=r(c,o);return !s.success&&c==null&&(s.success=true,s.data=c),s}),this},default(r){return m(this,"_parse",(c,o,s)=>(o===void 0&&(o=typeof r=="function"?r():r),c(o,s))),this},catch(r){return m(this,"_parse",(c,o,s)=>{let i=c(o,s);return i.success?i:{success:true,data:typeof r=="function"?r({input:o,error:i.error}):r}}),this},pipe(r){return m(this,"_parse",(c,o,s)=>{let i=c(o,s);return i.success?r._parse(i.data,s):i}),this},refine(r,{message:c,type:o}={}){return o&&(c=c||D(o),t=o),m(this,"_parse",(s,i,p)=>{let I=s(i,p),{abortEarly:z}=P(p);if(!I.success)return I;let O=r(I.data);if(O===true||typeof O=="object"&&O?.success===true)return I;let E={message:typeof c=="function"?c(i):c};return {success:false,error:E,errors:[E]}}),this}};return w.length>0?w.reduce((r,c)=>c(r,e,f)??r,u):u}var w=[];function H(e){if(typeof e!="function")throw new TypeError("extend() requires a function argument");w.push(e);}Object.assign($.coerce,{string(e){return S(t=>String(t),d(e))},number(e){let t=e?.message??(n=>`Cannot coerce "${n}" to a valid number.`);return S(n=>Number(n),y({...e,message:t}))},boolean(e){return S(t=>!!t,g(e))},date(e){let t=e?.message??(n=>`Cannot coerce "${n}" to a valid date.`);return S(n=>new Date(n),b({...e,message:t}))}});exports.any=M;exports.array=L;exports.boolean=g;exports.cast=q;exports.date=b;exports.enumSchema=B;exports.extend=H;exports.functionSchema=V;exports.hookOriginal=m;exports.literal=J;exports.number=y;exports.object=C;exports.preprocess=S;exports.s=$;exports.schema=$;exports.string=d;exports.union=U;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export { ArraySchemaInterface, BooleanSchemaInterface, CoerceInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, FunctionSchemaInterface, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, any, array, boolean, cast, date, enumSchema, extend, functionSchema, hookOriginal, literal, number, object, preprocess, s, s as schema, string, union } from './index.mjs';
|
|
2
|
+
|
|
3
|
+
declare module './index.ts' {
|
|
4
|
+
interface CoerceInterface {
|
|
5
|
+
/**
|
|
6
|
+
* Creates a string schema that coerces input using `String(value)`.
|
|
7
|
+
* Always succeeds — `String()` never produces an invalid string.
|
|
8
|
+
*/
|
|
9
|
+
string(options?: SchemaInterfaceOptions): StringSchemaInterface;
|
|
10
|
+
/**
|
|
11
|
+
* Creates a number schema that coerces input using `Number(value)`.
|
|
12
|
+
* Fails when the result is `NaN` (e.g. `'bad'`, `undefined`, plain objects).
|
|
13
|
+
*/
|
|
14
|
+
number(options?: SchemaInterfaceOptions): NumberSchemaInterface;
|
|
15
|
+
/**
|
|
16
|
+
* Creates a boolean schema that coerces input using `Boolean(value)`.
|
|
17
|
+
* Always succeeds — `Boolean()` always produces `true` or `false`.
|
|
18
|
+
* Note: `Boolean('false')` is `true` because `'false'` is a non-empty string.
|
|
19
|
+
*/
|
|
20
|
+
boolean(options?: SchemaInterfaceOptions): BooleanSchemaInterface;
|
|
21
|
+
/**
|
|
22
|
+
* Creates a date schema that coerces input using `new Date(value)`.
|
|
23
|
+
* Fails when the result is an invalid Date (e.g. `'garbage'`).
|
|
24
|
+
*/
|
|
25
|
+
date(options?: SchemaInterfaceOptions): DateSchemaInterface;
|
|
26
|
+
}
|
|
27
|
+
}
|
package/dist/coerce.d.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
export { ArraySchemaInterface, BooleanSchemaInterface, CoerceInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, FunctionSchemaInterface, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, any, array, boolean, cast, date, enumSchema, extend, functionSchema, hookOriginal, literal, number, object, preprocess, s, s as schema, string, union } from './index.js';
|
|
2
|
+
|
|
3
|
+
declare module './index.ts' {
|
|
4
|
+
interface CoerceInterface {
|
|
5
|
+
/**
|
|
6
|
+
* Creates a string schema that coerces input using `String(value)`.
|
|
7
|
+
* Always succeeds — `String()` never produces an invalid string.
|
|
8
|
+
*/
|
|
9
|
+
string(options?: SchemaInterfaceOptions): StringSchemaInterface;
|
|
10
|
+
/**
|
|
11
|
+
* Creates a number schema that coerces input using `Number(value)`.
|
|
12
|
+
* Fails when the result is `NaN` (e.g. `'bad'`, `undefined`, plain objects).
|
|
13
|
+
*/
|
|
14
|
+
number(options?: SchemaInterfaceOptions): NumberSchemaInterface;
|
|
15
|
+
/**
|
|
16
|
+
* Creates a boolean schema that coerces input using `Boolean(value)`.
|
|
17
|
+
* Always succeeds — `Boolean()` always produces `true` or `false`.
|
|
18
|
+
* Note: `Boolean('false')` is `true` because `'false'` is a non-empty string.
|
|
19
|
+
*/
|
|
20
|
+
boolean(options?: SchemaInterfaceOptions): BooleanSchemaInterface;
|
|
21
|
+
/**
|
|
22
|
+
* Creates a date schema that coerces input using `new Date(value)`.
|
|
23
|
+
* Fails when the result is an invalid Date (e.g. `'garbage'`).
|
|
24
|
+
*/
|
|
25
|
+
date(options?: SchemaInterfaceOptions): DateSchemaInterface;
|
|
26
|
+
}
|
|
27
|
+
}
|
package/dist/coerce.mjs
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import'./chunk-SR7R5QYC.mjs';export{i as any,h as array,d as boolean,m as cast,e as date,g as enumSchema,p as extend,f as functionSchema,o as hookOriginal,l as literal,c as number,a as object,j as preprocess,n as s,n as schema,b as string,k as union}from'./chunk-AKXLQP64.mjs';
|
package/dist/full.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';var A={abortEarly:true};function O(e,c){if(!c)return e;let s=e?.cause?.key?`${c}.${e.cause.key}`:`${c}`;return {message:`Error parsing key "${s}": ${e.message}`,cause:{key:s}}}function x(e,c,s){if(e.errors?.length)for(let a=0;a<e.errors.length;a++)c.push(O(e.errors[a],s));}function T(e){return {...A,...e}}var l=e=>typeof e=="string"||e instanceof String,d=e=>(typeof e=="number"||e instanceof Number)&&!Number.isNaN(e),b=e=>e===true||e===false,P=e=>e instanceof Date&&!Number.isNaN(e.getTime()),E=e=>typeof e=="function",_=e=>Array.isArray(e),N=e=>typeof e=="object"&&e!==null&&!Array.isArray(e),p={object(e,c){let s=S(N,{...c,type:"object"});return s._getDescription=()=>`object({ ${Object.entries(e).map(([t,r])=>`${t}: ${r._getDescription()}`).join(", ")} })`,h(s,"_parse",(a,t,r)=>{let n=a(t,r),{abortEarly:o}=T(r);if(n.success===false)return n;let u={},i=[];for(let m in e){let f=e[m]._parse(n.data[m],r);if(f.success)u[m]=f.data;else {if(f=f,o!==false){let I=O(f.error,m);return {success:false,error:I,errors:[I]}}x(f,i,m);}}return i.length>0?{success:false,error:i[0],errors:i}:{success:true,data:u}}),s},string(e){return S(l,{...e,type:"string"})},number(e){return S(d,{...e,type:"number"})},boolean(e){return S(b,{...e,type:"boolean"})},date(e){return S(P,{...e,type:"date"})},function(e){return S(E,{...e,type:"function"})},enum(e,c){let s=n=>e.includes(n),a=n=>`Invalid ${t} value. Expected ${e.map(o=>`"${o}"`).join(" | ")}, received "${n}".`,t="enum",r=S(s,{message:a,...c,type:t});return r._getDescription=()=>`enum(${e.map(n=>`"${n}"`).join(" | ")})`,r},array(e,c){let s=S(_,{...c,type:"array"});return s._getDescription=()=>`array(${e._getDescription()})`,h(s,"_parse",(a,t,r)=>{let n=a(t,r),{abortEarly:o}=T(r);if(n.success===false)return n;let u=[],i=[];for(let m=0;m<n.data.length;m++){let f=e._parse(n.data[m],r);if(f.success)u.push(f.data);else {if(f=f,o!==false){let I=O(f.error,m);return {success:false,error:I,errors:[I]}}x(f,i,m);}}return i.length>0?{success:false,error:i[0],errors:i}:{success:true,data:u}}),s},any(){return S(()=>true)},preprocess(e,c){return h(c,"_parse",(s,a)=>(a=e(a),s(a))),c},union(e,c){return S(r=>{for(let n=0;n<e.length;n++){let o=e[n]._parse(r);if(o.success)return o}return false},{message:r=>`Invalid union value. Expected the value to match one of the schemas:${e.map((n,o)=>` ${o+1}. ${n._getDescription()}`).join(",")} but received "${typeof r}" with value: ${N(r)?JSON.stringify(r):`"${r}"`}`,...c,type:"union"})},literal(e,c){let t=S(r=>r===e,{message:r=>c?.message?typeof c.message=="function"?c.message(r):c.message:`Expected literal value "${e}", received "${r}"`,name:c?.name,type:"literal"});return t._getDescription=()=>`literal("${e}")`,t},coerce:{string(e){return p.preprocess(c=>String(c),p.string(e))},number(e){let c=e?.message??(s=>`Cannot coerce "${s}" to a valid number.`);return p.preprocess(s=>Number(s),p.number({...e,message:c}))},boolean(e){return p.preprocess(c=>!!c,p.boolean(e))},date(e){let c=e?.message??(s=>`Cannot coerce "${s}" to a valid date.`);return p.preprocess(s=>new Date(s),p.date({...e,message:c}))}},cast:{boolean(e){let c=e?.message??(s=>`Cannot cast "${s}" to boolean. Accepted: true/false, 1/0, yes/no, on/off.`);return p.preprocess(s=>{let a;return l(s)&&(a=s.toLowerCase()),a==="true"||a==="yes"||a==="on"||a==="1"||s===1?true:a==="false"||a==="no"||a==="off"||a==="0"||s===0?false:s},p.boolean({...e,message:c}))},number(e){let c=e?.message??(s=>`Cannot cast "${s}" to a number. Expected a numeric string or number.`);return p.preprocess(s=>{if(b(s))return Number(s);if(l(s)){let a=s.trim();if(a==="")return s;let t=Number(a);if(Number.isFinite(t))return t}return s},p.number({...e,message:c}))},string(e){let c=e?.message??(s=>`Cannot cast "${s}" to string. Expected a string, number, or boolean.`);return p.preprocess(s=>b(s)||d(s)&&Number.isFinite(s)?String(s):s,p.string({...e,message:c}))},date(e){let c=e?.message??(s=>`Cannot cast "${s}" to a valid date.`);return p.preprocess(s=>{let a;return l(s)&&(a=s.trim()),d(s)&&Number.isFinite(s)||a?new Date(a??s):s},p.date({...e,message:c}))},json(e,c){let s=c?.message??(a=>`Cannot parse "${a}" as JSON.`);return h(e,"_parse",(a,t)=>{if(l(t))try{t=JSON.parse(t);}catch{let r={message:typeof s=="function"?s(t):s};return {success:false,error:r,errors:[r]}}return a(t)}),e}}};function $(e){return c=>`The value "${c}" must be type of ${e} but is type of "${typeof c}".`}function h(e,c,s){let a=e[c];e[c]=(...t)=>s(a,...t);}function S(e,{type:c="any",name:s,message:a}={}){a=a||$(c);let t={name:s,message:a,type:c},r={_getName(){return s},_getType(){return c},_getDescription(){return this._getName()??this._getType()},_parse(n,o){let u=e(n);if(u===true)return {success:true,data:n};if(typeof u=="object"&&u?.success===true)return u;let i={message:typeof a=="function"?a(n):a};return {success:false,error:i,errors:[i]}},parse(n,o){let u=r._parse(n,o);if(!u.success)throw u=u,new Error(u.error.message,{cause:u.error.cause});return u.data},safeParse(n,o){return r._parse(n,o)},transform(n){return h(this,"_parse",(o,u,i)=>{let m=o(u,i);return m.success&&(m.data=n(m.data)),m}),this},optional(){return h(this,"_parse",(n,o,u)=>{let i=n(o,u);return !i.success&&o===void 0&&(i.data=void 0,i.success=true),i}),this},nullable(){return h(this,"_parse",(n,o,u)=>{let i=n(o,u);return !i.success&&o===null&&(i.data=null,i.success=true),i}),this},nullish(){return h(this,"_parse",(n,o,u)=>{let i=n(o,u);return !i.success&&o==null&&(i.success=true,i.data=o),i}),this},default(n){return h(this,"_parse",(o,u,i)=>(u===void 0&&(u=typeof n=="function"?n():n),o(u,i))),this},catch(n){return h(this,"_parse",(o,u,i)=>{let m=o(u,i);return m.success?m:{success:true,data:typeof n=="function"?n({input:u,error:m.error}):n}}),this},pipe(n){return h(this,"_parse",(o,u,i)=>{let m=o(u,i);return m.success?n._parse(m.data,i):m}),this},refine(n,{message:o,type:u}={}){return u&&(o=o||$(u),c=u),h(this,"_parse",(i,m,f)=>{let I=i(m,f);T(f);if(!I.success)return I;let y=n(I.data);if(y===true||typeof y=="object"&&y?.success===true)return I;let k={message:typeof o=="function"?o(m):o};return {success:false,error:k,errors:[k]}}),this}};return w.length>0?w.reduce((n,o)=>o(n,e,t)??n,r):r}var w=[];function g(e){if(typeof e!="function")throw new TypeError("extend() requires a function argument");w.push(e);}g((e,c,s)=>{if(s?.type==="string"){let a=e;a.min=function(t,{message:r}={}){return this.refine(n=>n.length>=t,{message:r||(n=>`String must be at least ${t} characters long (received ${n.length} characters: "${n}")`)})},a.max=function(t,{message:r}={}){return this.refine(n=>n.length<=t,{message:r||(n=>`String must be at most ${t} characters long (received ${n.length} characters: "${n}")`)})},a.length=function(t,{message:r}={}){return this.refine(n=>n.length===t,{message:r||(n=>`String must be exactly ${t} characters long (received ${n.length} characters: "${n}")`)})},a.nonEmpty=function({message:t}={}){return this.refine(r=>r.length>0,{message:t||"String must not be empty (received empty string)"})},a.startsWith=function(t,{message:r}={}){return this.refine(n=>n.startsWith(t),{message:r||(n=>`String must start with "${t}" (received: "${n}")`)})},a.endsWith=function(t,{message:r}={}){return this.refine(n=>n.endsWith(t),{message:r||(n=>`String must end with "${t}" (received: "${n}")`)})},a.includes=function(t,{message:r}={}){return this.refine(n=>n.includes(t),{message:r||(n=>`String must include "${t}" (received: "${n}")`)})},a.toLowerCase=function(){return this.transform(t=>t.toLowerCase())},a.toUpperCase=function(){return this.transform(t=>t.toUpperCase())},a.trim=function(){return this.transform(t=>t.trim())},a.padStart=function(t,r=" "){return this.transform(n=>n.padStart(t,r))},a.padEnd=function(t,r=" "){return this.transform(n=>n.padEnd(t,r))},a.replace=function(t,r){return this.transform(n=>n.replace(t,r))};}return e});g((e,c,s)=>{if(s?.type==="number"){let a=e;a.min=function(t,{message:r}={}){return this.refine(n=>n>=t,{message:r||`Number must be greater than or equal to ${t}.`})},a.max=function(t,{message:r}={}){return this.refine(n=>n<=t,{message:r||`Number must be less than or equal to ${t}.`})},a.positive=function({message:t}={}){return this.refine(r=>r>0,{message:t||"Number must be positive."})},a.negative=function({message:t}={}){return this.refine(r=>r<0,{message:t||"Number must be negative."})},a.int=function({message:t}={}){return this.refine(r=>Number.isInteger(r),{message:t||"Number must be an integer."})},a.float=function({message:t}={}){return this.refine(r=>Number.isFinite(r)&&!Number.isInteger(r),{message:t||"Number must be a floating point (non-integer)."})},a.multipleOf=function(t,{message:r}={}){return this.refine(n=>n%t===0,{message:r||`Number must be a multiple of ${t}.`})},a.finite=function({message:t}={}){return this.refine(r=>Number.isFinite(r),{message:t||"Number must be finite."})};}return e});g((e,c,s)=>{if(s?.type==="array"){let a=e;a.min=function(t,{message:r}={}){return this.refine(n=>n.length>=t,{message:r||`Array must contain at least ${t} items.`})},a.max=function(t,{message:r}={}){return this.refine(n=>n.length<=t,{message:r||`Array must contain at most ${t} items.`})},a.length=function(t,{message:r}={}){return this.refine(n=>n.length===t,{message:r||`Array must contain exactly ${t} items.`})},a.nonEmpty=function({message:t}={}){return this.refine(r=>r.length>0,{message:t||"Array must not be empty."})},a.unique=function({message:t}={}){return this.refine(r=>{let n=new Set;try{return r.every(o=>{let u=JSON.stringify(o);return n.has(u)?!1:(n.add(u),!0)})}catch{return new Set(r).size===r.length}},{message:t||"Array items must be unique."})},a.sort=function(){return this.transform(t=>[...t].sort())},a.reverse=function(){return this.transform(t=>[...t].reverse())};}return e});exports.extend=g;exports.hookOriginal=h;exports.s=p;exports.schema=p;
|
|
1
|
+
'use strict';var R={abortEarly:true};function N(e,s){if(!s)return e;let c=e?.cause?.key?`${s}.${e.cause.key}`:`${s}`;return {message:`Error parsing key "${c}": ${e.message}`,cause:{key:c}}}function E(e,s,c){if(e.errors?.length)for(let a=0;a<e.errors.length;a++)s.push(N(e.errors[a],c));}function $(e){return e?.abortEarly!==void 0?e:R}var g=e=>typeof e=="string"||e instanceof String,w=e=>(typeof e=="number"||e instanceof Number)&&!Number.isNaN(e),k=e=>e===true||e===false,j=e=>e instanceof Date&&!Number.isNaN(e.getTime()),C=e=>typeof e=="function",F=e=>Array.isArray(e),_=e=>typeof e=="object"&&e!==null&&!Array.isArray(e);function v(e,s){let c=h(_,{...s,type:"object"});return c._getDescription=()=>`object({ ${Object.entries(e).map(([t,r])=>`${t}: ${r._getDescription()}`).join(", ")} })`,p(c,"_parse",(a,t,r)=>{let n=a(t,r),{abortEarly:o}=$(r);if(n.success===false)return n;let u={},i=[];for(let m in e){let f=e[m]._parse(n.data[m],r);if(f.success)u[m]=f.data;else {if(f=f,o!==false){let S=N(f.error,m);return {success:false,error:S,errors:[S]}}E(f,i,m);}}return i.length>0?{success:false,error:i[0],errors:i}:{success:true,data:u}}),c}function y(e){return h(g,{...e,type:"string"})}function d(e){return h(w,{...e,type:"number"})}function b(e){return h(k,{...e,type:"boolean"})}function O(e){return h(j,{...e,type:"date"})}function V(e){return h(C,{...e,type:"function"})}function B(e,s){let c=n=>e.includes(n),a=n=>`Invalid ${t} value. Expected ${e.map(o=>`"${o}"`).join(" | ")}, received "${n}".`,t="enum",r=h(c,{message:a,...s,type:t});return r._getDescription=()=>`enum(${e.map(n=>`"${n}"`).join(" | ")})`,r}function L(e,s){let c=h(F,{...s,type:"array"});return c._getDescription=()=>`array(${e._getDescription()})`,p(c,"_parse",(a,t,r)=>{let n=a(t,r),{abortEarly:o}=$(r);if(n.success===false)return n;let u=[],i=[];for(let m=0;m<n.data.length;m++){let f=e._parse(n.data[m],r);if(f.success)u.push(f.data);else {if(f=f,o!==false){let S=N(f.error,m);return {success:false,error:S,errors:[S]}}E(f,i,m);}}return i.length>0?{success:false,error:i[0],errors:i}:{success:true,data:u}}),c}function M(){return h(()=>true)}function I(e,s){return p(s,"_parse",(c,a)=>(a=e(a),c(a))),s}function U(e,s){return h(r=>{for(let n=0;n<e.length;n++){let o=e[n]._parse(r);if(o.success)return o}return false},{message:r=>`Invalid union value. Expected the value to match one of the schemas:${e.map((n,o)=>` ${o+1}. ${n._getDescription()}`).join(",")} but received "${typeof r}" with value: ${_(r)?JSON.stringify(r):`"${r}"`}`,...s,type:"union"})}function q(e,s){let t=h(r=>r===e,{message:r=>s?.message?typeof s.message=="function"?s.message(r):s.message:`Expected literal value "${e}", received "${r}"`,name:s?.name,type:"literal"});return t._getDescription=()=>`literal("${e}")`,t}var W={boolean(e){let s=e?.message??(c=>`Cannot cast "${c}" to boolean. Accepted: true/false, 1/0, yes/no, on/off.`);return I(c=>{let a;return g(c)&&(a=c.toLowerCase()),a==="true"||a==="yes"||a==="on"||a==="1"||c===1?true:a==="false"||a==="no"||a==="off"||a==="0"||c===0?false:c},b({...e,message:s}))},number(e){let s=e?.message??(c=>`Cannot cast "${c}" to a number. Expected a numeric string or number.`);return I(c=>{if(k(c))return Number(c);if(g(c)){let a=c.trim();if(a==="")return c;let t=Number(a);if(Number.isFinite(t))return t}return c},d({...e,message:s}))},string(e){let s=e?.message??(c=>`Cannot cast "${c}" to string. Expected a string, number, or boolean.`);return I(c=>k(c)||w(c)&&Number.isFinite(c)?String(c):c,y({...e,message:s}))},date(e){let s=e?.message??(c=>`Cannot cast "${c}" to a valid date.`);return I(c=>{let a;return g(c)&&(a=c.trim()),w(c)&&Number.isFinite(c)||a?new Date(a??c):c},O({...e,message:s}))},json(e,s){let c=s?.message??(a=>`Cannot parse "${a}" as JSON.`);return p(e,"_parse",(a,t)=>{if(g(t))try{t=JSON.parse(t);}catch{let r={message:typeof c=="function"?c(t):c};return {success:false,error:r,errors:[r]}}return a(t)}),e}},D={object:v,string:y,number:d,boolean:b,date:O,function:V,enum:B,array:L,any:M,preprocess:I,union:U,literal:q,coerce:{},cast:W};function P(e){return s=>`The value "${s}" must be type of ${e} but is type of "${typeof s}".`}function p(e,s,c){let a=e[s];e[s]=(...t)=>c(a,...t);}function h(e,{type:s="any",name:c,message:a}={}){a=a||P(s);let t={name:c,message:a,type:s},r={_getName(){return c},_getType(){return s},_getDescription(){return this._getName()??this._getType()},_parse(n,o){let u=e(n);if(u===true)return {success:true,data:n};if(typeof u=="object"&&u?.success===true)return u;let i={message:typeof a=="function"?a(n):a};return {success:false,error:i,errors:[i]}},parse(n,o){let u=r._parse(n,o);if(!u.success)throw u=u,new Error(u.error.message,{cause:u.error.cause});return u.data},safeParse(n,o){return r._parse(n,o)},transform(n){return p(this,"_parse",(o,u,i)=>{let m=o(u,i);return m.success&&(m.data=n(m.data)),m}),this},optional(){return p(this,"_parse",(n,o,u)=>{let i=n(o,u);return !i.success&&o===void 0&&(i.data=void 0,i.success=true),i}),this},nullable(){return p(this,"_parse",(n,o,u)=>{let i=n(o,u);return !i.success&&o===null&&(i.data=null,i.success=true),i}),this},nullish(){return p(this,"_parse",(n,o,u)=>{let i=n(o,u);return !i.success&&o==null&&(i.success=true,i.data=o),i}),this},default(n){return p(this,"_parse",(o,u,i)=>(u===void 0&&(u=typeof n=="function"?n():n),o(u,i))),this},catch(n){return p(this,"_parse",(o,u,i)=>{let m=o(u,i);return m.success?m:{success:true,data:typeof n=="function"?n({input:u,error:m.error}):n}}),this},pipe(n){return p(this,"_parse",(o,u,i)=>{let m=o(u,i);return m.success?n._parse(m.data,i):m}),this},refine(n,{message:o,type:u}={}){return u&&(o=o||P(u),s=u),p(this,"_parse",(i,m,f)=>{let S=i(m,f),{abortEarly:J}=$(f);if(!S.success)return S;let T=n(S.data);if(T===true||typeof T=="object"&&T?.success===true)return S;let A={message:typeof o=="function"?o(m):o};return {success:false,error:A,errors:[A]}}),this}};return x.length>0?x.reduce((n,o)=>o(n,e,t)??n,r):r}var x=[];function l(e){if(typeof e!="function")throw new TypeError("extend() requires a function argument");x.push(e);}l((e,s,c)=>{if(c?.type==="string"){let a=e;a.min=function(t,{message:r}={}){return this.refine(n=>n.length>=t,{message:r||(n=>`String must be at least ${t} characters long (received ${n.length} characters: "${n}")`)})},a.max=function(t,{message:r}={}){return this.refine(n=>n.length<=t,{message:r||(n=>`String must be at most ${t} characters long (received ${n.length} characters: "${n}")`)})},a.length=function(t,{message:r}={}){return this.refine(n=>n.length===t,{message:r||(n=>`String must be exactly ${t} characters long (received ${n.length} characters: "${n}")`)})},a.nonEmpty=function({message:t}={}){return this.refine(r=>r.length>0,{message:t||"String must not be empty (received empty string)"})},a.startsWith=function(t,{message:r}={}){return this.refine(n=>n.startsWith(t),{message:r||(n=>`String must start with "${t}" (received: "${n}")`)})},a.endsWith=function(t,{message:r}={}){return this.refine(n=>n.endsWith(t),{message:r||(n=>`String must end with "${t}" (received: "${n}")`)})},a.includes=function(t,{message:r}={}){return this.refine(n=>n.includes(t),{message:r||(n=>`String must include "${t}" (received: "${n}")`)})},a.toLowerCase=function(){return this.transform(t=>t.toLowerCase())},a.toUpperCase=function(){return this.transform(t=>t.toUpperCase())},a.trim=function(){return this.transform(t=>t.trim())},a.padStart=function(t,r=" "){return this.transform(n=>n.padStart(t,r))},a.padEnd=function(t,r=" "){return this.transform(n=>n.padEnd(t,r))},a.replace=function(t,r){return this.transform(n=>n.replace(t,r))};}return e});l((e,s,c)=>{if(c?.type==="number"){let a=e;a.min=function(t,{message:r}={}){return this.refine(n=>n>=t,{message:r||`Number must be greater than or equal to ${t}.`})},a.max=function(t,{message:r}={}){return this.refine(n=>n<=t,{message:r||`Number must be less than or equal to ${t}.`})},a.positive=function({message:t}={}){return this.refine(r=>r>0,{message:t||"Number must be positive."})},a.negative=function({message:t}={}){return this.refine(r=>r<0,{message:t||"Number must be negative."})},a.int=function({message:t}={}){return this.refine(r=>Number.isInteger(r),{message:t||"Number must be an integer."})},a.float=function({message:t}={}){return this.refine(r=>Number.isFinite(r)&&!Number.isInteger(r),{message:t||"Number must be a floating point (non-integer)."})},a.multipleOf=function(t,{message:r}={}){return this.refine(n=>n%t===0,{message:r||`Number must be a multiple of ${t}.`})},a.finite=function({message:t}={}){return this.refine(r=>Number.isFinite(r),{message:t||"Number must be finite."})};}return e});l((e,s,c)=>{if(c?.type==="array"){let a=e;a.min=function(t,{message:r}={}){return this.refine(n=>n.length>=t,{message:r||`Array must contain at least ${t} items.`})},a.max=function(t,{message:r}={}){return this.refine(n=>n.length<=t,{message:r||`Array must contain at most ${t} items.`})},a.length=function(t,{message:r}={}){return this.refine(n=>n.length===t,{message:r||`Array must contain exactly ${t} items.`})},a.nonEmpty=function({message:t}={}){return this.refine(r=>r.length>0,{message:t||"Array must not be empty."})},a.unique=function({message:t}={}){return this.refine(r=>{let n=new Set;try{return r.every(o=>{let u=JSON.stringify(o);return n.has(u)?!1:(n.add(u),!0)})}catch{return new Set(r).size===r.length}},{message:t||"Array items must be unique."})},a.sort=function(){return this.transform(t=>[...t].sort())},a.reverse=function(){return this.transform(t=>[...t].reverse())};}return e});Object.assign(D.coerce,{string(e){return I(s=>String(s),y(e))},number(e){let s=e?.message??(c=>`Cannot coerce "${c}" to a valid number.`);return I(c=>Number(c),d({...e,message:s}))},boolean(e){return I(s=>!!s,b(e))},date(e){let s=e?.message??(c=>`Cannot coerce "${c}" to a valid date.`);return I(c=>new Date(c),O({...e,message:s}))}});exports.any=M;exports.array=L;exports.boolean=b;exports.cast=W;exports.date=O;exports.enumSchema=B;exports.extend=l;exports.functionSchema=V;exports.hookOriginal=p;exports.literal=q;exports.number=d;exports.object=v;exports.preprocess=I;exports.s=D;exports.schema=D;exports.string=y;exports.union=U;
|
package/dist/full.d.mts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import './string.mjs';
|
|
2
2
|
import './number.mjs';
|
|
3
3
|
import './array.mjs';
|
|
4
|
-
|
|
4
|
+
import './coerce.mjs';
|
|
5
|
+
export { ArraySchemaInterface, BooleanSchemaInterface, CoerceInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, FunctionSchemaInterface, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, any, array, boolean, cast, date, enumSchema, extend, functionSchema, hookOriginal, literal, number, object, preprocess, s, s as schema, string, union } from './index.mjs';
|
package/dist/full.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import './string.js';
|
|
2
2
|
import './number.js';
|
|
3
3
|
import './array.js';
|
|
4
|
-
|
|
4
|
+
import './coerce.js';
|
|
5
|
+
export { ArraySchemaInterface, BooleanSchemaInterface, CoerceInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, FunctionSchemaInterface, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, any, array, boolean, cast, date, enumSchema, extend, functionSchema, hookOriginal, literal, number, object, preprocess, s, s as schema, string, union } from './index.js';
|
package/dist/full.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import'./chunk-
|
|
1
|
+
import'./chunk-AQFELD23.mjs';import'./chunk-SR7R5QYC.mjs';import'./chunk-HVQY2UKM.mjs';import'./chunk-7VTVE334.mjs';export{i as any,h as array,d as boolean,m as cast,e as date,g as enumSchema,p as extend,f as functionSchema,o as hookOriginal,l as literal,c as number,a as object,j as preprocess,n as s,n as schema,b as string,k as union}from'./chunk-AKXLQP64.mjs';
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';var
|
|
1
|
+
'use strict';var N={abortEarly:true};function T(e,a){if(!a)return e;let n=e?.cause?.key?`${a}.${e.cause.key}`:`${a}`;return {message:`Error parsing key "${n}": ${e.message}`,cause:{key:n}}}function w(e,a,n){if(e.errors?.length)for(let r=0;r<e.errors.length;r++)a.push(T(e.errors[r],n));}function O(e){return e?.abortEarly!==void 0?e:N}var l=e=>typeof e=="string"||e instanceof String,y=e=>(typeof e=="number"||e instanceof Number)&&!Number.isNaN(e),g=e=>e===true||e===false,R=e=>e instanceof Date&&!Number.isNaN(e.getTime()),A=e=>typeof e=="function",j=e=>Array.isArray(e),P=e=>typeof e=="object"&&e!==null&&!Array.isArray(e);function F(e,a){let n=h(P,{...a,type:"object"});return n._getDescription=()=>`object({ ${Object.entries(e).map(([p,o])=>`${p}: ${o._getDescription()}`).join(", ")} })`,m(n,"_parse",(r,p,o)=>{let t=r(p,o),{abortEarly:s}=O(o);if(t.success===false)return t;let u={},c=[];for(let i in e){let f=e[i]._parse(t.data[i],o);if(f.success)u[i]=f.data;else {if(f=f,s!==false){let I=T(f.error,i);return {success:false,error:I,errors:[I]}}w(f,c,i);}}return c.length>0?{success:false,error:c[0],errors:c}:{success:true,data:u}}),n}function E(e){return h(l,{...e,type:"string"})}function _(e){return h(y,{...e,type:"number"})}function D(e){return h(g,{...e,type:"boolean"})}function $(e){return h(R,{...e,type:"date"})}function C(e){return h(A,{...e,type:"function"})}function V(e,a){let n=t=>e.includes(t),r=t=>`Invalid ${p} value. Expected ${e.map(s=>`"${s}"`).join(" | ")}, received "${t}".`,p="enum",o=h(n,{message:r,...a,type:p});return o._getDescription=()=>`enum(${e.map(t=>`"${t}"`).join(" | ")})`,o}function L(e,a){let n=h(j,{...a,type:"array"});return n._getDescription=()=>`array(${e._getDescription()})`,m(n,"_parse",(r,p,o)=>{let t=r(p,o),{abortEarly:s}=O(o);if(t.success===false)return t;let u=[],c=[];for(let i=0;i<t.data.length;i++){let f=e._parse(t.data[i],o);if(f.success)u.push(f.data);else {if(f=f,s!==false){let I=T(f.error,i);return {success:false,error:I,errors:[I]}}w(f,c,i);}}return c.length>0?{success:false,error:c[0],errors:c}:{success:true,data:u}}),n}function M(){return h(()=>true)}function d(e,a){return m(a,"_parse",(n,r)=>(r=e(r),n(r))),a}function B(e,a){return h(o=>{for(let t=0;t<e.length;t++){let s=e[t]._parse(o);if(s.success)return s}return false},{message:o=>`Invalid union value. Expected the value to match one of the schemas:${e.map((t,s)=>` ${s+1}. ${t._getDescription()}`).join(",")} but received "${typeof o}" with value: ${P(o)?JSON.stringify(o):`"${o}"`}`,...a,type:"union"})}function U(e,a){let p=h(o=>o===e,{message:o=>a?.message?typeof a.message=="function"?a.message(o):a.message:`Expected literal value "${e}", received "${o}"`,name:a?.name,type:"literal"});return p._getDescription=()=>`literal("${e}")`,p}var J={boolean(e){let a=e?.message??(n=>`Cannot cast "${n}" to boolean. Accepted: true/false, 1/0, yes/no, on/off.`);return d(n=>{let r;return l(n)&&(r=n.toLowerCase()),r==="true"||r==="yes"||r==="on"||r==="1"||n===1?true:r==="false"||r==="no"||r==="off"||r==="0"||n===0?false:n},D({...e,message:a}))},number(e){let a=e?.message??(n=>`Cannot cast "${n}" to a number. Expected a numeric string or number.`);return d(n=>{if(g(n))return Number(n);if(l(n)){let r=n.trim();if(r==="")return n;let p=Number(r);if(Number.isFinite(p))return p}return n},_({...e,message:a}))},string(e){let a=e?.message??(n=>`Cannot cast "${n}" to string. Expected a string, number, or boolean.`);return d(n=>g(n)||y(n)&&Number.isFinite(n)?String(n):n,E({...e,message:a}))},date(e){let a=e?.message??(n=>`Cannot cast "${n}" to a valid date.`);return d(n=>{let r;return l(n)&&(r=n.trim()),y(n)&&Number.isFinite(n)||r?new Date(r??n):n},$({...e,message:a}))},json(e,a){let n=a?.message??(r=>`Cannot parse "${r}" as JSON.`);return m(e,"_parse",(r,p)=>{if(l(p))try{p=JSON.parse(p);}catch{let o={message:typeof n=="function"?n(p):n};return {success:false,error:o,errors:[o]}}return r(p)}),e}},G={object:F,string:E,number:_,boolean:D,date:$,function:C,enum:V,array:L,any:M,preprocess:d,union:B,literal:U,coerce:{},cast:J};function x(e){return a=>`The value "${a}" must be type of ${e} but is type of "${typeof a}".`}function m(e,a,n){let r=e[a];e[a]=(...p)=>n(r,...p);}function h(e,{type:a="any",name:n,message:r}={}){r=r||x(a);let p={name:n,message:r,type:a},o={_getName(){return n},_getType(){return a},_getDescription(){return this._getName()??this._getType()},_parse(t,s){let u=e(t);if(u===true)return {success:true,data:t};if(typeof u=="object"&&u?.success===true)return u;let c={message:typeof r=="function"?r(t):r};return {success:false,error:c,errors:[c]}},parse(t,s){let u=o._parse(t,s);if(!u.success)throw u=u,new Error(u.error.message,{cause:u.error.cause});return u.data},safeParse(t,s){return o._parse(t,s)},transform(t){return m(this,"_parse",(s,u,c)=>{let i=s(u,c);return i.success&&(i.data=t(i.data)),i}),this},optional(){return m(this,"_parse",(t,s,u)=>{let c=t(s,u);return !c.success&&s===void 0&&(c.data=void 0,c.success=true),c}),this},nullable(){return m(this,"_parse",(t,s,u)=>{let c=t(s,u);return !c.success&&s===null&&(c.data=null,c.success=true),c}),this},nullish(){return m(this,"_parse",(t,s,u)=>{let c=t(s,u);return !c.success&&s==null&&(c.success=true,c.data=s),c}),this},default(t){return m(this,"_parse",(s,u,c)=>(u===void 0&&(u=typeof t=="function"?t():t),s(u,c))),this},catch(t){return m(this,"_parse",(s,u,c)=>{let i=s(u,c);return i.success?i:{success:true,data:typeof t=="function"?t({input:u,error:i.error}):t}}),this},pipe(t){return m(this,"_parse",(s,u,c)=>{let i=s(u,c);return i.success?t._parse(i.data,c):i}),this},refine(t,{message:s,type:u}={}){return u&&(s=s||x(u),a=u),m(this,"_parse",(c,i,f)=>{let I=c(i,f),{abortEarly:q}=O(f);if(!I.success)return I;let S=t(I.data);if(S===true||typeof S=="object"&&S?.success===true)return I;let k={message:typeof s=="function"?s(i):s};return {success:false,error:k,errors:[k]}}),this}};return b.length>0?b.reduce((t,s)=>s(t,e,p)??t,o):o}var b=[];function H(e){if(typeof e!="function")throw new TypeError("extend() requires a function argument");b.push(e);}exports.any=M;exports.array=L;exports.boolean=D;exports.cast=J;exports.date=$;exports.enumSchema=V;exports.extend=H;exports.functionSchema=C;exports.hookOriginal=m;exports.literal=U;exports.number=_;exports.object=F;exports.preprocess=d;exports.s=G;exports.schema=G;exports.string=E;exports.union=B;
|