@esmj/schema 0.7.4 → 0.8.2
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 +56 -3
- 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-NRGPACGD.mjs → chunk-7VTVE334.mjs} +1 -1
- package/dist/chunk-AKXLQP64.mjs +1 -0
- package/dist/{chunk-5XWCCG75.mjs → chunk-AQFELD23.mjs} +1 -1
- package/dist/{chunk-NCRS6NBV.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 +4 -13
- package/dist/index.d.ts +4 -13
- 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 +13 -4
- package/dist/chunk-37YJYCTR.mjs +0 -1
package/README.md
CHANGED
|
@@ -14,6 +14,7 @@ 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)
|
|
18
19
|
- [Named Exports & Tree-Shaking](#named-exports--tree-shaking)
|
|
19
20
|
- [API Reference Summary](#api-reference-summary)
|
|
@@ -127,6 +128,7 @@ When choosing a schema validation library, bundle size can be an important facto
|
|
|
127
128
|
| @sinclair/typebox | ~11.7 KB |
|
|
128
129
|
| Yup | ~12.2 KB |
|
|
129
130
|
| Zod@3 | ~13 KB |
|
|
131
|
+
| Valibot | ~13.5 KB |
|
|
130
132
|
| @zod/mini | ~20.5 KB |
|
|
131
133
|
| AJV | ~31.4 KB |
|
|
132
134
|
| Joi | ~40.4 KB |
|
|
@@ -151,6 +153,7 @@ When choosing a schema validation library, bundle size can be an important facto
|
|
|
151
153
|
| `@sinclair/typebox` | `4 370 618.49 ± 1.23%` | `0.23 ± 1.23%` |
|
|
152
154
|
| ArkType | 16 282.69 ± 4.14% | 61.61 ± 4.38% |
|
|
153
155
|
| effect/Schema | 24 919.15 ± 4.31% | 40.31 ± 4.78% |
|
|
156
|
+
| Valibot | 288 143.23 ± 1.93% | 3.47 ± 2.00% |
|
|
154
157
|
| AJV | 240.32 ± 4.67% | 4 164.31 ± 2.04% |
|
|
155
158
|
|
|
156
159
|
#### Parsing Performance
|
|
@@ -166,6 +169,7 @@ When choosing a schema validation library, bundle size can be an important facto
|
|
|
166
169
|
| @sinclair/typebox | 228 711.62 ± 2.03% | 4.38 ± 2.23% |
|
|
167
170
|
| ArkType | 1 677 066.00 ± 0.58% | 0.60 ± 0.59% |
|
|
168
171
|
| effect/Schema | 1 060 056.14 ± 0.61% | 0.94 ± 0.61% |
|
|
172
|
+
| Valibot | 1 789 204.27 ± 0.49% | 0.56 ± 0.49% |
|
|
169
173
|
| `AJV` | `33 620 146.24 ± 1.19%` | `0.03 ± 1.26%` |
|
|
170
174
|
|
|
171
175
|
#### Error Handling Performance
|
|
@@ -181,6 +185,7 @@ When choosing a schema validation library, bundle size can be an important facto
|
|
|
181
185
|
| @sinclair/typebox | 228 734.49 ± 0.55% | 4.37 ± 0.56% |
|
|
182
186
|
| ArkType | 258 685.33 ± 1.23% | 3.87 ± 1.23% |
|
|
183
187
|
| effect/Schema | 165 753.69 ± 0.99% | 6.03 ± 1.00% |
|
|
188
|
+
| Valibot | 1 401 537.68 ± 1.02% | 0.71 ± 1.02% |
|
|
184
189
|
| `AJV` | `19 693 821.79 ± 1.81%` | `0.05 ± 1.83%` |
|
|
185
190
|
|
|
186
191
|
**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.
|
|
@@ -253,9 +258,13 @@ import { s } from '@esmj/schema/number';
|
|
|
253
258
|
// Array extensions only
|
|
254
259
|
import { s } from '@esmj/schema/array';
|
|
255
260
|
|
|
261
|
+
// Coerce extensions only
|
|
262
|
+
import { s } from '@esmj/schema/coerce';
|
|
263
|
+
|
|
256
264
|
// Mix and match (side-effect imports)
|
|
257
265
|
import '@esmj/schema/string';
|
|
258
266
|
import '@esmj/schema/number';
|
|
267
|
+
import '@esmj/schema/coerce';
|
|
259
268
|
import { s } from '@esmj/schema';
|
|
260
269
|
|
|
261
270
|
// Tree-shakeable named exports — bundle only what you use
|
|
@@ -272,6 +281,7 @@ import { functionSchema, enumSchema } from '@esmj/schema';
|
|
|
272
281
|
- **String extensions** (`@esmj/schema/string`): +~0.8 KB
|
|
273
282
|
- **Number extensions** (`@esmj/schema/number`): +~0.6 KB
|
|
274
283
|
- **Array extensions** (`@esmj/schema/array`): +~0.5 KB
|
|
284
|
+
- **Coerce extensions** (`@esmj/schema/coerce`): +~0.3 KB
|
|
275
285
|
- **Full** (`@esmj/schema/full`): ~4 KB gzipped (all extensions)
|
|
276
286
|
|
|
277
287
|
**Recommendation:** Import only the extensions you need to minimize bundle size.
|
|
@@ -300,10 +310,19 @@ const ageSchema = number();
|
|
|
300
310
|
The `coerce` and `cast` namespaces are also individually exported:
|
|
301
311
|
|
|
302
312
|
```typescript
|
|
303
|
-
|
|
313
|
+
// coerce is a named export from @esmj/schema but s.coerce methods
|
|
314
|
+
// require importing '@esmj/schema/coerce' (or '@esmj/schema/full') first:
|
|
315
|
+
import '@esmj/schema/coerce';
|
|
316
|
+
import { s } from '@esmj/schema';
|
|
304
317
|
|
|
305
|
-
const schema = coerce.number();
|
|
318
|
+
const schema = s.coerce.number();
|
|
306
319
|
schema.parse('42'); // 42
|
|
320
|
+
|
|
321
|
+
// Or import the standalone coerce object directly:
|
|
322
|
+
import { coerce } from '@esmj/schema/coerce';
|
|
323
|
+
|
|
324
|
+
const schema2 = coerce.number();
|
|
325
|
+
schema2.parse('42'); // 42
|
|
307
326
|
```
|
|
308
327
|
|
|
309
328
|
Because `function` and `enum` are reserved words in JavaScript, their standalone names are `functionSchema` and `enumSchema`:
|
|
@@ -415,6 +434,36 @@ tagsSchema.parse({
|
|
|
415
434
|
- **Content validations**: `unique()`
|
|
416
435
|
- **Transformations**: `sort()`, `reverse()`
|
|
417
436
|
|
|
437
|
+
### Coerce Extensions (`@esmj/schema/coerce`)
|
|
438
|
+
|
|
439
|
+
Coerce extensions add `s.coerce` methods that apply native JS constructors before validation, providing convenient type coercion with clear error messages.
|
|
440
|
+
|
|
441
|
+
> **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.
|
|
442
|
+
|
|
443
|
+
```typescript
|
|
444
|
+
import { s } from '@esmj/schema/coerce';
|
|
445
|
+
|
|
446
|
+
s.coerce.number().parse('42'); // 42
|
|
447
|
+
s.coerce.string().parse(123); // '123'
|
|
448
|
+
s.coerce.boolean().parse(0); // false
|
|
449
|
+
s.coerce.date().parse('2024-01-01'); // Date object
|
|
450
|
+
|
|
451
|
+
// Coerce then chain schema methods:
|
|
452
|
+
s.coerce.number().refine((v) => v > 0, { message: 'Must be positive' }).parse('5'); // 5
|
|
453
|
+
|
|
454
|
+
// Custom error message:
|
|
455
|
+
s.coerce.number({ message: 'Expected a numeric value' }).parse('bad'); // throws: Expected a numeric value
|
|
456
|
+
```
|
|
457
|
+
|
|
458
|
+
**Available Coerce Methods:**
|
|
459
|
+
|
|
460
|
+
| Method | Coercion | Fails when |
|
|
461
|
+
|---|---|---|
|
|
462
|
+
| `s.coerce.string(options?)` | `String(v)` | Never |
|
|
463
|
+
| `s.coerce.number(options?)` | `Number(v)` | Result is `NaN` |
|
|
464
|
+
| `s.coerce.boolean(options?)` | `Boolean(v)` | Never |
|
|
465
|
+
| `s.coerce.date(options?)` | `new Date(v)` | Result is an invalid Date |
|
|
466
|
+
|
|
418
467
|
### Full Extensions (`@esmj/schema/full`)
|
|
419
468
|
|
|
420
469
|
The full version includes all string, number, and array extensions in a single import.
|
|
@@ -491,7 +540,7 @@ All factory functions below are available both as methods on `s` **and** as indi
|
|
|
491
540
|
- `s.union(schemas)` / `import { union }` - Union validation
|
|
492
541
|
- `s.any()` / `import { any }` - Any type
|
|
493
542
|
- `s.preprocess(fn, schema)` / `import { preprocess }` - Preprocess before validation
|
|
494
|
-
- `s.coerce` / `import { coerce }` - Coerce namespace
|
|
543
|
+
- `s.coerce` / `import { coerce } from '@esmj/schema/coerce'` - Coerce namespace (requires `@esmj/schema/coerce` import)
|
|
495
544
|
- `s.cast` / `import { cast }` - Cast namespace
|
|
496
545
|
|
|
497
546
|
### Modifiers
|
|
@@ -504,6 +553,8 @@ All factory functions below are available both as methods on `s` **and** as indi
|
|
|
504
553
|
|
|
505
554
|
### Coerce
|
|
506
555
|
|
|
556
|
+
> Requires `import '@esmj/schema/coerce'` or `import '@esmj/schema/full'`. `s.coerce` is empty without this import.
|
|
557
|
+
|
|
507
558
|
- `s.coerce.string()` - Coerce any value to string, then validate
|
|
508
559
|
- `s.coerce.number()` - Coerce any value to number, then validate (fails for NaN)
|
|
509
560
|
- `s.coerce.boolean()` - Coerce any value to boolean, then validate
|
|
@@ -830,6 +881,8 @@ const preprocessSchema = s.preprocess((value) => new Date(value), s.date());
|
|
|
830
881
|
|
|
831
882
|
#### `s.coerce`
|
|
832
883
|
|
|
884
|
+
> **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.
|
|
885
|
+
|
|
833
886
|
The `coerce` namespace applies a native JS constructor to the input **before** validation.
|
|
834
887
|
Unlike `s.preprocess`, you don't need to write the conversion yourself, and coerce methods
|
|
835
888
|
provide clear, specific error messages when coercion produces an invalid result.
|
package/dist/array.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';var N={abortEarly:true};function b(e,
|
|
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, any, array, boolean, cast,
|
|
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, any, array, boolean, cast,
|
|
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 D={abortEarly:true};function T(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(T(e.errors[a],c));}function w(e){return e?.abortEarly!==void 0?e:D}var g=e=>typeof e=="string"||e instanceof String,d=e=>(typeof e=="number"||e instanceof Number)&&!Number.isNaN(e),b=e=>e===true||e===false,R=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,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}=w(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=T(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 k(e){return h(g,{...e,type:"string"})}function x(e){return h(d,{...e,type:"number"})}function N(e){return h(b,{...e,type:"boolean"})}function $(e){return h(R,{...e,type:"date"})}function v(e){return h(j,{...e,type:"function"})}function V(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}=w(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=T(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 B(){return h(()=>true)}function I(e,s){return p(s,"_parse",(c,a)=>(a=e(a),c(a))),s}function M(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 U(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 q={string(e){return I(s=>String(s),k(e))},number(e){let s=e?.message??(c=>`Cannot coerce "${c}" to a valid number.`);return I(c=>Number(c),x({...e,message:s}))},boolean(e){return I(s=>!!s,N(e))},date(e){let s=e?.message??(c=>`Cannot coerce "${c}" to a valid date.`);return I(c=>new Date(c),$({...e,message:s}))}},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},N({...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(b(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},x({...e,message:s}))},string(e){let s=e?.message??(c=>`Cannot cast "${c}" to string. Expected a string, number, or boolean.`);return I(c=>b(c)||d(c)&&Number.isFinite(c)?String(c):c,k({...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()),d(c)&&Number.isFinite(c)||a?new Date(a??c):c},$({...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}},G={object:C,string:k,number:x,boolean:N,date:$,function:v,enum:V,array:L,any:B,preprocess:I,union:M,literal:U,coerce:q,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}=w(f);if(!S.success)return S;let y=n(S.data);if(y===true||typeof y=="object"&&y?.success===true)return S;let A={message:typeof o=="function"?o(m):o};return {success:false,error:A,errors:[A]}}),this}};return O.length>0?O.reduce((n,o)=>o(n,e,t)??n,r):r}var O=[];function l(e){if(typeof e!="function")throw new TypeError("extend() requires a function argument");O.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});exports.any=B;exports.array=L;exports.boolean=N;exports.cast=W;exports.coerce=q;exports.date=$;exports.enumSchema=V;exports.extend=l;exports.functionSchema=v;exports.hookOriginal=p;exports.literal=U;exports.number=x;exports.object=C;exports.preprocess=I;exports.s=G;exports.schema=G;exports.string=k;exports.union=M;
|
|
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 N={abortEarly:true};function T(e,
|
|
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;
|
package/dist/index.d.mts
CHANGED
|
@@ -55,6 +55,8 @@ interface FunctionSchemaInterface extends SchemaInterface<Function, Function> {
|
|
|
55
55
|
}
|
|
56
56
|
interface ArraySchemaInterface<T extends SchemaType> extends SchemaInterface<Array<ReturnType<T['parse']>>, Array<ReturnType<T['parse']>>> {
|
|
57
57
|
}
|
|
58
|
+
interface CoerceInterface {
|
|
59
|
+
}
|
|
58
60
|
interface ObjectSchemaInterface<T extends Record<string, SchemaType>> extends SchemaInterface<{
|
|
59
61
|
[Property in keyof T]: ReturnType<T[Property]['parse']>;
|
|
60
62
|
}, {
|
|
@@ -87,12 +89,6 @@ declare function any(): SchemaInterface<unknown, unknown>;
|
|
|
87
89
|
declare function preprocess<T extends SchemaType>(callback: Function, schema: T): T;
|
|
88
90
|
declare function union<T extends Array<SchemaType>>(definitions: T, options?: SchemaInterfaceOptions): UnionSchemaInterface<T>;
|
|
89
91
|
declare function literal<T extends string | number | boolean>(value: T, options?: SchemaInterfaceOptions): LiteralSchemaInterface<T>;
|
|
90
|
-
declare const coerce: {
|
|
91
|
-
string(options?: SchemaInterfaceOptions): StringSchemaInterface;
|
|
92
|
-
number(options?: SchemaInterfaceOptions): NumberSchemaInterface;
|
|
93
|
-
boolean(options?: SchemaInterfaceOptions): BooleanSchemaInterface;
|
|
94
|
-
date(options?: SchemaInterfaceOptions): DateSchemaInterface;
|
|
95
|
-
};
|
|
96
92
|
declare const cast: {
|
|
97
93
|
boolean(options?: SchemaInterfaceOptions): BooleanSchemaInterface;
|
|
98
94
|
number(options?: SchemaInterfaceOptions): NumberSchemaInterface;
|
|
@@ -113,12 +109,7 @@ declare const s: {
|
|
|
113
109
|
preprocess: typeof preprocess;
|
|
114
110
|
union: typeof union;
|
|
115
111
|
literal: typeof literal;
|
|
116
|
-
coerce:
|
|
117
|
-
string(options?: SchemaInterfaceOptions): StringSchemaInterface;
|
|
118
|
-
number(options?: SchemaInterfaceOptions): NumberSchemaInterface;
|
|
119
|
-
boolean(options?: SchemaInterfaceOptions): BooleanSchemaInterface;
|
|
120
|
-
date(options?: SchemaInterfaceOptions): DateSchemaInterface;
|
|
121
|
-
};
|
|
112
|
+
coerce: CoerceInterface;
|
|
122
113
|
cast: {
|
|
123
114
|
boolean(options?: SchemaInterfaceOptions): BooleanSchemaInterface;
|
|
124
115
|
number(options?: SchemaInterfaceOptions): NumberSchemaInterface;
|
|
@@ -186,4 +177,4 @@ declare function extend(callback: ExtenderType): void;
|
|
|
186
177
|
*/
|
|
187
178
|
type Infer<T> = T extends SchemaType ? ReturnType<T['parse']> : unknown;
|
|
188
179
|
|
|
189
|
-
export { type ArraySchemaInterface, type BooleanSchemaInterface, type DateSchemaInterface, type EnumSchemaInterface, type ErrorStructure, type ExtenderType, type FunctionSchemaInterface, type Infer, type Invalid, type LiteralSchemaInterface, type NumberSchemaInterface, type ObjectSchemaInterface, type SchemaInterface, type SchemaInterfaceOptions, type SchemaType, type StringSchemaInterface, type UnionSchemaInterface, type Valid, any, array, boolean, cast,
|
|
180
|
+
export { type ArraySchemaInterface, type BooleanSchemaInterface, type CoerceInterface, type DateSchemaInterface, type EnumSchemaInterface, type ErrorStructure, type ExtenderType, type FunctionSchemaInterface, type Infer, type Invalid, type LiteralSchemaInterface, type NumberSchemaInterface, type ObjectSchemaInterface, type SchemaInterface, type SchemaInterfaceOptions, type SchemaType, type StringSchemaInterface, type UnionSchemaInterface, type Valid, any, array, boolean, cast, date, enumSchema, extend, functionSchema, hookOriginal, literal, number, object, preprocess, s, s as schema, string, union };
|
package/dist/index.d.ts
CHANGED
|
@@ -55,6 +55,8 @@ interface FunctionSchemaInterface extends SchemaInterface<Function, Function> {
|
|
|
55
55
|
}
|
|
56
56
|
interface ArraySchemaInterface<T extends SchemaType> extends SchemaInterface<Array<ReturnType<T['parse']>>, Array<ReturnType<T['parse']>>> {
|
|
57
57
|
}
|
|
58
|
+
interface CoerceInterface {
|
|
59
|
+
}
|
|
58
60
|
interface ObjectSchemaInterface<T extends Record<string, SchemaType>> extends SchemaInterface<{
|
|
59
61
|
[Property in keyof T]: ReturnType<T[Property]['parse']>;
|
|
60
62
|
}, {
|
|
@@ -87,12 +89,6 @@ declare function any(): SchemaInterface<unknown, unknown>;
|
|
|
87
89
|
declare function preprocess<T extends SchemaType>(callback: Function, schema: T): T;
|
|
88
90
|
declare function union<T extends Array<SchemaType>>(definitions: T, options?: SchemaInterfaceOptions): UnionSchemaInterface<T>;
|
|
89
91
|
declare function literal<T extends string | number | boolean>(value: T, options?: SchemaInterfaceOptions): LiteralSchemaInterface<T>;
|
|
90
|
-
declare const coerce: {
|
|
91
|
-
string(options?: SchemaInterfaceOptions): StringSchemaInterface;
|
|
92
|
-
number(options?: SchemaInterfaceOptions): NumberSchemaInterface;
|
|
93
|
-
boolean(options?: SchemaInterfaceOptions): BooleanSchemaInterface;
|
|
94
|
-
date(options?: SchemaInterfaceOptions): DateSchemaInterface;
|
|
95
|
-
};
|
|
96
92
|
declare const cast: {
|
|
97
93
|
boolean(options?: SchemaInterfaceOptions): BooleanSchemaInterface;
|
|
98
94
|
number(options?: SchemaInterfaceOptions): NumberSchemaInterface;
|
|
@@ -113,12 +109,7 @@ declare const s: {
|
|
|
113
109
|
preprocess: typeof preprocess;
|
|
114
110
|
union: typeof union;
|
|
115
111
|
literal: typeof literal;
|
|
116
|
-
coerce:
|
|
117
|
-
string(options?: SchemaInterfaceOptions): StringSchemaInterface;
|
|
118
|
-
number(options?: SchemaInterfaceOptions): NumberSchemaInterface;
|
|
119
|
-
boolean(options?: SchemaInterfaceOptions): BooleanSchemaInterface;
|
|
120
|
-
date(options?: SchemaInterfaceOptions): DateSchemaInterface;
|
|
121
|
-
};
|
|
112
|
+
coerce: CoerceInterface;
|
|
122
113
|
cast: {
|
|
123
114
|
boolean(options?: SchemaInterfaceOptions): BooleanSchemaInterface;
|
|
124
115
|
number(options?: SchemaInterfaceOptions): NumberSchemaInterface;
|
|
@@ -186,4 +177,4 @@ declare function extend(callback: ExtenderType): void;
|
|
|
186
177
|
*/
|
|
187
178
|
type Infer<T> = T extends SchemaType ? ReturnType<T['parse']> : unknown;
|
|
188
179
|
|
|
189
|
-
export { type ArraySchemaInterface, type BooleanSchemaInterface, type DateSchemaInterface, type EnumSchemaInterface, type ErrorStructure, type ExtenderType, type FunctionSchemaInterface, type Infer, type Invalid, type LiteralSchemaInterface, type NumberSchemaInterface, type ObjectSchemaInterface, type SchemaInterface, type SchemaInterfaceOptions, type SchemaType, type StringSchemaInterface, type UnionSchemaInterface, type Valid, any, array, boolean, cast,
|
|
180
|
+
export { type ArraySchemaInterface, type BooleanSchemaInterface, type CoerceInterface, type DateSchemaInterface, type EnumSchemaInterface, type ErrorStructure, type ExtenderType, type FunctionSchemaInterface, type Infer, type Invalid, type LiteralSchemaInterface, type NumberSchemaInterface, type ObjectSchemaInterface, type SchemaInterface, type SchemaInterfaceOptions, type SchemaType, type StringSchemaInterface, type UnionSchemaInterface, type Valid, any, array, boolean, cast, date, enumSchema, extend, functionSchema, hookOriginal, literal, number, object, preprocess, s, s as schema, string, union };
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export{i as any,h as array,d as boolean,
|
|
1
|
+
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/number.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';var R={abortEarly:true};function O(e,
|
|
1
|
+
'use strict';var R={abortEarly:true};function O(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(O(e.errors[r],n));}function T(e){return e?.abortEarly!==void 0?e:R}var S=e=>typeof e=="string"||e instanceof String,y=e=>(typeof e=="number"||e instanceof Number)&&!Number.isNaN(e),b=e=>e===true||e===false,A=e=>e instanceof Date&&!Number.isNaN(e.getTime()),j=e=>typeof e=="function",F=e=>Array.isArray(e),N=e=>typeof e=="object"&&e!==null&&!Array.isArray(e);function C(e,c){let n=h(N,{...c,type:"object"});return n._getDescription=()=>`object({ ${Object.entries(e).map(([s,a])=>`${s}: ${a._getDescription()}`).join(", ")} })`,p(n,"_parse",(r,s,a)=>{let t=r(s,a),{abortEarly:u}=T(a);if(t.success===false)return t;let i={},o=[];for(let f in e){let m=e[f]._parse(t.data[f],a);if(m.success)i[f]=m.data;else {if(m=m,u!==false){let I=O(m.error,f);return {success:false,error:I,errors:[I]}}x(m,o,f);}}return o.length>0?{success:false,error:o[0],errors:o}:{success:true,data:i}}),n}function P(e){return h(S,{...e,type:"string"})}function E(e){return h(y,{...e,type:"number"})}function _(e){return h(b,{...e,type:"boolean"})}function $(e){return h(A,{...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 ${s} value. Expected ${e.map(u=>`"${u}"`).join(" | ")}, received "${t}".`,s="enum",a=h(n,{message:r,...c,type:s});return a._getDescription=()=>`enum(${e.map(t=>`"${t}"`).join(" | ")})`,a}function M(e,c){let n=h(F,{...c,type:"array"});return n._getDescription=()=>`array(${e._getDescription()})`,p(n,"_parse",(r,s,a)=>{let t=r(s,a),{abortEarly:u}=T(a);if(t.success===false)return t;let i=[],o=[];for(let f=0;f<t.data.length;f++){let m=e._parse(t.data[f],a);if(m.success)i.push(m.data);else {if(m=m,u!==false){let I=O(m.error,f);return {success:false,error:I,errors:[I]}}x(m,o,f);}}return o.length>0?{success:false,error:o[0],errors:o}:{success:true,data:i}}),n}function B(){return h(()=>true)}function l(e,c){return p(c,"_parse",(n,r)=>(r=e(r),n(r))),c}function U(e,c){return h(a=>{for(let t=0;t<e.length;t++){let u=e[t]._parse(a);if(u.success)return u}return false},{message:a=>`Invalid union value. Expected the value to match one of the schemas:${e.map((t,u)=>` ${u+1}. ${t._getDescription()}`).join(",")} but received "${typeof a}" with value: ${N(a)?JSON.stringify(a):`"${a}"`}`,...c,type:"union"})}function q(e,c){let s=h(a=>a===e,{message:a=>c?.message?typeof c.message=="function"?c.message(a):c.message:`Expected literal value "${e}", received "${a}"`,name:c?.name,type:"literal"});return s._getDescription=()=>`literal("${e}")`,s}var v={boolean(e){let c=e?.message??(n=>`Cannot cast "${n}" to boolean. Accepted: true/false, 1/0, yes/no, on/off.`);return l(n=>{let r;return S(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 l(n=>{if(b(n))return Number(n);if(S(n)){let r=n.trim();if(r==="")return n;let s=Number(r);if(Number.isFinite(s))return s}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 l(n=>b(n)||y(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 l(n=>{let r;return S(n)&&(r=n.trim()),y(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 p(e,"_parse",(r,s)=>{if(S(s))try{s=JSON.parse(s);}catch{let a={message:typeof n=="function"?n(s):n};return {success:false,error:a,errors:[a]}}return r(s)}),e}},G={object:C,string:P,number:E,boolean:_,date:$,function:V,enum:L,array:M,any:B,preprocess:l,union:U,literal:q,coerce:{},cast:v};function w(e){return c=>`The value "${c}" must be type of ${e} but is type of "${typeof c}".`}function p(e,c,n){let r=e[c];e[c]=(...s)=>n(r,...s);}function h(e,{type:c="any",name:n,message:r}={}){r=r||w(c);let s={name:n,message:r,type:c},a={_getName(){return n},_getType(){return c},_getDescription(){return this._getName()??this._getType()},_parse(t,u){let i=e(t);if(i===true)return {success:true,data:t};if(typeof i=="object"&&i?.success===true)return i;let o={message:typeof r=="function"?r(t):r};return {success:false,error:o,errors:[o]}},parse(t,u){let i=a._parse(t,u);if(!i.success)throw i=i,new Error(i.error.message,{cause:i.error.cause});return i.data},safeParse(t,u){return a._parse(t,u)},transform(t){return p(this,"_parse",(u,i,o)=>{let f=u(i,o);return f.success&&(f.data=t(f.data)),f}),this},optional(){return p(this,"_parse",(t,u,i)=>{let o=t(u,i);return !o.success&&u===void 0&&(o.data=void 0,o.success=true),o}),this},nullable(){return p(this,"_parse",(t,u,i)=>{let o=t(u,i);return !o.success&&u===null&&(o.data=null,o.success=true),o}),this},nullish(){return p(this,"_parse",(t,u,i)=>{let o=t(u,i);return !o.success&&u==null&&(o.success=true,o.data=u),o}),this},default(t){return p(this,"_parse",(u,i,o)=>(i===void 0&&(i=typeof t=="function"?t():t),u(i,o))),this},catch(t){return p(this,"_parse",(u,i,o)=>{let f=u(i,o);return f.success?f:{success:true,data:typeof t=="function"?t({input:i,error:f.error}):t}}),this},pipe(t){return p(this,"_parse",(u,i,o)=>{let f=u(i,o);return f.success?t._parse(f.data,o):f}),this},refine(t,{message:u,type:i}={}){return i&&(u=u||w(i),c=i),p(this,"_parse",(o,f,m)=>{let I=o(f,m),{abortEarly:J}=T(m);if(!I.success)return I;let d=t(I.data);if(d===true||typeof d=="object"&&d?.success===true)return I;let k={message:typeof u=="function"?u(f):u};return {success:false,error:k,errors:[k]}}),this}};return g.length>0?g.reduce((t,u)=>u(t,e,s)??t,a):a}var g=[];function D(e){if(typeof e!="function")throw new TypeError("extend() requires a function argument");g.push(e);}D((e,c,n)=>{if(n?.type==="number"){let r=e;r.min=function(s,{message:a}={}){return this.refine(t=>t>=s,{message:a||`Number must be greater than or equal to ${s}.`})},r.max=function(s,{message:a}={}){return this.refine(t=>t<=s,{message:a||`Number must be less than or equal to ${s}.`})},r.positive=function({message:s}={}){return this.refine(a=>a>0,{message:s||"Number must be positive."})},r.negative=function({message:s}={}){return this.refine(a=>a<0,{message:s||"Number must be negative."})},r.int=function({message:s}={}){return this.refine(a=>Number.isInteger(a),{message:s||"Number must be an integer."})},r.float=function({message:s}={}){return this.refine(a=>Number.isFinite(a)&&!Number.isInteger(a),{message:s||"Number must be a floating point (non-integer)."})},r.multipleOf=function(s,{message:a}={}){return this.refine(t=>t%s===0,{message:a||`Number must be a multiple of ${s}.`})},r.finite=function({message:s}={}){return this.refine(a=>Number.isFinite(a),{message:s||"Number must be finite."})};}return e});exports.any=B;exports.array=M;exports.boolean=_;exports.cast=v;exports.date=$;exports.enumSchema=L;exports.extend=D;exports.functionSchema=V;exports.hookOriginal=p;exports.literal=q;exports.number=E;exports.object=C;exports.preprocess=l;exports.s=G;exports.schema=G;exports.string=P;exports.union=U;
|
package/dist/number.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, any, array, boolean, cast,
|
|
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 NumberSchemaInterface {
|
package/dist/number.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, any, array, boolean, cast,
|
|
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 NumberSchemaInterface {
|
package/dist/number.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import'./chunk-
|
|
1
|
+
import'./chunk-HVQY2UKM.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/string.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';var R={abortEarly:true};function O(e,
|
|
1
|
+
'use strict';var R={abortEarly:true};function O(e,s){if(!s)return e;let t=e?.cause?.key?`${s}.${e.cause.key}`:`${s}`;return {message:`Error parsing key "${t}": ${e.message}`,cause:{key:t}}}function x(e,s,t){if(e.errors?.length)for(let r=0;r<e.errors.length;r++)s.push(O(e.errors[r],t));}function T(e){return e?.abortEarly!==void 0?e:R}var I=e=>typeof e=="string"||e instanceof String,g=e=>(typeof e=="number"||e instanceof Number)&&!Number.isNaN(e),y=e=>e===true||e===false,A=e=>e instanceof Date&&!Number.isNaN(e.getTime()),j=e=>typeof e=="function",C=e=>Array.isArray(e),P=e=>typeof e=="object"&&e!==null&&!Array.isArray(e);function F(e,s){let t=h(P,{...s,type:"object"});return t._getDescription=()=>`object({ ${Object.entries(e).map(([a,c])=>`${a}: ${c._getDescription()}`).join(", ")} })`,m(t,"_parse",(r,a,c)=>{let n=r(a,c),{abortEarly:o}=T(c);if(n.success===false)return n;let i={},u=[];for(let f in e){let p=e[f]._parse(n.data[f],c);if(p.success)i[f]=p.data;else {if(p=p,o!==false){let S=O(p.error,f);return {success:false,error:S,errors:[S]}}x(p,u,f);}}return u.length>0?{success:false,error:u[0],errors:u}:{success:true,data:i}}),t}function $(e){return h(I,{...e,type:"string"})}function E(e){return h(g,{...e,type:"number"})}function _(e){return h(y,{...e,type:"boolean"})}function D(e){return h(A,{...e,type:"date"})}function V(e){return h(j,{...e,type:"function"})}function L(e,s){let t=n=>e.includes(n),r=n=>`Invalid ${a} value. Expected ${e.map(o=>`"${o}"`).join(" | ")}, received "${n}".`,a="enum",c=h(t,{message:r,...s,type:a});return c._getDescription=()=>`enum(${e.map(n=>`"${n}"`).join(" | ")})`,c}function M(e,s){let t=h(C,{...s,type:"array"});return t._getDescription=()=>`array(${e._getDescription()})`,m(t,"_parse",(r,a,c)=>{let n=r(a,c),{abortEarly:o}=T(c);if(n.success===false)return n;let i=[],u=[];for(let f=0;f<n.data.length;f++){let p=e._parse(n.data[f],c);if(p.success)i.push(p.data);else {if(p=p,o!==false){let S=O(p.error,f);return {success:false,error:S,errors:[S]}}x(p,u,f);}}return u.length>0?{success:false,error:u[0],errors:u}:{success:true,data:i}}),t}function U(){return h(()=>true)}function l(e,s){return m(s,"_parse",(t,r)=>(r=e(r),t(r))),s}function B(e,s){return h(c=>{for(let n=0;n<e.length;n++){let o=e[n]._parse(c);if(o.success)return o}return false},{message:c=>`Invalid union value. Expected the value to match one of the schemas:${e.map((n,o)=>` ${o+1}. ${n._getDescription()}`).join(",")} but received "${typeof c}" with value: ${P(c)?JSON.stringify(c):`"${c}"`}`,...s,type:"union"})}function W(e,s){let a=h(c=>c===e,{message:c=>s?.message?typeof s.message=="function"?s.message(c):s.message:`Expected literal value "${e}", received "${c}"`,name:s?.name,type:"literal"});return a._getDescription=()=>`literal("${e}")`,a}var J={boolean(e){let s=e?.message??(t=>`Cannot cast "${t}" to boolean. Accepted: true/false, 1/0, yes/no, on/off.`);return l(t=>{let r;return I(t)&&(r=t.toLowerCase()),r==="true"||r==="yes"||r==="on"||r==="1"||t===1?true:r==="false"||r==="no"||r==="off"||r==="0"||t===0?false:t},_({...e,message:s}))},number(e){let s=e?.message??(t=>`Cannot cast "${t}" to a number. Expected a numeric string or number.`);return l(t=>{if(y(t))return Number(t);if(I(t)){let r=t.trim();if(r==="")return t;let a=Number(r);if(Number.isFinite(a))return a}return t},E({...e,message:s}))},string(e){let s=e?.message??(t=>`Cannot cast "${t}" to string. Expected a string, number, or boolean.`);return l(t=>y(t)||g(t)&&Number.isFinite(t)?String(t):t,$({...e,message:s}))},date(e){let s=e?.message??(t=>`Cannot cast "${t}" to a valid date.`);return l(t=>{let r;return I(t)&&(r=t.trim()),g(t)&&Number.isFinite(t)||r?new Date(r??t):t},D({...e,message:s}))},json(e,s){let t=s?.message??(r=>`Cannot parse "${r}" as JSON.`);return m(e,"_parse",(r,a)=>{if(I(a))try{a=JSON.parse(a);}catch{let c={message:typeof t=="function"?t(a):t};return {success:false,error:c,errors:[c]}}return r(a)}),e}},z={object:F,string:$,number:E,boolean:_,date:D,function:V,enum:L,array:M,any:U,preprocess:l,union:B,literal:W,coerce:{},cast:J};function k(e){return s=>`The value "${s}" must be type of ${e} but is type of "${typeof s}".`}function m(e,s,t){let r=e[s];e[s]=(...a)=>t(r,...a);}function h(e,{type:s="any",name:t,message:r}={}){r=r||k(s);let a={name:t,message:r,type:s},c={_getName(){return t},_getType(){return s},_getDescription(){return this._getName()??this._getType()},_parse(n,o){let i=e(n);if(i===true)return {success:true,data:n};if(typeof i=="object"&&i?.success===true)return i;let u={message:typeof r=="function"?r(n):r};return {success:false,error:u,errors:[u]}},parse(n,o){let i=c._parse(n,o);if(!i.success)throw i=i,new Error(i.error.message,{cause:i.error.cause});return i.data},safeParse(n,o){return c._parse(n,o)},transform(n){return m(this,"_parse",(o,i,u)=>{let f=o(i,u);return f.success&&(f.data=n(f.data)),f}),this},optional(){return m(this,"_parse",(n,o,i)=>{let u=n(o,i);return !u.success&&o===void 0&&(u.data=void 0,u.success=true),u}),this},nullable(){return m(this,"_parse",(n,o,i)=>{let u=n(o,i);return !u.success&&o===null&&(u.data=null,u.success=true),u}),this},nullish(){return m(this,"_parse",(n,o,i)=>{let u=n(o,i);return !u.success&&o==null&&(u.success=true,u.data=o),u}),this},default(n){return m(this,"_parse",(o,i,u)=>(i===void 0&&(i=typeof n=="function"?n():n),o(i,u))),this},catch(n){return m(this,"_parse",(o,i,u)=>{let f=o(i,u);return f.success?f:{success:true,data:typeof n=="function"?n({input:i,error:f.error}):n}}),this},pipe(n){return m(this,"_parse",(o,i,u)=>{let f=o(i,u);return f.success?n._parse(f.data,u):f}),this},refine(n,{message:o,type:i}={}){return i&&(o=o||k(i),s=i),m(this,"_parse",(u,f,p)=>{let S=u(f,p),{abortEarly:q}=T(p);if(!S.success)return S;let d=n(S.data);if(d===true||typeof d=="object"&&d?.success===true)return S;let w={message:typeof o=="function"?o(f):o};return {success:false,error:w,errors:[w]}}),this}};return b.length>0?b.reduce((n,o)=>o(n,e,a)??n,c):c}var b=[];function N(e){if(typeof e!="function")throw new TypeError("extend() requires a function argument");b.push(e);}N((e,s,t)=>{if(t?.type==="string"){let r=e;r.min=function(a,{message:c}={}){return this.refine(n=>n.length>=a,{message:c||(n=>`String must be at least ${a} characters long (received ${n.length} characters: "${n}")`)})},r.max=function(a,{message:c}={}){return this.refine(n=>n.length<=a,{message:c||(n=>`String must be at most ${a} characters long (received ${n.length} characters: "${n}")`)})},r.length=function(a,{message:c}={}){return this.refine(n=>n.length===a,{message:c||(n=>`String must be exactly ${a} characters long (received ${n.length} characters: "${n}")`)})},r.nonEmpty=function({message:a}={}){return this.refine(c=>c.length>0,{message:a||"String must not be empty (received empty string)"})},r.startsWith=function(a,{message:c}={}){return this.refine(n=>n.startsWith(a),{message:c||(n=>`String must start with "${a}" (received: "${n}")`)})},r.endsWith=function(a,{message:c}={}){return this.refine(n=>n.endsWith(a),{message:c||(n=>`String must end with "${a}" (received: "${n}")`)})},r.includes=function(a,{message:c}={}){return this.refine(n=>n.includes(a),{message:c||(n=>`String must include "${a}" (received: "${n}")`)})},r.toLowerCase=function(){return this.transform(a=>a.toLowerCase())},r.toUpperCase=function(){return this.transform(a=>a.toUpperCase())},r.trim=function(){return this.transform(a=>a.trim())},r.padStart=function(a,c=" "){return this.transform(n=>n.padStart(a,c))},r.padEnd=function(a,c=" "){return this.transform(n=>n.padEnd(a,c))},r.replace=function(a,c){return this.transform(n=>n.replace(a,c))};}return e});exports.any=U;exports.array=M;exports.boolean=_;exports.cast=J;exports.date=D;exports.enumSchema=L;exports.extend=N;exports.functionSchema=V;exports.hookOriginal=m;exports.literal=W;exports.number=E;exports.object=F;exports.preprocess=l;exports.s=z;exports.schema=z;exports.string=$;exports.union=B;
|
package/dist/string.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, any, array, boolean, cast,
|
|
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 StringSchemaInterface {
|
package/dist/string.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, any, array, boolean, cast,
|
|
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 StringSchemaInterface {
|
package/dist/string.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import'./chunk-
|
|
1
|
+
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/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@esmj/schema",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.8.2",
|
|
4
4
|
"description": "Tiny extendable package for schema validation.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"schema",
|
|
@@ -46,6 +46,11 @@
|
|
|
46
46
|
"types": "./dist/array.d.ts",
|
|
47
47
|
"import": "./dist/array.mjs",
|
|
48
48
|
"require": "./dist/array.cjs"
|
|
49
|
+
},
|
|
50
|
+
"./coerce": {
|
|
51
|
+
"types": "./dist/coerce.d.ts",
|
|
52
|
+
"import": "./dist/coerce.mjs",
|
|
53
|
+
"require": "./dist/coerce.cjs"
|
|
49
54
|
}
|
|
50
55
|
},
|
|
51
56
|
"sideEffects": [
|
|
@@ -60,7 +65,10 @@
|
|
|
60
65
|
"./dist/array.mjs",
|
|
61
66
|
"./dist/array.cjs",
|
|
62
67
|
"./dist/full.mjs",
|
|
63
|
-
"./dist/full.cjs"
|
|
68
|
+
"./dist/full.cjs",
|
|
69
|
+
"./src/coerce.ts",
|
|
70
|
+
"./dist/coerce.mjs",
|
|
71
|
+
"./dist/coerce.cjs"
|
|
64
72
|
],
|
|
65
73
|
"typings": "dist/index.d.ts",
|
|
66
74
|
"scripts": {
|
|
@@ -107,12 +115,12 @@
|
|
|
107
115
|
"homepage": "https://github.com/mjancarik/esmj-schema#readme",
|
|
108
116
|
"devDependencies": {
|
|
109
117
|
"@biomejs/biome": "1.9.4",
|
|
110
|
-
"ajv": "^8.17.1",
|
|
111
|
-
"ajv-keywords": "^5.1.0",
|
|
112
118
|
"@commitlint/cli": "^19.8.1",
|
|
113
119
|
"@commitlint/config-conventional": "^19.8.1",
|
|
114
120
|
"@sinclair/typebox": "^0.34.41",
|
|
115
121
|
"@zod/mini": "^4.0.0-beta.0",
|
|
122
|
+
"ajv": "^8.17.1",
|
|
123
|
+
"ajv-keywords": "^5.1.0",
|
|
116
124
|
"arktype": "^2.1.22",
|
|
117
125
|
"commitizen": "^4.3.1",
|
|
118
126
|
"conventional-changelog-cli": "^5.0.0",
|
|
@@ -124,6 +132,7 @@
|
|
|
124
132
|
"lint-staged": "^16.1.6",
|
|
125
133
|
"superstruct": "^2.0.2",
|
|
126
134
|
"tsup": "^8.5.1",
|
|
135
|
+
"valibot": "^1.4.2",
|
|
127
136
|
"yup": "^1.7.0",
|
|
128
137
|
"zod": "^3.25.42"
|
|
129
138
|
}
|
package/dist/chunk-37YJYCTR.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
var N={abortEarly:true};function T(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 _(e,t,n){if(e.errors?.length)for(let a=0;a<e.errors.length;a++)t.push(T(e.errors[a],n));}function O(e){return e?.abortEarly!==void 0?e:N}var S=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),$=e=>typeof e=="object"&&e!==null&&!Array.isArray(e);function F(e,t){let n=h($,{...t,type:"object"});return n._getDescription=()=>`object({ ${Object.entries(e).map(([f,o])=>`${f}: ${o._getDescription()}`).join(", ")} })`,m(n,"_parse",(a,f,o)=>{let r=a(f,o),{abortEarly:c}=O(o);if(r.success===false)return r;let u={},s=[];for(let i in e){let p=e[i]._parse(r.data[i],o);if(p.success)u[i]=p.data;else {if(p=p,c!==false){let I=T(p.error,i);return {success:false,error:I,errors:[I]}}_(p,s,i);}}return s.length>0?{success:false,error:s[0],errors:s}:{success:true,data:u}}),n}function k(e){return h(S,{...e,type:"string"})}function w(e){return h(y,{...e,type:"number"})}function x(e){return h(g,{...e,type:"boolean"})}function P(e){return h(R,{...e,type:"date"})}function C(e){return h(A,{...e,type:"function"})}function V(e,t){let n=r=>e.includes(r),a=r=>`Invalid ${f} value. Expected ${e.map(c=>`"${c}"`).join(" | ")}, received "${r}".`,f="enum",o=h(n,{message:a,...t,type:f});return o._getDescription=()=>`enum(${e.map(r=>`"${r}"`).join(" | ")})`,o}function B(e,t){let n=h(j,{...t,type:"array"});return n._getDescription=()=>`array(${e._getDescription()})`,m(n,"_parse",(a,f,o)=>{let r=a(f,o),{abortEarly:c}=O(o);if(r.success===false)return r;let u=[],s=[];for(let i=0;i<r.data.length;i++){let p=e._parse(r.data[i],o);if(p.success)u.push(p.data);else {if(p=p,c!==false){let I=T(p.error,i);return {success:false,error:I,errors:[I]}}_(p,s,i);}}return s.length>0?{success:false,error:s[0],errors:s}:{success:true,data:u}}),n}function L(){return h(()=>true)}function l(e,t){return m(t,"_parse",(n,a)=>(a=e(a),n(a))),t}function M(e,t){return h(o=>{for(let r=0;r<e.length;r++){let c=e[r]._parse(o);if(c.success)return c}return false},{message:o=>`Invalid union value. Expected the value to match one of the schemas:${e.map((r,c)=>` ${c+1}. ${r._getDescription()}`).join(",")} but received "${typeof o}" with value: ${$(o)?JSON.stringify(o):`"${o}"`}`,...t,type:"union"})}function U(e,t){let f=h(o=>o===e,{message:o=>t?.message?typeof t.message=="function"?t.message(o):t.message:`Expected literal value "${e}", received "${o}"`,name:t?.name,type:"literal"});return f._getDescription=()=>`literal("${e}")`,f}var J={string(e){return l(t=>String(t),k(e))},number(e){let t=e?.message??(n=>`Cannot coerce "${n}" to a valid number.`);return l(n=>Number(n),w({...e,message:t}))},boolean(e){return l(t=>!!t,x(e))},date(e){let t=e?.message??(n=>`Cannot coerce "${n}" to a valid date.`);return l(n=>new Date(n),P({...e,message:t}))}},q={boolean(e){let t=e?.message??(n=>`Cannot cast "${n}" to boolean. Accepted: true/false, 1/0, yes/no, on/off.`);return l(n=>{let a;return S(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},x({...e,message:t}))},number(e){let t=e?.message??(n=>`Cannot cast "${n}" to a number. Expected a numeric string or number.`);return l(n=>{if(g(n))return Number(n);if(S(n)){let a=n.trim();if(a==="")return n;let f=Number(a);if(Number.isFinite(f))return f}return n},w({...e,message:t}))},string(e){let t=e?.message??(n=>`Cannot cast "${n}" to string. Expected a string, number, or boolean.`);return l(n=>g(n)||y(n)&&Number.isFinite(n)?String(n):n,k({...e,message:t}))},date(e){let t=e?.message??(n=>`Cannot cast "${n}" to a valid date.`);return l(n=>{let a;return S(n)&&(a=n.trim()),y(n)&&Number.isFinite(n)||a?new Date(a??n):n},P({...e,message:t}))},json(e,t){let n=t?.message??(a=>`Cannot parse "${a}" as JSON.`);return m(e,"_parse",(a,f)=>{if(S(f))try{f=JSON.parse(f);}catch{let o={message:typeof n=="function"?n(f):n};return {success:false,error:o,errors:[o]}}return a(f)}),e}},H={object:F,string:k,number:w,boolean:x,date:P,function:C,enum:V,array:B,any:L,preprocess:l,union:M,literal:U,coerce:J,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},o={_getName(){return n},_getType(){return t},_getDescription(){return this._getName()??this._getType()},_parse(r,c){let u=e(r);if(u===true)return {success:true,data:r};if(typeof u=="object"&&u?.success===true)return u;let s={message:typeof a=="function"?a(r):a};return {success:false,error:s,errors:[s]}},parse(r,c){let u=o._parse(r,c);if(!u.success)throw u=u,new Error(u.error.message,{cause:u.error.cause});return u.data},safeParse(r,c){return o._parse(r,c)},transform(r){return m(this,"_parse",(c,u,s)=>{let i=c(u,s);return i.success&&(i.data=r(i.data)),i}),this},optional(){return m(this,"_parse",(r,c,u)=>{let s=r(c,u);return !s.success&&c===void 0&&(s.data=void 0,s.success=true),s}),this},nullable(){return m(this,"_parse",(r,c,u)=>{let s=r(c,u);return !s.success&&c===null&&(s.data=null,s.success=true),s}),this},nullish(){return m(this,"_parse",(r,c,u)=>{let s=r(c,u);return !s.success&&c==null&&(s.success=true,s.data=c),s}),this},default(r){return m(this,"_parse",(c,u,s)=>(u===void 0&&(u=typeof r=="function"?r():r),c(u,s))),this},catch(r){return m(this,"_parse",(c,u,s)=>{let i=c(u,s);return i.success?i:{success:true,data:typeof r=="function"?r({input:u,error:i.error}):r}}),this},pipe(r){return m(this,"_parse",(c,u,s)=>{let i=c(u,s);return i.success?r._parse(i.data,s):i}),this},refine(r,{message:c,type:u}={}){return u&&(c=c||D(u),t=u),m(this,"_parse",(s,i,p)=>{let I=s(i,p),{abortEarly:z}=O(p);if(!I.success)return I;let d=r(I.data);if(d===true||typeof d=="object"&&d?.success===true)return I;let E={message:typeof c=="function"?c(i):c};return {success:false,error:E,errors:[E]}}),this}};return b.length>0?b.reduce((r,c)=>c(r,e,f)??r,o):o}var b=[];function Q(e){if(typeof e!="function")throw new TypeError("extend() requires a function argument");b.push(e);}export{F as a,k as b,w as c,x as d,P as e,C as f,V as g,B as h,L as i,l as j,M as k,U as l,J as m,q as n,H as o,m as p,Q as q};
|