@esmj/schema 0.7.1 → 0.7.3
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 +157 -1
- 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-CLEFU3BA.mjs +1 -0
- package/dist/chunk-QQOSS5DZ.mjs +1 -0
- package/dist/chunk-YUL3WJPU.mjs +1 -0
- package/dist/chunk-YYPP27RX.mjs +1 -0
- package/dist/full.cjs +1 -1
- package/dist/full.d.mts +1 -1
- package/dist/full.d.ts +1 -1
- package/dist/full.mjs +1 -1
- package/dist/index.cjs +1 -1
- package/dist/index.d.mts +127 -4
- package/dist/index.d.ts +127 -4
- 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 +2 -2
- package/dist/chunk-2NCXW7ID.mjs +0 -1
- package/dist/chunk-4JJPVRF6.mjs +0 -1
- package/dist/chunk-LZRUIXQL.mjs +0 -1
- package/dist/chunk-PH4B25KI.mjs +0 -1
package/README.md
CHANGED
|
@@ -17,6 +17,8 @@ This small library provides a simple schema validation system for JavaScript/Typ
|
|
|
17
17
|
- [Full Extensions](#full-extensions-esmjschemafull)
|
|
18
18
|
- [API Reference Summary](#api-reference-summary)
|
|
19
19
|
- [Schema Types](#schema-types)
|
|
20
|
+
- [s.coerce](#scoerce)
|
|
21
|
+
- [s.cast](#scast)
|
|
20
22
|
- [Schema Methods](#schema-methods)
|
|
21
23
|
- [parse](#parsevalue-parseoptions)
|
|
22
24
|
- [safeParse](#safeparsevalue-parseoptions)
|
|
@@ -117,7 +119,7 @@ When choosing a schema validation library, bundle size can be an important facto
|
|
|
117
119
|
|
|
118
120
|
| Library | Bundle Size (minified + gzipped) |
|
|
119
121
|
|-------------------|---------------------------------|
|
|
120
|
-
| `@esmj/schema` | `~1.
|
|
122
|
+
| `@esmj/schema` | `~1.6 KB` |
|
|
121
123
|
| Superstruct | ~3.2 KB |
|
|
122
124
|
| @sinclair/typebox | ~11.7 KB |
|
|
123
125
|
| Yup | ~12.2 KB |
|
|
@@ -424,6 +426,7 @@ const schema = s.object({
|
|
|
424
426
|
- `s.number()` - Number validation
|
|
425
427
|
- `s.boolean()` - Boolean validation
|
|
426
428
|
- `s.date()` - Date validation
|
|
429
|
+
- `s.function()` - Function validation
|
|
427
430
|
- `s.object(def)` - Object validation
|
|
428
431
|
- `s.array(def)` - Array validation
|
|
429
432
|
- `s.literal(value)` - Literal value validation
|
|
@@ -442,6 +445,23 @@ const schema = s.object({
|
|
|
442
445
|
- `.default(value)` - Sets default value
|
|
443
446
|
- `.catch(value)` - Returns fallback value on any parse failure
|
|
444
447
|
|
|
448
|
+
### Coerce
|
|
449
|
+
|
|
450
|
+
- `s.coerce.string()` - Coerce any value to string, then validate
|
|
451
|
+
- `s.coerce.number()` - Coerce any value to number, then validate (fails for NaN)
|
|
452
|
+
- `s.coerce.boolean()` - Coerce any value to boolean, then validate
|
|
453
|
+
- `s.coerce.date()` - Coerce any value to Date, then validate (fails for invalid dates)
|
|
454
|
+
|
|
455
|
+
### Cast
|
|
456
|
+
|
|
457
|
+
Semantic casting that understands common string representations and rejects ambiguous inputs:
|
|
458
|
+
|
|
459
|
+
- `s.cast.boolean()` - Cast to boolean; understands `'true'/'false'`, `'yes'/'no'`, `'on'/'off'`, `'1'/'0'` (case-insensitive); rejects `null`/`undefined`/unrecognised strings
|
|
460
|
+
- `s.cast.number()` - Cast to number; trims whitespace from strings, accepts booleans (`true`→1, `false`→0); rejects `null`/`undefined`/empty strings
|
|
461
|
+
- `s.cast.string()` - Cast to string; accepts strings, finite numbers, and booleans; rejects `null`/`undefined`/objects/`NaN`/`Infinity`
|
|
462
|
+
- `s.cast.date()` - Cast to Date; accepts ISO strings, finite timestamps, and existing Dates; rejects `null`/`undefined`/booleans/empty strings
|
|
463
|
+
- `s.cast.json(schema)` - Parse a JSON string and validate the result against a schema; non-string inputs pass through directly; malformed JSON returns a proper validation failure
|
|
464
|
+
|
|
445
465
|
### Transformations
|
|
446
466
|
|
|
447
467
|
- `.transform(fn)` - Transform value
|
|
@@ -571,6 +591,27 @@ const dateSchemaFunc = s.date({
|
|
|
571
591
|
});
|
|
572
592
|
```
|
|
573
593
|
|
|
594
|
+
#### `s.function(options?)`
|
|
595
|
+
|
|
596
|
+
Creates a function schema that validates the value is callable.
|
|
597
|
+
|
|
598
|
+
- **`message`**: Can be either a constant string or a function `(value) => string`.
|
|
599
|
+
|
|
600
|
+
```typescript
|
|
601
|
+
const callbackSchema = s.function();
|
|
602
|
+
callbackSchema.parse(() => {}); // () => {}
|
|
603
|
+
callbackSchema.parse(async () => {}); // async () => {}
|
|
604
|
+
callbackSchema.parse('hello'); // throws
|
|
605
|
+
|
|
606
|
+
const callbackSchemaMsg = s.function({
|
|
607
|
+
message: 'Expected a callback function.',
|
|
608
|
+
});
|
|
609
|
+
|
|
610
|
+
const callbackSchemaFunc = s.function({
|
|
611
|
+
message: (value) => `Custom error: "${value}" is not a function.`,
|
|
612
|
+
});
|
|
613
|
+
```
|
|
614
|
+
|
|
574
615
|
#### `s.object(definition, options?)`
|
|
575
616
|
|
|
576
617
|
Creates an object schema with the given definition. You can optionally pass `options` to customize error messages.
|
|
@@ -730,6 +771,119 @@ Creates a schema that preprocesses the input value using the provided callback b
|
|
|
730
771
|
const preprocessSchema = s.preprocess((value) => new Date(value), s.date());
|
|
731
772
|
```
|
|
732
773
|
|
|
774
|
+
#### `s.coerce`
|
|
775
|
+
|
|
776
|
+
The `coerce` namespace applies a native JS constructor to the input **before** validation.
|
|
777
|
+
Unlike `s.preprocess`, you don't need to write the conversion yourself, and coerce methods
|
|
778
|
+
provide clear, specific error messages when coercion produces an invalid result.
|
|
779
|
+
|
|
780
|
+
| Method | Coercion applied | Fails when |
|
|
781
|
+
|---|---|---|
|
|
782
|
+
| `s.coerce.string(options?)` | `String(v)` | Never — `String()` always succeeds |
|
|
783
|
+
| `s.coerce.number(options?)` | `Number(v)` | Result is `NaN` (e.g. `'bad'`, `undefined`) |
|
|
784
|
+
| `s.coerce.boolean(options?)` | `Boolean(v)` | Never — `Boolean()` always succeeds |
|
|
785
|
+
| `s.coerce.date(options?)` | `new Date(v)` | Result is an invalid Date (e.g. `'garbage'`) |
|
|
786
|
+
|
|
787
|
+
> **Note:** `Boolean('false')` is `true` because `'false'` is a non-empty string. This matches JavaScript semantics.
|
|
788
|
+
|
|
789
|
+
```typescript
|
|
790
|
+
s.coerce.number().parse('42'); // 42
|
|
791
|
+
s.coerce.number().parse(true); // 1
|
|
792
|
+
s.coerce.number().parse('bad'); // throws: Cannot coerce "NaN" to a valid number.
|
|
793
|
+
|
|
794
|
+
s.coerce.string().parse(123); // '123'
|
|
795
|
+
s.coerce.string().parse(null); // 'null'
|
|
796
|
+
|
|
797
|
+
s.coerce.boolean().parse(0); // false
|
|
798
|
+
s.coerce.boolean().parse('false'); // true — non-empty string!
|
|
799
|
+
|
|
800
|
+
s.coerce.date().parse('2024-01-01'); // Date object
|
|
801
|
+
s.coerce.date().parse('garbage'); // throws: Cannot coerce "Invalid Date" to a valid date.
|
|
802
|
+
|
|
803
|
+
// All schema methods chain normally after coerce:
|
|
804
|
+
s.coerce.number().refine((v) => v > 0, { message: 'Must be positive' }).parse('5'); // 5
|
|
805
|
+
|
|
806
|
+
// Custom error message:
|
|
807
|
+
s.coerce.number({ message: 'Expected a numeric value' }).parse('bad'); // throws: Expected a numeric value
|
|
808
|
+
```
|
|
809
|
+
|
|
810
|
+
#### `s.cast`
|
|
811
|
+
|
|
812
|
+
Programmer-friendly semantic casting. Unlike `s.coerce` (raw JS constructors), `s.cast` understands
|
|
813
|
+
common string representations and rejects ambiguous inputs like `null`, `undefined`, and empty strings.
|
|
814
|
+
|
|
815
|
+
| Method | Accepted inputs | Rejects |
|
|
816
|
+
|---|---|---|
|
|
817
|
+
| `s.cast.string(options?)` | strings, finite numbers, booleans | `null`, `undefined`, objects, `NaN`, `Infinity` |
|
|
818
|
+
| `s.cast.number(options?)` | numbers (incl. booleans `true`/`false`→1/0), trimmed numeric strings | `null`, `undefined`, empty strings, non-numeric strings |
|
|
819
|
+
| `s.cast.boolean(options?)` | booleans, `1`/`0`, `'true'/'false'`, `'yes'/'no'`, `'on'/'off'`, `'1'/'0'` | `null`, `undefined`, unrecognised strings, other numbers |
|
|
820
|
+
| `s.cast.date(options?)` | `Date` objects, ISO strings, finite integer timestamps | `null`, `undefined`, booleans, empty strings, invalid date strings |
|
|
821
|
+
| `s.cast.json(schema, options?)` | JSON strings (parsed), any non-string value (pass-through) | malformed JSON strings |
|
|
822
|
+
|
|
823
|
+
**Key differences from `s.coerce`:**
|
|
824
|
+
|
|
825
|
+
| Input | `s.coerce.boolean()` | `s.cast.boolean()` |
|
|
826
|
+
|---|---|---|
|
|
827
|
+
| `'false'` | `true` (non-empty string!) | `false` |
|
|
828
|
+
| `'yes'` / `'no'` | `true` / `true` | `true` / `false` |
|
|
829
|
+
| `null` | `false` | throws |
|
|
830
|
+
|
|
831
|
+
| Input | `s.coerce.number()` | `s.cast.number()` |
|
|
832
|
+
|---|---|---|
|
|
833
|
+
| `null` | `0` | throws |
|
|
834
|
+
| `''` | `0` | throws |
|
|
835
|
+
|
|
836
|
+
| Input | `s.coerce.string()` | `s.cast.string()` |
|
|
837
|
+
|---|---|---|
|
|
838
|
+
| `null` | `'null'` | throws |
|
|
839
|
+
| `undefined` | `'undefined'` | throws |
|
|
840
|
+
|
|
841
|
+
```typescript
|
|
842
|
+
// boolean
|
|
843
|
+
s.cast.boolean().parse('false'); // false — unlike coerce!
|
|
844
|
+
s.cast.boolean().parse('yes'); // true
|
|
845
|
+
s.cast.boolean().parse('on'); // true
|
|
846
|
+
s.cast.boolean().parse('OFF'); // false (case-insensitive)
|
|
847
|
+
s.cast.boolean().parse(1); // true
|
|
848
|
+
s.cast.boolean().parse(0); // false
|
|
849
|
+
s.cast.boolean().parse('hello'); // throws: Cannot cast "hello" to boolean...
|
|
850
|
+
s.cast.boolean().parse(null); // throws
|
|
851
|
+
|
|
852
|
+
// number
|
|
853
|
+
s.cast.number().parse('42'); // 42
|
|
854
|
+
s.cast.number().parse(' 3.14 '); // 3.14 — trims whitespace
|
|
855
|
+
s.cast.number().parse(true); // 1
|
|
856
|
+
s.cast.number().parse(false); // 0
|
|
857
|
+
s.cast.number().parse(null); // throws: Cannot cast "null" to a number...
|
|
858
|
+
s.cast.number().parse(''); // throws
|
|
859
|
+
|
|
860
|
+
// string
|
|
861
|
+
s.cast.string().parse(123); // '123'
|
|
862
|
+
s.cast.string().parse(true); // 'true'
|
|
863
|
+
s.cast.string().parse(false); // 'false'
|
|
864
|
+
s.cast.string().parse(null); // throws: Cannot cast "null" to string...
|
|
865
|
+
s.cast.string().parse(NaN); // throws
|
|
866
|
+
|
|
867
|
+
// date
|
|
868
|
+
s.cast.date().parse('2024-01-01'); // Date object
|
|
869
|
+
s.cast.date().parse(1704067200000); // Date object
|
|
870
|
+
s.cast.date().parse(null); // throws: Cannot cast "null" to a valid date.
|
|
871
|
+
s.cast.date().parse(true); // throws
|
|
872
|
+
|
|
873
|
+
// All schema methods chain normally:
|
|
874
|
+
s.cast.number().refine((v) => v > 0, { message: 'Must be positive' }).parse('5'); // 5
|
|
875
|
+
|
|
876
|
+
// Custom error message:
|
|
877
|
+
s.cast.boolean({ message: 'Must be a boolean flag' }).parse('maybe'); // throws: Must be a boolean flag
|
|
878
|
+
|
|
879
|
+
// json
|
|
880
|
+
s.cast.json(s.object({ name: s.string() })).parse('{"name":"Alice"}'); // { name: 'Alice' }
|
|
881
|
+
s.cast.json(s.array(s.number())).parse('[1,2,3]'); // [1, 2, 3]
|
|
882
|
+
s.cast.json(s.object({ name: s.string() })).parse({ name: 'Alice' }); // { name: 'Alice' } — pass-through
|
|
883
|
+
s.cast.json(s.number()).safeParse('not json'); // { success: false, error: ... }
|
|
884
|
+
s.cast.json(s.number(), { message: 'Invalid JSON' }).parse('bad'); // throws: Invalid JSON
|
|
885
|
+
```
|
|
886
|
+
|
|
733
887
|
### Schema Methods
|
|
734
888
|
|
|
735
889
|
#### `parse(value, parseOptions?)`
|
|
@@ -1360,6 +1514,8 @@ const userSchema = s.object({
|
|
|
1360
1514
|
| Bundle size | ~13 KB | ~1.4 KB (core), ~4 KB (full) |
|
|
1361
1515
|
| Email validation | `.email()` built-in | Custom extension (see [Extending Schemas](#extending-schemas)) |
|
|
1362
1516
|
| Error format | Native Error | Plain object `{ success, error, errors }` |
|
|
1517
|
+
| Coerce | `z.coerce.number()` | `s.coerce.number()` |
|
|
1518
|
+
| Smart cast | No direct equivalent | `s.cast.number()` — rejects nulls, understands `'yes'/'no'`, etc. |
|
|
1363
1519
|
|
|
1364
1520
|
**Migration Tips:**
|
|
1365
1521
|
|
package/dist/array.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';var
|
|
1
|
+
'use strict';var E={abortEarly:true};function b(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(b(e.errors[r],n));}function T(e){return {...E,...e}}var y=e=>typeof e=="string"||e instanceof String,d=e=>(typeof e=="number"||e instanceof Number)&&!Number.isNaN(e),g=e=>e===true||e===false,$=e=>e instanceof Date&&!Number.isNaN(e.getTime()),_=e=>typeof e=="function",D=e=>Array.isArray(e),x=e=>typeof e=="object"&&e!==null&&!Array.isArray(e),m={object(e,a){let n=S(x,{...a,type:"object"});return n._getDescription=()=>`object({ ${Object.entries(e).map(([u,c])=>`${u}: ${c._getDescription()}`).join(", ")} })`,h(n,"_parse",(r,u,c)=>{let t=r(u,c),{abortEarly:s}=T(c);if(t.success===false)return t;let o={},i=[];for(let p in e){let f=e[p]._parse(t.data[p],c);if(f.success)o[p]=f.data;else {if(f=f,s!==false){let I=b(f.error,p);return {success:false,error:I,errors:[I]}}w(f,i,p);}}return i.length>0?{success:false,error:i[0],errors:i}:{success:true,data:o}}),n},string(e){return S(y,{...e,type:"string"})},number(e){return S(d,{...e,type:"number"})},boolean(e){return S(g,{...e,type:"boolean"})},date(e){return S($,{...e,type:"date"})},function(e){return S(_,{...e,type:"function"})},enum(e,a){let n=t=>e.includes(t),r=t=>`Invalid ${u} value. Expected ${e.map(s=>`"${s}"`).join(" | ")}, received "${t}".`,u="enum",c=S(n,{message:r,...a,type:u});return c._getDescription=()=>`enum(${e.map(t=>`"${t}"`).join(" | ")})`,c},array(e,a){let n=S(D,{...a,type:"array"});return n._getDescription=()=>`array(${e._getDescription()})`,h(n,"_parse",(r,u,c)=>{let t=r(u,c),{abortEarly:s}=T(c);if(t.success===false)return t;let o=[],i=[];for(let p=0;p<t.data.length;p++){let f=e._parse(t.data[p],c);if(f.success)o.push(f.data);else {if(f=f,s!==false){let I=b(f.error,p);return {success:false,error:I,errors:[I]}}w(f,i,p);}}return i.length>0?{success:false,error:i[0],errors:i}:{success:true,data:o}}),n},any(){return S(()=>true)},preprocess(e,a){return h(a,"_parse",(n,r)=>(r=e(r),n(r))),a},union(e,a){return S(c=>{for(let t=0;t<e.length;t++){let s=e[t]._parse(c);if(s.success)return s}return false},{message:c=>`Invalid union value. Expected the value to match one of the schemas:${e.map((t,s)=>` ${s+1}. ${t._getDescription()}`).join(",")} but received "${typeof c}" with value: ${x(c)?JSON.stringify(c):`"${c}"`}`,...a,type:"union"})},literal(e,a){let u=S(c=>c===e,{message:c=>a?.message?typeof a.message=="function"?a.message(c):a.message:`Expected literal value "${e}", received "${c}"`,name:a?.name,type:"literal"});return u._getDescription=()=>`literal("${e}")`,u},coerce:{string(e){return m.preprocess(a=>String(a),m.string(e))},number(e){let a=e?.message??(n=>`Cannot coerce "${n}" to a valid number.`);return m.preprocess(n=>Number(n),m.number({...e,message:a}))},boolean(e){return m.preprocess(a=>!!a,m.boolean(e))},date(e){let a=e?.message??(n=>`Cannot coerce "${n}" to a valid date.`);return m.preprocess(n=>new Date(n),m.date({...e,message:a}))}},cast:{boolean(e){let a=e?.message??(n=>`Cannot cast "${n}" to boolean. Accepted: true/false, 1/0, yes/no, on/off.`);return m.preprocess(n=>{let r;return y(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},m.boolean({...e,message:a}))},number(e){let a=e?.message??(n=>`Cannot cast "${n}" to a number. Expected a numeric string or number.`);return m.preprocess(n=>{if(g(n))return Number(n);if(y(n)){let r=n.trim();if(r==="")return n;let u=Number(r);if(Number.isFinite(u))return u}return n},m.number({...e,message:a}))},string(e){let a=e?.message??(n=>`Cannot cast "${n}" to string. Expected a string, number, or boolean.`);return m.preprocess(n=>g(n)||d(n)&&Number.isFinite(n)?String(n):n,m.string({...e,message:a}))},date(e){let a=e?.message??(n=>`Cannot cast "${n}" to a valid date.`);return m.preprocess(n=>{let r;return y(n)&&(r=n.trim()),d(n)&&Number.isFinite(n)||r?new Date(r??n):n},m.date({...e,message:a}))},json(e,a){let n=a?.message??(r=>`Cannot parse "${r}" as JSON.`);return h(e,"_parse",(r,u)=>{if(y(u))try{u=JSON.parse(u);}catch{let c={message:typeof n=="function"?n(u):n};return {success:false,error:c,errors:[c]}}return r(u)}),e}}};function A(e){return a=>`The value "${a}" must be type of ${e} but is type of "${typeof a}".`}function h(e,a,n){let r=e[a];e[a]=(...u)=>n(r,...u);}function S(e,{type:a="any",name:n,message:r}={}){r=r||A(a);let u={name:n,message:r,type:a},c={_getName(){return n},_getType(){return a},_getDescription(){return this._getName()??this._getType()},_parse(t,s){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,s){let o=c._parse(t,s);if(!o.success)throw o=o,new Error(o.error.message,{cause:o.error.cause});return o.data},safeParse(t,s){return c._parse(t,s)},transform(t){return h(this,"_parse",(s,o,i)=>{let p=s(o,i);return p.success&&(p.data=t(p.data)),p}),this},optional(){return h(this,"_parse",(t,s,o)=>{let i=t(s,o);return !i.success&&s===void 0&&(i.data=void 0,i.success=true),i}),this},nullable(){return h(this,"_parse",(t,s,o)=>{let i=t(s,o);return !i.success&&s===null&&(i.data=null,i.success=true),i}),this},nullish(){return h(this,"_parse",(t,s,o)=>{let i=t(s,o);return !i.success&&s==null&&(i.success=true,i.data=s),i}),this},default(t){return h(this,"_parse",(s,o,i)=>(o===void 0&&(o=typeof t=="function"?t():t),s(o,i))),this},catch(t){return h(this,"_parse",(s,o,i)=>{let p=s(o,i);return p.success?p:{success:true,data:typeof t=="function"?t({input:o,error:p.error}):t}}),this},pipe(t){return h(this,"_parse",(s,o,i)=>{let p=s(o,i);return p.success?t._parse(p.data,i):p}),this},refine(t,{message:s,type:o}={}){return o&&(s=s||A(o),a=o),h(this,"_parse",(i,p,f)=>{let I=i(p,f);T(f);if(!I.success)return I;let l=t(I.data);if(l===true||typeof l=="object"&&l?.success===true)return I;let k={message:typeof s=="function"?s(p):s};return {success:false,error:k,errors:[k]}}),this}};return O.length>0?O.reduce((t,s)=>s(t,e,u)??t,c):c}var O=[];function P(e){if(typeof e!="function")throw new TypeError("extend() requires a function argument");O.push(e);}P((e,a,n)=>{if(n?.type==="array"){let r=e;r.min=function(u,{message:c}={}){return this.refine(t=>t.length>=u,{message:c||`Array must contain at least ${u} items.`})},r.max=function(u,{message:c}={}){return this.refine(t=>t.length<=u,{message:c||`Array must contain at most ${u} items.`})},r.length=function(u,{message:c}={}){return this.refine(t=>t.length===u,{message:c||`Array must contain exactly ${u} items.`})},r.nonEmpty=function({message:u}={}){return this.refine(c=>c.length>0,{message:u||"Array must not be empty."})},r.unique=function({message:u}={}){return this.refine(c=>{let t=new Set;try{return c.every(s=>{let o=JSON.stringify(s);return t.has(o)?!1:(t.add(o),!0)})}catch{return new Set(c).size===c.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.extend=P;exports.hookOriginal=h;exports.s=m;exports.schema=m;
|
package/dist/array.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s } from './index.mjs';
|
|
1
|
+
export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, FunctionSchemaInterface, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s, s as schema } from './index.mjs';
|
|
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, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s } from './index.js';
|
|
1
|
+
export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, FunctionSchemaInterface, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s, s as schema } from './index.js';
|
|
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-YYPP27RX.mjs';export{c as extend,b as hookOriginal,a as s,a as schema}from'./chunk-QQOSS5DZ.mjs';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import {c}from'./chunk-QQOSS5DZ.mjs';c((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
|
+
var E={abortEarly:true};function b(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 w(e,t,n){if(e.errors?.length)for(let a=0;a<e.errors.length;a++)t.push(b(e.errors[a],n));}function T(e){return {...E,...e}}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,D=e=>e instanceof Date&&!Number.isNaN(e.getTime()),_=e=>typeof e=="function",$=e=>Array.isArray(e),x=e=>typeof e=="object"&&e!==null&&!Array.isArray(e),m={object(e,t){let n=I(x,{...t,type:"object"});return n._getDescription=()=>`object({ ${Object.entries(e).map(([p,o])=>`${p}: ${o._getDescription()}`).join(", ")} })`,h(n,"_parse",(a,p,o)=>{let r=a(p,o),{abortEarly:s}=T(o);if(r.success===false)return r;let u={},c=[];for(let i in e){let f=e[i]._parse(r.data[i],o);if(f.success)u[i]=f.data;else {if(f=f,s!==false){let l=b(f.error,i);return {success:false,error:l,errors:[l]}}w(f,c,i);}}return c.length>0?{success:false,error:c[0],errors:c}:{success:true,data:u}}),n},string(e){return I(S,{...e,type:"string"})},number(e){return I(y,{...e,type:"number"})},boolean(e){return I(g,{...e,type:"boolean"})},date(e){return I(D,{...e,type:"date"})},function(e){return I(_,{...e,type:"function"})},enum(e,t){let n=r=>e.includes(r),a=r=>`Invalid ${p} value. Expected ${e.map(s=>`"${s}"`).join(" | ")}, received "${r}".`,p="enum",o=I(n,{message:a,...t,type:p});return o._getDescription=()=>`enum(${e.map(r=>`"${r}"`).join(" | ")})`,o},array(e,t){let n=I($,{...t,type:"array"});return n._getDescription=()=>`array(${e._getDescription()})`,h(n,"_parse",(a,p,o)=>{let r=a(p,o),{abortEarly:s}=T(o);if(r.success===false)return r;let u=[],c=[];for(let i=0;i<r.data.length;i++){let f=e._parse(r.data[i],o);if(f.success)u.push(f.data);else {if(f=f,s!==false){let l=b(f.error,i);return {success:false,error:l,errors:[l]}}w(f,c,i);}}return c.length>0?{success:false,error:c[0],errors:c}:{success:true,data:u}}),n},any(){return I(()=>true)},preprocess(e,t){return h(t,"_parse",(n,a)=>(a=e(a),n(a))),t},union(e,t){return I(o=>{for(let r=0;r<e.length;r++){let s=e[r]._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((r,s)=>` ${s+1}. ${r._getDescription()}`).join(",")} but received "${typeof o}" with value: ${x(o)?JSON.stringify(o):`"${o}"`}`,...t,type:"union"})},literal(e,t){let p=I(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 p._getDescription=()=>`literal("${e}")`,p},coerce:{string(e){return m.preprocess(t=>String(t),m.string(e))},number(e){let t=e?.message??(n=>`Cannot coerce "${n}" to a valid number.`);return m.preprocess(n=>Number(n),m.number({...e,message:t}))},boolean(e){return m.preprocess(t=>!!t,m.boolean(e))},date(e){let t=e?.message??(n=>`Cannot coerce "${n}" to a valid date.`);return m.preprocess(n=>new Date(n),m.date({...e,message:t}))}},cast:{boolean(e){let t=e?.message??(n=>`Cannot cast "${n}" to boolean. Accepted: true/false, 1/0, yes/no, on/off.`);return m.preprocess(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},m.boolean({...e,message:t}))},number(e){let t=e?.message??(n=>`Cannot cast "${n}" to a number. Expected a numeric string or number.`);return m.preprocess(n=>{if(g(n))return Number(n);if(S(n)){let a=n.trim();if(a==="")return n;let p=Number(a);if(Number.isFinite(p))return p}return n},m.number({...e,message:t}))},string(e){let t=e?.message??(n=>`Cannot cast "${n}" to string. Expected a string, number, or boolean.`);return m.preprocess(n=>g(n)||y(n)&&Number.isFinite(n)?String(n):n,m.string({...e,message:t}))},date(e){let t=e?.message??(n=>`Cannot cast "${n}" to a valid date.`);return m.preprocess(n=>{let a;return S(n)&&(a=n.trim()),y(n)&&Number.isFinite(n)||a?new Date(a??n):n},m.date({...e,message:t}))},json(e,t){let n=t?.message??(a=>`Cannot parse "${a}" as JSON.`);return h(e,"_parse",(a,p)=>{if(S(p))try{p=JSON.parse(p);}catch{let o={message:typeof n=="function"?n(p):n};return {success:false,error:o,errors:[o]}}return a(p)}),e}}};function P(e){return t=>`The value "${t}" must be type of ${e} but is type of "${typeof t}".`}function h(e,t,n){let a=e[t];e[t]=(...p)=>n(a,...p);}function I(e,{type:t="any",name:n,message:a}={}){a=a||P(t);let p={name:n,message:a,type:t},o={_getName(){return n},_getType(){return t},_getDescription(){return this._getName()??this._getType()},_parse(r,s){let u=e(r);if(u===true)return {success:true,data:r};if(typeof u=="object"&&u?.success===true)return u;let c={message:typeof a=="function"?a(r):a};return {success:false,error:c,errors:[c]}},parse(r,s){let u=o._parse(r,s);if(!u.success)throw u=u,new Error(u.error.message,{cause:u.error.cause});return u.data},safeParse(r,s){return o._parse(r,s)},transform(r){return h(this,"_parse",(s,u,c)=>{let i=s(u,c);return i.success&&(i.data=r(i.data)),i}),this},optional(){return h(this,"_parse",(r,s,u)=>{let c=r(s,u);return !c.success&&s===void 0&&(c.data=void 0,c.success=true),c}),this},nullable(){return h(this,"_parse",(r,s,u)=>{let c=r(s,u);return !c.success&&s===null&&(c.data=null,c.success=true),c}),this},nullish(){return h(this,"_parse",(r,s,u)=>{let c=r(s,u);return !c.success&&s==null&&(c.success=true,c.data=s),c}),this},default(r){return h(this,"_parse",(s,u,c)=>(u===void 0&&(u=typeof r=="function"?r():r),s(u,c))),this},catch(r){return h(this,"_parse",(s,u,c)=>{let i=s(u,c);return i.success?i:{success:true,data:typeof r=="function"?r({input:u,error:i.error}):r}}),this},pipe(r){return h(this,"_parse",(s,u,c)=>{let i=s(u,c);return i.success?r._parse(i.data,c):i}),this},refine(r,{message:s,type:u}={}){return u&&(s=s||P(u),t=u),h(this,"_parse",(c,i,f)=>{let l=c(i,f);T(f);if(!l.success)return l;let d=r(l.data);if(d===true||typeof d=="object"&&d?.success===true)return l;let k={message:typeof s=="function"?s(i):s};return {success:false,error:k,errors:[k]}}),this}};return O.length>0?O.reduce((r,s)=>s(r,e,p)??r,o):o}var O=[];function A(e){if(typeof e!="function")throw new TypeError("extend() requires a function argument");O.push(e);}export{m as a,h as b,A as c};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
import {c}from'./chunk-QQOSS5DZ.mjs';c((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
|
+
import {c}from'./chunk-QQOSS5DZ.mjs';c((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});
|
package/dist/full.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';var
|
|
1
|
+
'use strict';var A={abortEarly:true};function O(e,c){if(!c)return e;let s=e?.cause?.key?`${c}.${e.cause.key}`:`${c}`;return {message:`Error parsing key "${s}": ${e.message}`,cause:{key:s}}}function x(e,c,s){if(e.errors?.length)for(let a=0;a<e.errors.length;a++)c.push(O(e.errors[a],s));}function T(e){return {...A,...e}}var l=e=>typeof e=="string"||e instanceof String,d=e=>(typeof e=="number"||e instanceof Number)&&!Number.isNaN(e),b=e=>e===true||e===false,P=e=>e instanceof Date&&!Number.isNaN(e.getTime()),E=e=>typeof e=="function",_=e=>Array.isArray(e),N=e=>typeof e=="object"&&e!==null&&!Array.isArray(e),p={object(e,c){let s=S(N,{...c,type:"object"});return s._getDescription=()=>`object({ ${Object.entries(e).map(([t,r])=>`${t}: ${r._getDescription()}`).join(", ")} })`,h(s,"_parse",(a,t,r)=>{let n=a(t,r),{abortEarly:o}=T(r);if(n.success===false)return n;let u={},i=[];for(let m in e){let f=e[m]._parse(n.data[m],r);if(f.success)u[m]=f.data;else {if(f=f,o!==false){let I=O(f.error,m);return {success:false,error:I,errors:[I]}}x(f,i,m);}}return i.length>0?{success:false,error:i[0],errors:i}:{success:true,data:u}}),s},string(e){return S(l,{...e,type:"string"})},number(e){return S(d,{...e,type:"number"})},boolean(e){return S(b,{...e,type:"boolean"})},date(e){return S(P,{...e,type:"date"})},function(e){return S(E,{...e,type:"function"})},enum(e,c){let s=n=>e.includes(n),a=n=>`Invalid ${t} value. Expected ${e.map(o=>`"${o}"`).join(" | ")}, received "${n}".`,t="enum",r=S(s,{message:a,...c,type:t});return r._getDescription=()=>`enum(${e.map(n=>`"${n}"`).join(" | ")})`,r},array(e,c){let s=S(_,{...c,type:"array"});return s._getDescription=()=>`array(${e._getDescription()})`,h(s,"_parse",(a,t,r)=>{let n=a(t,r),{abortEarly:o}=T(r);if(n.success===false)return n;let u=[],i=[];for(let m=0;m<n.data.length;m++){let f=e._parse(n.data[m],r);if(f.success)u.push(f.data);else {if(f=f,o!==false){let I=O(f.error,m);return {success:false,error:I,errors:[I]}}x(f,i,m);}}return i.length>0?{success:false,error:i[0],errors:i}:{success:true,data:u}}),s},any(){return S(()=>true)},preprocess(e,c){return h(c,"_parse",(s,a)=>(a=e(a),s(a))),c},union(e,c){return S(r=>{for(let n=0;n<e.length;n++){let o=e[n]._parse(r);if(o.success)return o}return false},{message:r=>`Invalid union value. Expected the value to match one of the schemas:${e.map((n,o)=>` ${o+1}. ${n._getDescription()}`).join(",")} but received "${typeof r}" with value: ${N(r)?JSON.stringify(r):`"${r}"`}`,...c,type:"union"})},literal(e,c){let t=S(r=>r===e,{message:r=>c?.message?typeof c.message=="function"?c.message(r):c.message:`Expected literal value "${e}", received "${r}"`,name:c?.name,type:"literal"});return t._getDescription=()=>`literal("${e}")`,t},coerce:{string(e){return p.preprocess(c=>String(c),p.string(e))},number(e){let c=e?.message??(s=>`Cannot coerce "${s}" to a valid number.`);return p.preprocess(s=>Number(s),p.number({...e,message:c}))},boolean(e){return p.preprocess(c=>!!c,p.boolean(e))},date(e){let c=e?.message??(s=>`Cannot coerce "${s}" to a valid date.`);return p.preprocess(s=>new Date(s),p.date({...e,message:c}))}},cast:{boolean(e){let c=e?.message??(s=>`Cannot cast "${s}" to boolean. Accepted: true/false, 1/0, yes/no, on/off.`);return p.preprocess(s=>{let a;return l(s)&&(a=s.toLowerCase()),a==="true"||a==="yes"||a==="on"||a==="1"||s===1?true:a==="false"||a==="no"||a==="off"||a==="0"||s===0?false:s},p.boolean({...e,message:c}))},number(e){let c=e?.message??(s=>`Cannot cast "${s}" to a number. Expected a numeric string or number.`);return p.preprocess(s=>{if(b(s))return Number(s);if(l(s)){let a=s.trim();if(a==="")return s;let t=Number(a);if(Number.isFinite(t))return t}return s},p.number({...e,message:c}))},string(e){let c=e?.message??(s=>`Cannot cast "${s}" to string. Expected a string, number, or boolean.`);return p.preprocess(s=>b(s)||d(s)&&Number.isFinite(s)?String(s):s,p.string({...e,message:c}))},date(e){let c=e?.message??(s=>`Cannot cast "${s}" to a valid date.`);return p.preprocess(s=>{let a;return l(s)&&(a=s.trim()),d(s)&&Number.isFinite(s)||a?new Date(a??s):s},p.date({...e,message:c}))},json(e,c){let s=c?.message??(a=>`Cannot parse "${a}" as JSON.`);return h(e,"_parse",(a,t)=>{if(l(t))try{t=JSON.parse(t);}catch{let r={message:typeof s=="function"?s(t):s};return {success:false,error:r,errors:[r]}}return a(t)}),e}}};function $(e){return c=>`The value "${c}" must be type of ${e} but is type of "${typeof c}".`}function h(e,c,s){let a=e[c];e[c]=(...t)=>s(a,...t);}function S(e,{type:c="any",name:s,message:a}={}){a=a||$(c);let t={name:s,message:a,type:c},r={_getName(){return s},_getType(){return c},_getDescription(){return this._getName()??this._getType()},_parse(n,o){let u=e(n);if(u===true)return {success:true,data:n};if(typeof u=="object"&&u?.success===true)return u;let i={message:typeof a=="function"?a(n):a};return {success:false,error:i,errors:[i]}},parse(n,o){let u=r._parse(n,o);if(!u.success)throw u=u,new Error(u.error.message,{cause:u.error.cause});return u.data},safeParse(n,o){return r._parse(n,o)},transform(n){return h(this,"_parse",(o,u,i)=>{let m=o(u,i);return m.success&&(m.data=n(m.data)),m}),this},optional(){return h(this,"_parse",(n,o,u)=>{let i=n(o,u);return !i.success&&o===void 0&&(i.data=void 0,i.success=true),i}),this},nullable(){return h(this,"_parse",(n,o,u)=>{let i=n(o,u);return !i.success&&o===null&&(i.data=null,i.success=true),i}),this},nullish(){return h(this,"_parse",(n,o,u)=>{let i=n(o,u);return !i.success&&o==null&&(i.success=true,i.data=o),i}),this},default(n){return h(this,"_parse",(o,u,i)=>(u===void 0&&(u=typeof n=="function"?n():n),o(u,i))),this},catch(n){return h(this,"_parse",(o,u,i)=>{let m=o(u,i);return m.success?m:{success:true,data:typeof n=="function"?n({input:u,error:m.error}):n}}),this},pipe(n){return h(this,"_parse",(o,u,i)=>{let m=o(u,i);return m.success?n._parse(m.data,i):m}),this},refine(n,{message:o,type:u}={}){return u&&(o=o||$(u),c=u),h(this,"_parse",(i,m,f)=>{let I=i(m,f);T(f);if(!I.success)return I;let y=n(I.data);if(y===true||typeof y=="object"&&y?.success===true)return I;let k={message:typeof o=="function"?o(m):o};return {success:false,error:k,errors:[k]}}),this}};return w.length>0?w.reduce((n,o)=>o(n,e,t)??n,r):r}var w=[];function g(e){if(typeof e!="function")throw new TypeError("extend() requires a function argument");w.push(e);}g((e,c,s)=>{if(s?.type==="string"){let a=e;a.min=function(t,{message:r}={}){return this.refine(n=>n.length>=t,{message:r||(n=>`String must be at least ${t} characters long (received ${n.length} characters: "${n}")`)})},a.max=function(t,{message:r}={}){return this.refine(n=>n.length<=t,{message:r||(n=>`String must be at most ${t} characters long (received ${n.length} characters: "${n}")`)})},a.length=function(t,{message:r}={}){return this.refine(n=>n.length===t,{message:r||(n=>`String must be exactly ${t} characters long (received ${n.length} characters: "${n}")`)})},a.nonEmpty=function({message:t}={}){return this.refine(r=>r.length>0,{message:t||"String must not be empty (received empty string)"})},a.startsWith=function(t,{message:r}={}){return this.refine(n=>n.startsWith(t),{message:r||(n=>`String must start with "${t}" (received: "${n}")`)})},a.endsWith=function(t,{message:r}={}){return this.refine(n=>n.endsWith(t),{message:r||(n=>`String must end with "${t}" (received: "${n}")`)})},a.includes=function(t,{message:r}={}){return this.refine(n=>n.includes(t),{message:r||(n=>`String must include "${t}" (received: "${n}")`)})},a.toLowerCase=function(){return this.transform(t=>t.toLowerCase())},a.toUpperCase=function(){return this.transform(t=>t.toUpperCase())},a.trim=function(){return this.transform(t=>t.trim())},a.padStart=function(t,r=" "){return this.transform(n=>n.padStart(t,r))},a.padEnd=function(t,r=" "){return this.transform(n=>n.padEnd(t,r))},a.replace=function(t,r){return this.transform(n=>n.replace(t,r))};}return e});g((e,c,s)=>{if(s?.type==="number"){let a=e;a.min=function(t,{message:r}={}){return this.refine(n=>n>=t,{message:r||`Number must be greater than or equal to ${t}.`})},a.max=function(t,{message:r}={}){return this.refine(n=>n<=t,{message:r||`Number must be less than or equal to ${t}.`})},a.positive=function({message:t}={}){return this.refine(r=>r>0,{message:t||"Number must be positive."})},a.negative=function({message:t}={}){return this.refine(r=>r<0,{message:t||"Number must be negative."})},a.int=function({message:t}={}){return this.refine(r=>Number.isInteger(r),{message:t||"Number must be an integer."})},a.float=function({message:t}={}){return this.refine(r=>Number.isFinite(r)&&!Number.isInteger(r),{message:t||"Number must be a floating point (non-integer)."})},a.multipleOf=function(t,{message:r}={}){return this.refine(n=>n%t===0,{message:r||`Number must be a multiple of ${t}.`})},a.finite=function({message:t}={}){return this.refine(r=>Number.isFinite(r),{message:t||"Number must be finite."})};}return e});g((e,c,s)=>{if(s?.type==="array"){let a=e;a.min=function(t,{message:r}={}){return this.refine(n=>n.length>=t,{message:r||`Array must contain at least ${t} items.`})},a.max=function(t,{message:r}={}){return this.refine(n=>n.length<=t,{message:r||`Array must contain at most ${t} items.`})},a.length=function(t,{message:r}={}){return this.refine(n=>n.length===t,{message:r||`Array must contain exactly ${t} items.`})},a.nonEmpty=function({message:t}={}){return this.refine(r=>r.length>0,{message:t||"Array must not be empty."})},a.unique=function({message:t}={}){return this.refine(r=>{let n=new Set;try{return r.every(o=>{let u=JSON.stringify(o);return n.has(u)?!1:(n.add(u),!0)})}catch{return new Set(r).size===r.length}},{message:t||"Array items must be unique."})},a.sort=function(){return this.transform(t=>[...t].sort())},a.reverse=function(){return this.transform(t=>[...t].reverse())};}return e});exports.extend=g;exports.hookOriginal=h;exports.s=p;exports.schema=p;
|
package/dist/full.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import './string.mjs';
|
|
2
2
|
import './number.mjs';
|
|
3
3
|
import './array.mjs';
|
|
4
|
-
export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s } from './index.mjs';
|
|
4
|
+
export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, FunctionSchemaInterface, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s, s as schema } from './index.mjs';
|
package/dist/full.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import './string.js';
|
|
2
2
|
import './number.js';
|
|
3
3
|
import './array.js';
|
|
4
|
-
export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s } from './index.js';
|
|
4
|
+
export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, FunctionSchemaInterface, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s, s as schema } from './index.js';
|
package/dist/full.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import'./chunk-
|
|
1
|
+
import'./chunk-YYPP27RX.mjs';import'./chunk-CLEFU3BA.mjs';import'./chunk-YUL3WJPU.mjs';export{c as extend,b as hookOriginal,a as s,a as schema}from'./chunk-QQOSS5DZ.mjs';
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';var
|
|
1
|
+
'use strict';var E={abortEarly:true};function b(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 w(e,t,n){if(e.errors?.length)for(let a=0;a<e.errors.length;a++)t.push(b(e.errors[a],n));}function T(e){return {...E,...e}}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,D=e=>e instanceof Date&&!Number.isNaN(e.getTime()),_=e=>typeof e=="function",$=e=>Array.isArray(e),x=e=>typeof e=="object"&&e!==null&&!Array.isArray(e),m={object(e,t){let n=I(x,{...t,type:"object"});return n._getDescription=()=>`object({ ${Object.entries(e).map(([p,o])=>`${p}: ${o._getDescription()}`).join(", ")} })`,h(n,"_parse",(a,p,o)=>{let r=a(p,o),{abortEarly:s}=T(o);if(r.success===false)return r;let u={},c=[];for(let i in e){let f=e[i]._parse(r.data[i],o);if(f.success)u[i]=f.data;else {if(f=f,s!==false){let l=b(f.error,i);return {success:false,error:l,errors:[l]}}w(f,c,i);}}return c.length>0?{success:false,error:c[0],errors:c}:{success:true,data:u}}),n},string(e){return I(S,{...e,type:"string"})},number(e){return I(y,{...e,type:"number"})},boolean(e){return I(g,{...e,type:"boolean"})},date(e){return I(D,{...e,type:"date"})},function(e){return I(_,{...e,type:"function"})},enum(e,t){let n=r=>e.includes(r),a=r=>`Invalid ${p} value. Expected ${e.map(s=>`"${s}"`).join(" | ")}, received "${r}".`,p="enum",o=I(n,{message:a,...t,type:p});return o._getDescription=()=>`enum(${e.map(r=>`"${r}"`).join(" | ")})`,o},array(e,t){let n=I($,{...t,type:"array"});return n._getDescription=()=>`array(${e._getDescription()})`,h(n,"_parse",(a,p,o)=>{let r=a(p,o),{abortEarly:s}=T(o);if(r.success===false)return r;let u=[],c=[];for(let i=0;i<r.data.length;i++){let f=e._parse(r.data[i],o);if(f.success)u.push(f.data);else {if(f=f,s!==false){let l=b(f.error,i);return {success:false,error:l,errors:[l]}}w(f,c,i);}}return c.length>0?{success:false,error:c[0],errors:c}:{success:true,data:u}}),n},any(){return I(()=>true)},preprocess(e,t){return h(t,"_parse",(n,a)=>(a=e(a),n(a))),t},union(e,t){return I(o=>{for(let r=0;r<e.length;r++){let s=e[r]._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((r,s)=>` ${s+1}. ${r._getDescription()}`).join(",")} but received "${typeof o}" with value: ${x(o)?JSON.stringify(o):`"${o}"`}`,...t,type:"union"})},literal(e,t){let p=I(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 p._getDescription=()=>`literal("${e}")`,p},coerce:{string(e){return m.preprocess(t=>String(t),m.string(e))},number(e){let t=e?.message??(n=>`Cannot coerce "${n}" to a valid number.`);return m.preprocess(n=>Number(n),m.number({...e,message:t}))},boolean(e){return m.preprocess(t=>!!t,m.boolean(e))},date(e){let t=e?.message??(n=>`Cannot coerce "${n}" to a valid date.`);return m.preprocess(n=>new Date(n),m.date({...e,message:t}))}},cast:{boolean(e){let t=e?.message??(n=>`Cannot cast "${n}" to boolean. Accepted: true/false, 1/0, yes/no, on/off.`);return m.preprocess(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},m.boolean({...e,message:t}))},number(e){let t=e?.message??(n=>`Cannot cast "${n}" to a number. Expected a numeric string or number.`);return m.preprocess(n=>{if(g(n))return Number(n);if(S(n)){let a=n.trim();if(a==="")return n;let p=Number(a);if(Number.isFinite(p))return p}return n},m.number({...e,message:t}))},string(e){let t=e?.message??(n=>`Cannot cast "${n}" to string. Expected a string, number, or boolean.`);return m.preprocess(n=>g(n)||y(n)&&Number.isFinite(n)?String(n):n,m.string({...e,message:t}))},date(e){let t=e?.message??(n=>`Cannot cast "${n}" to a valid date.`);return m.preprocess(n=>{let a;return S(n)&&(a=n.trim()),y(n)&&Number.isFinite(n)||a?new Date(a??n):n},m.date({...e,message:t}))},json(e,t){let n=t?.message??(a=>`Cannot parse "${a}" as JSON.`);return h(e,"_parse",(a,p)=>{if(S(p))try{p=JSON.parse(p);}catch{let o={message:typeof n=="function"?n(p):n};return {success:false,error:o,errors:[o]}}return a(p)}),e}}};function P(e){return t=>`The value "${t}" must be type of ${e} but is type of "${typeof t}".`}function h(e,t,n){let a=e[t];e[t]=(...p)=>n(a,...p);}function I(e,{type:t="any",name:n,message:a}={}){a=a||P(t);let p={name:n,message:a,type:t},o={_getName(){return n},_getType(){return t},_getDescription(){return this._getName()??this._getType()},_parse(r,s){let u=e(r);if(u===true)return {success:true,data:r};if(typeof u=="object"&&u?.success===true)return u;let c={message:typeof a=="function"?a(r):a};return {success:false,error:c,errors:[c]}},parse(r,s){let u=o._parse(r,s);if(!u.success)throw u=u,new Error(u.error.message,{cause:u.error.cause});return u.data},safeParse(r,s){return o._parse(r,s)},transform(r){return h(this,"_parse",(s,u,c)=>{let i=s(u,c);return i.success&&(i.data=r(i.data)),i}),this},optional(){return h(this,"_parse",(r,s,u)=>{let c=r(s,u);return !c.success&&s===void 0&&(c.data=void 0,c.success=true),c}),this},nullable(){return h(this,"_parse",(r,s,u)=>{let c=r(s,u);return !c.success&&s===null&&(c.data=null,c.success=true),c}),this},nullish(){return h(this,"_parse",(r,s,u)=>{let c=r(s,u);return !c.success&&s==null&&(c.success=true,c.data=s),c}),this},default(r){return h(this,"_parse",(s,u,c)=>(u===void 0&&(u=typeof r=="function"?r():r),s(u,c))),this},catch(r){return h(this,"_parse",(s,u,c)=>{let i=s(u,c);return i.success?i:{success:true,data:typeof r=="function"?r({input:u,error:i.error}):r}}),this},pipe(r){return h(this,"_parse",(s,u,c)=>{let i=s(u,c);return i.success?r._parse(i.data,c):i}),this},refine(r,{message:s,type:u}={}){return u&&(s=s||P(u),t=u),h(this,"_parse",(c,i,f)=>{let l=c(i,f);T(f);if(!l.success)return l;let d=r(l.data);if(d===true||typeof d=="object"&&d?.success===true)return l;let k={message:typeof s=="function"?s(i):s};return {success:false,error:k,errors:[k]}}),this}};return O.length>0?O.reduce((r,s)=>s(r,e,p)??r,o):o}var O=[];function A(e){if(typeof e!="function")throw new TypeError("extend() requires a function argument");O.push(e);}exports.extend=A;exports.hookOriginal=h;exports.s=m;exports.schema=m;
|
package/dist/index.d.mts
CHANGED
|
@@ -19,7 +19,7 @@ interface ParseOptions {
|
|
|
19
19
|
abortEarly?: boolean;
|
|
20
20
|
}
|
|
21
21
|
interface SchemaInterface<Input, Output> {
|
|
22
|
-
_getName(): string;
|
|
22
|
+
_getName(): undefined | string;
|
|
23
23
|
_getType(): string;
|
|
24
24
|
_getDescription(): string;
|
|
25
25
|
_parse(value: Input | Partial<Input>, options?: ParseOptions): InternalParseOutput<Output>;
|
|
@@ -51,6 +51,8 @@ interface BooleanSchemaInterface extends SchemaInterface<boolean, boolean> {
|
|
|
51
51
|
}
|
|
52
52
|
interface DateSchemaInterface extends SchemaInterface<Date, Date> {
|
|
53
53
|
}
|
|
54
|
+
interface FunctionSchemaInterface extends SchemaInterface<Function, Function> {
|
|
55
|
+
}
|
|
54
56
|
interface ArraySchemaInterface<T extends SchemaType> extends SchemaInterface<Array<ReturnType<T['parse']>>, Array<ReturnType<T['parse']>>> {
|
|
55
57
|
}
|
|
56
58
|
interface ObjectSchemaInterface<T extends Record<string, SchemaType>> extends SchemaInterface<{
|
|
@@ -59,7 +61,7 @@ interface ObjectSchemaInterface<T extends Record<string, SchemaType>> extends Sc
|
|
|
59
61
|
[Property in keyof T]: ReturnType<T[Property]['parse']>;
|
|
60
62
|
}> {
|
|
61
63
|
}
|
|
62
|
-
type SchemaType = LiteralSchemaInterface<string> | LiteralSchemaInterface<number> | LiteralSchemaInterface<boolean> | StringSchemaInterface | SchemaInterface<unknown, unknown> | SchemaInterface<string, string> | SchemaInterface<string, string | undefined> | SchemaInterface<string, string | null> | SchemaInterface<string, string | undefined | null> | ObjectSchemaInterface<Record<string, SchemaType>> | SchemaInterface<object, object> | SchemaInterface<object, object | undefined> | SchemaInterface<object, object | null> | SchemaInterface<object, object | undefined | null> | NumberSchemaInterface | SchemaInterface<number, number> | SchemaInterface<number, number | undefined> | SchemaInterface<number, number | null> | SchemaInterface<number, number | undefined | null> | BooleanSchemaInterface | SchemaInterface<boolean, boolean> | SchemaInterface<boolean, boolean | undefined> | SchemaInterface<boolean, boolean | null> | SchemaInterface<boolean, boolean | undefined | null> | DateSchemaInterface | SchemaInterface<Date, Date> | SchemaInterface<Date, Date | undefined> | SchemaInterface<Date, Date | null> | SchemaInterface<Date, Date | undefined | null> | EnumSchemaInterface<string> | UnionSchemaInterface<Array<SchemaInterface<unknown, unknown>>> | ArraySchemaInterface<StringSchemaInterface | ObjectSchemaInterface<Record<string, SchemaType>> | NumberSchemaInterface | BooleanSchemaInterface | DateSchemaInterface | EnumSchemaInterface<string>> | SchemaInterface<Array<unknown>, Array<unknown>> | SchemaInterface<Array<unknown>, Array<unknown> | undefined> | SchemaInterface<Array<unknown>, Array<unknown> | null> | SchemaInterface<Array<unknown>, Array<unknown> | undefined | null>;
|
|
64
|
+
type SchemaType = LiteralSchemaInterface<string> | LiteralSchemaInterface<number> | LiteralSchemaInterface<boolean> | StringSchemaInterface | SchemaInterface<unknown, unknown> | SchemaInterface<string, string> | SchemaInterface<string, string | undefined> | SchemaInterface<string, string | null> | SchemaInterface<string, string | undefined | null> | ObjectSchemaInterface<Record<string, SchemaType>> | SchemaInterface<object, object> | SchemaInterface<object, object | undefined> | SchemaInterface<object, object | null> | SchemaInterface<object, object | undefined | null> | NumberSchemaInterface | SchemaInterface<number, number> | SchemaInterface<number, number | undefined> | SchemaInterface<number, number | null> | SchemaInterface<number, number | undefined | null> | BooleanSchemaInterface | SchemaInterface<boolean, boolean> | SchemaInterface<boolean, boolean | undefined> | SchemaInterface<boolean, boolean | null> | SchemaInterface<boolean, boolean | undefined | null> | DateSchemaInterface | SchemaInterface<Date, Date> | SchemaInterface<Date, Date | undefined> | SchemaInterface<Date, Date | null> | SchemaInterface<Date, Date | undefined | null> | FunctionSchemaInterface | EnumSchemaInterface<string> | UnionSchemaInterface<Array<SchemaInterface<unknown, unknown>>> | ArraySchemaInterface<StringSchemaInterface | ObjectSchemaInterface<Record<string, SchemaType>> | NumberSchemaInterface | BooleanSchemaInterface | DateSchemaInterface | EnumSchemaInterface<string>> | SchemaInterface<Array<unknown>, Array<unknown>> | SchemaInterface<Array<unknown>, Array<unknown> | undefined> | SchemaInterface<Array<unknown>, Array<unknown> | null> | SchemaInterface<Array<unknown>, Array<unknown> | undefined | null>;
|
|
63
65
|
type ErrorMessage = string | ((value: unknown) => string);
|
|
64
66
|
type ExtenderType = (inter: SchemaType, validation: Function, options?: {
|
|
65
67
|
message: ErrorMessage;
|
|
@@ -140,6 +142,20 @@ declare const s: {
|
|
|
140
142
|
* ```
|
|
141
143
|
*/
|
|
142
144
|
date(options?: SchemaInterfaceOptions): DateSchemaInterface;
|
|
145
|
+
/**
|
|
146
|
+
* Creates a function schema that validates the value is callable.
|
|
147
|
+
*
|
|
148
|
+
* @param options - Optional configuration (name, message)
|
|
149
|
+
* @returns Function schema interface
|
|
150
|
+
*
|
|
151
|
+
* @example
|
|
152
|
+
* ```typescript
|
|
153
|
+
* const callbackSchema = s.function();
|
|
154
|
+
* callbackSchema.parse(() => {}); // () => {}
|
|
155
|
+
* callbackSchema.parse('hello'); // throws
|
|
156
|
+
* ```
|
|
157
|
+
*/
|
|
158
|
+
function(options?: SchemaInterfaceOptions): FunctionSchemaInterface;
|
|
143
159
|
/**
|
|
144
160
|
* Creates an enum schema with predefined values.
|
|
145
161
|
*
|
|
@@ -179,7 +195,7 @@ declare const s: {
|
|
|
179
195
|
* const result = anySchema.parse({ anything: true }); // { anything: true }
|
|
180
196
|
* ```
|
|
181
197
|
*/
|
|
182
|
-
any():
|
|
198
|
+
any(): SchemaInterface<unknown, unknown>;
|
|
183
199
|
/**
|
|
184
200
|
* Preprocesses a value before passing it to a schema for validation.
|
|
185
201
|
*
|
|
@@ -228,6 +244,113 @@ declare const s: {
|
|
|
228
244
|
* ```
|
|
229
245
|
*/
|
|
230
246
|
literal<T extends string | number | boolean>(value: T, options?: SchemaInterfaceOptions): LiteralSchemaInterface<T>;
|
|
247
|
+
/**
|
|
248
|
+
* Coerce schemas that apply a native JS constructor before validation.
|
|
249
|
+
* Unlike `s.preprocess`, coerce provides a consistent API for common type
|
|
250
|
+
* conversions with clear error messages when coercion produces an invalid result.
|
|
251
|
+
*
|
|
252
|
+
* @example
|
|
253
|
+
* ```typescript
|
|
254
|
+
* s.coerce.number().parse('42'); // 42
|
|
255
|
+
* s.coerce.string().parse(123); // '123'
|
|
256
|
+
* s.coerce.boolean().parse(0); // false
|
|
257
|
+
* s.coerce.date().parse('2024-01-01'); // Date object
|
|
258
|
+
* ```
|
|
259
|
+
*/
|
|
260
|
+
coerce: {
|
|
261
|
+
/**
|
|
262
|
+
* Creates a string schema that coerces input using `String(value)`.
|
|
263
|
+
* Always succeeds — `String()` never produces an invalid string.
|
|
264
|
+
*/
|
|
265
|
+
string(options?: SchemaInterfaceOptions): StringSchemaInterface;
|
|
266
|
+
/**
|
|
267
|
+
* Creates a number schema that coerces input using `Number(value)`.
|
|
268
|
+
* Fails when the result is `NaN` (e.g. `'bad'`, `undefined`, plain objects).
|
|
269
|
+
*/
|
|
270
|
+
number(options?: SchemaInterfaceOptions): NumberSchemaInterface;
|
|
271
|
+
/**
|
|
272
|
+
* Creates a boolean schema that coerces input using `Boolean(value)`.
|
|
273
|
+
* Always succeeds — `Boolean()` always produces `true` or `false`.
|
|
274
|
+
* Note: `Boolean('false')` is `true` because `'false'` is a non-empty string.
|
|
275
|
+
*/
|
|
276
|
+
boolean(options?: SchemaInterfaceOptions): BooleanSchemaInterface;
|
|
277
|
+
/**
|
|
278
|
+
* Creates a date schema that coerces input using `new Date(value)`.
|
|
279
|
+
* Fails when the result is an invalid Date (e.g. `'garbage'`).
|
|
280
|
+
*/
|
|
281
|
+
date(options?: SchemaInterfaceOptions): DateSchemaInterface;
|
|
282
|
+
};
|
|
283
|
+
/**
|
|
284
|
+
* Smart cast schemas that apply semantic conversion before validation.
|
|
285
|
+
* Unlike `s.coerce` (which uses raw JS constructors), `s.cast` understands
|
|
286
|
+
* common programmer-friendly string representations and rejects ambiguous
|
|
287
|
+
* inputs such as `null`, `undefined`, and empty strings.
|
|
288
|
+
*
|
|
289
|
+
* Differences from `s.coerce`:
|
|
290
|
+
* - `cast.boolean('false')` → `false` (coerce gives `true` — non-empty string)
|
|
291
|
+
* - `cast.boolean('yes'/'no'/'on'/'off')` → `true`/`false` (coerce doesn't understand these)
|
|
292
|
+
* - `cast.number(null)` → throws (coerce gives `0`)
|
|
293
|
+
* - `cast.number('')` → throws (coerce gives `0`)
|
|
294
|
+
* - `cast.string(null)` → throws (coerce gives `'null'`)
|
|
295
|
+
* - `cast.date(null)` → throws (coerce gives epoch Date)
|
|
296
|
+
*
|
|
297
|
+
* @example
|
|
298
|
+
* ```typescript
|
|
299
|
+
* s.cast.boolean().parse('false'); // false — unlike coerce!
|
|
300
|
+
* s.cast.boolean().parse('yes'); // true
|
|
301
|
+
* s.cast.boolean().parse('on'); // true
|
|
302
|
+
* s.cast.number().parse(' 42 '); // 42 — trims whitespace
|
|
303
|
+
* s.cast.number().parse(null); // throws
|
|
304
|
+
* s.cast.string().parse(123); // '123'
|
|
305
|
+
* s.cast.string().parse(null); // throws — unlike coerce!
|
|
306
|
+
* s.cast.date().parse('2024-01-01'); // Date object
|
|
307
|
+
* s.cast.date().parse(null); // throws — unlike coerce!
|
|
308
|
+
* ```
|
|
309
|
+
*/
|
|
310
|
+
cast: {
|
|
311
|
+
/**
|
|
312
|
+
* Creates a boolean schema with semantic string casting.
|
|
313
|
+
* Recognises (case-insensitive): `'true'/'false'`, `'yes'/'no'`, `'on'/'off'`, `'1'/'0'`.
|
|
314
|
+
* Numbers `1` and `0` are accepted; any other number throws.
|
|
315
|
+
* `null`, `undefined`, and unrecognised strings throw.
|
|
316
|
+
*/
|
|
317
|
+
boolean(options?: SchemaInterfaceOptions): BooleanSchemaInterface;
|
|
318
|
+
/**
|
|
319
|
+
* Creates a number schema with smart string parsing.
|
|
320
|
+
* Trims whitespace from strings before converting. Accepts booleans (`true`→1, `false`→0).
|
|
321
|
+
* Rejects `null`, `undefined`, empty/whitespace-only strings, and non-numeric strings.
|
|
322
|
+
*/
|
|
323
|
+
number(options?: SchemaInterfaceOptions): NumberSchemaInterface;
|
|
324
|
+
/**
|
|
325
|
+
* Creates a string schema that accepts strings, finite numbers, and booleans.
|
|
326
|
+
* Rejects `null`, `undefined`, objects, arrays, `NaN`, and `Infinity`.
|
|
327
|
+
*/
|
|
328
|
+
string(options?: SchemaInterfaceOptions): StringSchemaInterface;
|
|
329
|
+
/**
|
|
330
|
+
* Creates a date schema with controlled casting.
|
|
331
|
+
* Accepts ISO date strings (non-empty), finite integer timestamps, and existing Date objects.
|
|
332
|
+
* Rejects `null`, `undefined`, booleans, empty strings, and non-finite numbers.
|
|
333
|
+
*/
|
|
334
|
+
date(options?: SchemaInterfaceOptions): DateSchemaInterface;
|
|
335
|
+
/**
|
|
336
|
+
* Parses a JSON string and validates the result against the provided schema.
|
|
337
|
+
* If the input is not a string, it is passed directly to the inner schema.
|
|
338
|
+
* Produces a proper validation failure (never throws) when the JSON is malformed.
|
|
339
|
+
*
|
|
340
|
+
* @param schema - Schema to validate the parsed value against
|
|
341
|
+
* @param options - Optional configuration (name, message)
|
|
342
|
+
* @returns The same schema type, with JSON string preprocessing applied
|
|
343
|
+
*
|
|
344
|
+
* @example
|
|
345
|
+
* ```typescript
|
|
346
|
+
* const schema = s.cast.json(s.object({ name: s.string() }));
|
|
347
|
+
* schema.parse('{"name":"Alice"}'); // { name: 'Alice' }
|
|
348
|
+
* schema.parse({ name: 'Alice' }); // { name: 'Alice' } — pass-through
|
|
349
|
+
* schema.safeParse('not json'); // { success: false, error: ... }
|
|
350
|
+
* ```
|
|
351
|
+
*/
|
|
352
|
+
json<T extends SchemaType>(schema: T, options?: SchemaInterfaceOptions): T;
|
|
353
|
+
};
|
|
231
354
|
};
|
|
232
355
|
declare function hookOriginal<Input, Output>(object: SchemaInterface<Input, Output> | SchemaType, method: keyof (SchemaInterface<Input, Output> | SchemaType), action: (original: Function, ...args: unknown[]) => InternalParseOutput<Output>): void;
|
|
233
356
|
/**
|
|
@@ -288,4 +411,4 @@ declare function extend(callback: ExtenderType): void;
|
|
|
288
411
|
*/
|
|
289
412
|
type Infer<T> = T extends SchemaType ? ReturnType<T['parse']> : unknown;
|
|
290
413
|
|
|
291
|
-
export { type ArraySchemaInterface, type BooleanSchemaInterface, type DateSchemaInterface, type EnumSchemaInterface, type ErrorStructure, type ExtenderType, type Infer, type Invalid, type LiteralSchemaInterface, type NumberSchemaInterface, type ObjectSchemaInterface, type SchemaInterface, type SchemaInterfaceOptions, type SchemaType, type StringSchemaInterface, type UnionSchemaInterface, type Valid, extend, hookOriginal, s };
|
|
414
|
+
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, extend, hookOriginal, s, s as schema };
|
package/dist/index.d.ts
CHANGED
|
@@ -19,7 +19,7 @@ interface ParseOptions {
|
|
|
19
19
|
abortEarly?: boolean;
|
|
20
20
|
}
|
|
21
21
|
interface SchemaInterface<Input, Output> {
|
|
22
|
-
_getName(): string;
|
|
22
|
+
_getName(): undefined | string;
|
|
23
23
|
_getType(): string;
|
|
24
24
|
_getDescription(): string;
|
|
25
25
|
_parse(value: Input | Partial<Input>, options?: ParseOptions): InternalParseOutput<Output>;
|
|
@@ -51,6 +51,8 @@ interface BooleanSchemaInterface extends SchemaInterface<boolean, boolean> {
|
|
|
51
51
|
}
|
|
52
52
|
interface DateSchemaInterface extends SchemaInterface<Date, Date> {
|
|
53
53
|
}
|
|
54
|
+
interface FunctionSchemaInterface extends SchemaInterface<Function, Function> {
|
|
55
|
+
}
|
|
54
56
|
interface ArraySchemaInterface<T extends SchemaType> extends SchemaInterface<Array<ReturnType<T['parse']>>, Array<ReturnType<T['parse']>>> {
|
|
55
57
|
}
|
|
56
58
|
interface ObjectSchemaInterface<T extends Record<string, SchemaType>> extends SchemaInterface<{
|
|
@@ -59,7 +61,7 @@ interface ObjectSchemaInterface<T extends Record<string, SchemaType>> extends Sc
|
|
|
59
61
|
[Property in keyof T]: ReturnType<T[Property]['parse']>;
|
|
60
62
|
}> {
|
|
61
63
|
}
|
|
62
|
-
type SchemaType = LiteralSchemaInterface<string> | LiteralSchemaInterface<number> | LiteralSchemaInterface<boolean> | StringSchemaInterface | SchemaInterface<unknown, unknown> | SchemaInterface<string, string> | SchemaInterface<string, string | undefined> | SchemaInterface<string, string | null> | SchemaInterface<string, string | undefined | null> | ObjectSchemaInterface<Record<string, SchemaType>> | SchemaInterface<object, object> | SchemaInterface<object, object | undefined> | SchemaInterface<object, object | null> | SchemaInterface<object, object | undefined | null> | NumberSchemaInterface | SchemaInterface<number, number> | SchemaInterface<number, number | undefined> | SchemaInterface<number, number | null> | SchemaInterface<number, number | undefined | null> | BooleanSchemaInterface | SchemaInterface<boolean, boolean> | SchemaInterface<boolean, boolean | undefined> | SchemaInterface<boolean, boolean | null> | SchemaInterface<boolean, boolean | undefined | null> | DateSchemaInterface | SchemaInterface<Date, Date> | SchemaInterface<Date, Date | undefined> | SchemaInterface<Date, Date | null> | SchemaInterface<Date, Date | undefined | null> | EnumSchemaInterface<string> | UnionSchemaInterface<Array<SchemaInterface<unknown, unknown>>> | ArraySchemaInterface<StringSchemaInterface | ObjectSchemaInterface<Record<string, SchemaType>> | NumberSchemaInterface | BooleanSchemaInterface | DateSchemaInterface | EnumSchemaInterface<string>> | SchemaInterface<Array<unknown>, Array<unknown>> | SchemaInterface<Array<unknown>, Array<unknown> | undefined> | SchemaInterface<Array<unknown>, Array<unknown> | null> | SchemaInterface<Array<unknown>, Array<unknown> | undefined | null>;
|
|
64
|
+
type SchemaType = LiteralSchemaInterface<string> | LiteralSchemaInterface<number> | LiteralSchemaInterface<boolean> | StringSchemaInterface | SchemaInterface<unknown, unknown> | SchemaInterface<string, string> | SchemaInterface<string, string | undefined> | SchemaInterface<string, string | null> | SchemaInterface<string, string | undefined | null> | ObjectSchemaInterface<Record<string, SchemaType>> | SchemaInterface<object, object> | SchemaInterface<object, object | undefined> | SchemaInterface<object, object | null> | SchemaInterface<object, object | undefined | null> | NumberSchemaInterface | SchemaInterface<number, number> | SchemaInterface<number, number | undefined> | SchemaInterface<number, number | null> | SchemaInterface<number, number | undefined | null> | BooleanSchemaInterface | SchemaInterface<boolean, boolean> | SchemaInterface<boolean, boolean | undefined> | SchemaInterface<boolean, boolean | null> | SchemaInterface<boolean, boolean | undefined | null> | DateSchemaInterface | SchemaInterface<Date, Date> | SchemaInterface<Date, Date | undefined> | SchemaInterface<Date, Date | null> | SchemaInterface<Date, Date | undefined | null> | FunctionSchemaInterface | EnumSchemaInterface<string> | UnionSchemaInterface<Array<SchemaInterface<unknown, unknown>>> | ArraySchemaInterface<StringSchemaInterface | ObjectSchemaInterface<Record<string, SchemaType>> | NumberSchemaInterface | BooleanSchemaInterface | DateSchemaInterface | EnumSchemaInterface<string>> | SchemaInterface<Array<unknown>, Array<unknown>> | SchemaInterface<Array<unknown>, Array<unknown> | undefined> | SchemaInterface<Array<unknown>, Array<unknown> | null> | SchemaInterface<Array<unknown>, Array<unknown> | undefined | null>;
|
|
63
65
|
type ErrorMessage = string | ((value: unknown) => string);
|
|
64
66
|
type ExtenderType = (inter: SchemaType, validation: Function, options?: {
|
|
65
67
|
message: ErrorMessage;
|
|
@@ -140,6 +142,20 @@ declare const s: {
|
|
|
140
142
|
* ```
|
|
141
143
|
*/
|
|
142
144
|
date(options?: SchemaInterfaceOptions): DateSchemaInterface;
|
|
145
|
+
/**
|
|
146
|
+
* Creates a function schema that validates the value is callable.
|
|
147
|
+
*
|
|
148
|
+
* @param options - Optional configuration (name, message)
|
|
149
|
+
* @returns Function schema interface
|
|
150
|
+
*
|
|
151
|
+
* @example
|
|
152
|
+
* ```typescript
|
|
153
|
+
* const callbackSchema = s.function();
|
|
154
|
+
* callbackSchema.parse(() => {}); // () => {}
|
|
155
|
+
* callbackSchema.parse('hello'); // throws
|
|
156
|
+
* ```
|
|
157
|
+
*/
|
|
158
|
+
function(options?: SchemaInterfaceOptions): FunctionSchemaInterface;
|
|
143
159
|
/**
|
|
144
160
|
* Creates an enum schema with predefined values.
|
|
145
161
|
*
|
|
@@ -179,7 +195,7 @@ declare const s: {
|
|
|
179
195
|
* const result = anySchema.parse({ anything: true }); // { anything: true }
|
|
180
196
|
* ```
|
|
181
197
|
*/
|
|
182
|
-
any():
|
|
198
|
+
any(): SchemaInterface<unknown, unknown>;
|
|
183
199
|
/**
|
|
184
200
|
* Preprocesses a value before passing it to a schema for validation.
|
|
185
201
|
*
|
|
@@ -228,6 +244,113 @@ declare const s: {
|
|
|
228
244
|
* ```
|
|
229
245
|
*/
|
|
230
246
|
literal<T extends string | number | boolean>(value: T, options?: SchemaInterfaceOptions): LiteralSchemaInterface<T>;
|
|
247
|
+
/**
|
|
248
|
+
* Coerce schemas that apply a native JS constructor before validation.
|
|
249
|
+
* Unlike `s.preprocess`, coerce provides a consistent API for common type
|
|
250
|
+
* conversions with clear error messages when coercion produces an invalid result.
|
|
251
|
+
*
|
|
252
|
+
* @example
|
|
253
|
+
* ```typescript
|
|
254
|
+
* s.coerce.number().parse('42'); // 42
|
|
255
|
+
* s.coerce.string().parse(123); // '123'
|
|
256
|
+
* s.coerce.boolean().parse(0); // false
|
|
257
|
+
* s.coerce.date().parse('2024-01-01'); // Date object
|
|
258
|
+
* ```
|
|
259
|
+
*/
|
|
260
|
+
coerce: {
|
|
261
|
+
/**
|
|
262
|
+
* Creates a string schema that coerces input using `String(value)`.
|
|
263
|
+
* Always succeeds — `String()` never produces an invalid string.
|
|
264
|
+
*/
|
|
265
|
+
string(options?: SchemaInterfaceOptions): StringSchemaInterface;
|
|
266
|
+
/**
|
|
267
|
+
* Creates a number schema that coerces input using `Number(value)`.
|
|
268
|
+
* Fails when the result is `NaN` (e.g. `'bad'`, `undefined`, plain objects).
|
|
269
|
+
*/
|
|
270
|
+
number(options?: SchemaInterfaceOptions): NumberSchemaInterface;
|
|
271
|
+
/**
|
|
272
|
+
* Creates a boolean schema that coerces input using `Boolean(value)`.
|
|
273
|
+
* Always succeeds — `Boolean()` always produces `true` or `false`.
|
|
274
|
+
* Note: `Boolean('false')` is `true` because `'false'` is a non-empty string.
|
|
275
|
+
*/
|
|
276
|
+
boolean(options?: SchemaInterfaceOptions): BooleanSchemaInterface;
|
|
277
|
+
/**
|
|
278
|
+
* Creates a date schema that coerces input using `new Date(value)`.
|
|
279
|
+
* Fails when the result is an invalid Date (e.g. `'garbage'`).
|
|
280
|
+
*/
|
|
281
|
+
date(options?: SchemaInterfaceOptions): DateSchemaInterface;
|
|
282
|
+
};
|
|
283
|
+
/**
|
|
284
|
+
* Smart cast schemas that apply semantic conversion before validation.
|
|
285
|
+
* Unlike `s.coerce` (which uses raw JS constructors), `s.cast` understands
|
|
286
|
+
* common programmer-friendly string representations and rejects ambiguous
|
|
287
|
+
* inputs such as `null`, `undefined`, and empty strings.
|
|
288
|
+
*
|
|
289
|
+
* Differences from `s.coerce`:
|
|
290
|
+
* - `cast.boolean('false')` → `false` (coerce gives `true` — non-empty string)
|
|
291
|
+
* - `cast.boolean('yes'/'no'/'on'/'off')` → `true`/`false` (coerce doesn't understand these)
|
|
292
|
+
* - `cast.number(null)` → throws (coerce gives `0`)
|
|
293
|
+
* - `cast.number('')` → throws (coerce gives `0`)
|
|
294
|
+
* - `cast.string(null)` → throws (coerce gives `'null'`)
|
|
295
|
+
* - `cast.date(null)` → throws (coerce gives epoch Date)
|
|
296
|
+
*
|
|
297
|
+
* @example
|
|
298
|
+
* ```typescript
|
|
299
|
+
* s.cast.boolean().parse('false'); // false — unlike coerce!
|
|
300
|
+
* s.cast.boolean().parse('yes'); // true
|
|
301
|
+
* s.cast.boolean().parse('on'); // true
|
|
302
|
+
* s.cast.number().parse(' 42 '); // 42 — trims whitespace
|
|
303
|
+
* s.cast.number().parse(null); // throws
|
|
304
|
+
* s.cast.string().parse(123); // '123'
|
|
305
|
+
* s.cast.string().parse(null); // throws — unlike coerce!
|
|
306
|
+
* s.cast.date().parse('2024-01-01'); // Date object
|
|
307
|
+
* s.cast.date().parse(null); // throws — unlike coerce!
|
|
308
|
+
* ```
|
|
309
|
+
*/
|
|
310
|
+
cast: {
|
|
311
|
+
/**
|
|
312
|
+
* Creates a boolean schema with semantic string casting.
|
|
313
|
+
* Recognises (case-insensitive): `'true'/'false'`, `'yes'/'no'`, `'on'/'off'`, `'1'/'0'`.
|
|
314
|
+
* Numbers `1` and `0` are accepted; any other number throws.
|
|
315
|
+
* `null`, `undefined`, and unrecognised strings throw.
|
|
316
|
+
*/
|
|
317
|
+
boolean(options?: SchemaInterfaceOptions): BooleanSchemaInterface;
|
|
318
|
+
/**
|
|
319
|
+
* Creates a number schema with smart string parsing.
|
|
320
|
+
* Trims whitespace from strings before converting. Accepts booleans (`true`→1, `false`→0).
|
|
321
|
+
* Rejects `null`, `undefined`, empty/whitespace-only strings, and non-numeric strings.
|
|
322
|
+
*/
|
|
323
|
+
number(options?: SchemaInterfaceOptions): NumberSchemaInterface;
|
|
324
|
+
/**
|
|
325
|
+
* Creates a string schema that accepts strings, finite numbers, and booleans.
|
|
326
|
+
* Rejects `null`, `undefined`, objects, arrays, `NaN`, and `Infinity`.
|
|
327
|
+
*/
|
|
328
|
+
string(options?: SchemaInterfaceOptions): StringSchemaInterface;
|
|
329
|
+
/**
|
|
330
|
+
* Creates a date schema with controlled casting.
|
|
331
|
+
* Accepts ISO date strings (non-empty), finite integer timestamps, and existing Date objects.
|
|
332
|
+
* Rejects `null`, `undefined`, booleans, empty strings, and non-finite numbers.
|
|
333
|
+
*/
|
|
334
|
+
date(options?: SchemaInterfaceOptions): DateSchemaInterface;
|
|
335
|
+
/**
|
|
336
|
+
* Parses a JSON string and validates the result against the provided schema.
|
|
337
|
+
* If the input is not a string, it is passed directly to the inner schema.
|
|
338
|
+
* Produces a proper validation failure (never throws) when the JSON is malformed.
|
|
339
|
+
*
|
|
340
|
+
* @param schema - Schema to validate the parsed value against
|
|
341
|
+
* @param options - Optional configuration (name, message)
|
|
342
|
+
* @returns The same schema type, with JSON string preprocessing applied
|
|
343
|
+
*
|
|
344
|
+
* @example
|
|
345
|
+
* ```typescript
|
|
346
|
+
* const schema = s.cast.json(s.object({ name: s.string() }));
|
|
347
|
+
* schema.parse('{"name":"Alice"}'); // { name: 'Alice' }
|
|
348
|
+
* schema.parse({ name: 'Alice' }); // { name: 'Alice' } — pass-through
|
|
349
|
+
* schema.safeParse('not json'); // { success: false, error: ... }
|
|
350
|
+
* ```
|
|
351
|
+
*/
|
|
352
|
+
json<T extends SchemaType>(schema: T, options?: SchemaInterfaceOptions): T;
|
|
353
|
+
};
|
|
231
354
|
};
|
|
232
355
|
declare function hookOriginal<Input, Output>(object: SchemaInterface<Input, Output> | SchemaType, method: keyof (SchemaInterface<Input, Output> | SchemaType), action: (original: Function, ...args: unknown[]) => InternalParseOutput<Output>): void;
|
|
233
356
|
/**
|
|
@@ -288,4 +411,4 @@ declare function extend(callback: ExtenderType): void;
|
|
|
288
411
|
*/
|
|
289
412
|
type Infer<T> = T extends SchemaType ? ReturnType<T['parse']> : unknown;
|
|
290
413
|
|
|
291
|
-
export { type ArraySchemaInterface, type BooleanSchemaInterface, type DateSchemaInterface, type EnumSchemaInterface, type ErrorStructure, type ExtenderType, type Infer, type Invalid, type LiteralSchemaInterface, type NumberSchemaInterface, type ObjectSchemaInterface, type SchemaInterface, type SchemaInterfaceOptions, type SchemaType, type StringSchemaInterface, type UnionSchemaInterface, type Valid, extend, hookOriginal, s };
|
|
414
|
+
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, extend, hookOriginal, s, s as schema };
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export{c as extend,b as hookOriginal,a as s}from'./chunk-
|
|
1
|
+
export{c as extend,b as hookOriginal,a as s,a as schema}from'./chunk-QQOSS5DZ.mjs';
|
package/dist/number.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';var
|
|
1
|
+
'use strict';var E={abortEarly:true};function g(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(g(e.errors[r],n));}function O(e){return {...E,...e}}var l=e=>typeof e=="string"||e instanceof String,b=e=>(typeof e=="number"||e instanceof Number)&&!Number.isNaN(e),y=e=>e===true||e===false,$=e=>e instanceof Date&&!Number.isNaN(e.getTime()),_=e=>typeof e=="function",D=e=>Array.isArray(e),x=e=>typeof e=="object"&&e!==null&&!Array.isArray(e),p={object(e,a){let n=I(x,{...a,type:"object"});return n._getDescription=()=>`object({ ${Object.entries(e).map(([c,s])=>`${c}: ${s._getDescription()}`).join(", ")} })`,h(n,"_parse",(r,c,s)=>{let t=r(c,s),{abortEarly:u}=O(s);if(t.success===false)return t;let i={},o=[];for(let m in e){let f=e[m]._parse(t.data[m],s);if(f.success)i[m]=f.data;else {if(f=f,u!==false){let S=g(f.error,m);return {success:false,error:S,errors:[S]}}w(f,o,m);}}return o.length>0?{success:false,error:o[0],errors:o}:{success:true,data:i}}),n},string(e){return I(l,{...e,type:"string"})},number(e){return I(b,{...e,type:"number"})},boolean(e){return I(y,{...e,type:"boolean"})},date(e){return I($,{...e,type:"date"})},function(e){return I(_,{...e,type:"function"})},enum(e,a){let n=t=>e.includes(t),r=t=>`Invalid ${c} value. Expected ${e.map(u=>`"${u}"`).join(" | ")}, received "${t}".`,c="enum",s=I(n,{message:r,...a,type:c});return s._getDescription=()=>`enum(${e.map(t=>`"${t}"`).join(" | ")})`,s},array(e,a){let n=I(D,{...a,type:"array"});return n._getDescription=()=>`array(${e._getDescription()})`,h(n,"_parse",(r,c,s)=>{let t=r(c,s),{abortEarly:u}=O(s);if(t.success===false)return t;let i=[],o=[];for(let m=0;m<t.data.length;m++){let f=e._parse(t.data[m],s);if(f.success)i.push(f.data);else {if(f=f,u!==false){let S=g(f.error,m);return {success:false,error:S,errors:[S]}}w(f,o,m);}}return o.length>0?{success:false,error:o[0],errors:o}:{success:true,data:i}}),n},any(){return I(()=>true)},preprocess(e,a){return h(a,"_parse",(n,r)=>(r=e(r),n(r))),a},union(e,a){return I(s=>{for(let t=0;t<e.length;t++){let u=e[t]._parse(s);if(u.success)return u}return false},{message:s=>`Invalid union value. Expected the value to match one of the schemas:${e.map((t,u)=>` ${u+1}. ${t._getDescription()}`).join(",")} but received "${typeof s}" with value: ${x(s)?JSON.stringify(s):`"${s}"`}`,...a,type:"union"})},literal(e,a){let c=I(s=>s===e,{message:s=>a?.message?typeof a.message=="function"?a.message(s):a.message:`Expected literal value "${e}", received "${s}"`,name:a?.name,type:"literal"});return c._getDescription=()=>`literal("${e}")`,c},coerce:{string(e){return p.preprocess(a=>String(a),p.string(e))},number(e){let a=e?.message??(n=>`Cannot coerce "${n}" to a valid number.`);return p.preprocess(n=>Number(n),p.number({...e,message:a}))},boolean(e){return p.preprocess(a=>!!a,p.boolean(e))},date(e){let a=e?.message??(n=>`Cannot coerce "${n}" to a valid date.`);return p.preprocess(n=>new Date(n),p.date({...e,message:a}))}},cast:{boolean(e){let a=e?.message??(n=>`Cannot cast "${n}" to boolean. Accepted: true/false, 1/0, yes/no, on/off.`);return p.preprocess(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},p.boolean({...e,message:a}))},number(e){let a=e?.message??(n=>`Cannot cast "${n}" to a number. Expected a numeric string or number.`);return p.preprocess(n=>{if(y(n))return Number(n);if(l(n)){let r=n.trim();if(r==="")return n;let c=Number(r);if(Number.isFinite(c))return c}return n},p.number({...e,message:a}))},string(e){let a=e?.message??(n=>`Cannot cast "${n}" to string. Expected a string, number, or boolean.`);return p.preprocess(n=>y(n)||b(n)&&Number.isFinite(n)?String(n):n,p.string({...e,message:a}))},date(e){let a=e?.message??(n=>`Cannot cast "${n}" to a valid date.`);return p.preprocess(n=>{let r;return l(n)&&(r=n.trim()),b(n)&&Number.isFinite(n)||r?new Date(r??n):n},p.date({...e,message:a}))},json(e,a){let n=a?.message??(r=>`Cannot parse "${r}" as JSON.`);return h(e,"_parse",(r,c)=>{if(l(c))try{c=JSON.parse(c);}catch{let s={message:typeof n=="function"?n(c):n};return {success:false,error:s,errors:[s]}}return r(c)}),e}}};function N(e){return a=>`The value "${a}" must be type of ${e} but is type of "${typeof a}".`}function h(e,a,n){let r=e[a];e[a]=(...c)=>n(r,...c);}function I(e,{type:a="any",name:n,message:r}={}){r=r||N(a);let c={name:n,message:r,type:a},s={_getName(){return n},_getType(){return a},_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=s._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 s._parse(t,u)},transform(t){return h(this,"_parse",(u,i,o)=>{let m=u(i,o);return m.success&&(m.data=t(m.data)),m}),this},optional(){return h(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 h(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 h(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 h(this,"_parse",(u,i,o)=>(i===void 0&&(i=typeof t=="function"?t():t),u(i,o))),this},catch(t){return h(this,"_parse",(u,i,o)=>{let m=u(i,o);return m.success?m:{success:true,data:typeof t=="function"?t({input:i,error:m.error}):t}}),this},pipe(t){return h(this,"_parse",(u,i,o)=>{let m=u(i,o);return m.success?t._parse(m.data,o):m}),this},refine(t,{message:u,type:i}={}){return i&&(u=u||N(i),a=i),h(this,"_parse",(o,m,f)=>{let S=o(m,f);O(f);if(!S.success)return S;let d=t(S.data);if(d===true||typeof d=="object"&&d?.success===true)return S;let k={message:typeof u=="function"?u(m):u};return {success:false,error:k,errors:[k]}}),this}};return T.length>0?T.reduce((t,u)=>u(t,e,c)??t,s):s}var T=[];function P(e){if(typeof e!="function")throw new TypeError("extend() requires a function argument");T.push(e);}P((e,a,n)=>{if(n?.type==="number"){let r=e;r.min=function(c,{message:s}={}){return this.refine(t=>t>=c,{message:s||`Number must be greater than or equal to ${c}.`})},r.max=function(c,{message:s}={}){return this.refine(t=>t<=c,{message:s||`Number must be less than or equal to ${c}.`})},r.positive=function({message:c}={}){return this.refine(s=>s>0,{message:c||"Number must be positive."})},r.negative=function({message:c}={}){return this.refine(s=>s<0,{message:c||"Number must be negative."})},r.int=function({message:c}={}){return this.refine(s=>Number.isInteger(s),{message:c||"Number must be an integer."})},r.float=function({message:c}={}){return this.refine(s=>Number.isFinite(s)&&!Number.isInteger(s),{message:c||"Number must be a floating point (non-integer)."})},r.multipleOf=function(c,{message:s}={}){return this.refine(t=>t%c===0,{message:s||`Number must be a multiple of ${c}.`})},r.finite=function({message:c}={}){return this.refine(s=>Number.isFinite(s),{message:c||"Number must be finite."})};}return e});exports.extend=P;exports.hookOriginal=h;exports.s=p;exports.schema=p;
|
package/dist/number.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s } from './index.mjs';
|
|
1
|
+
export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, FunctionSchemaInterface, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s, s as schema } from './index.mjs';
|
|
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, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s } from './index.js';
|
|
1
|
+
export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, FunctionSchemaInterface, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s, s as schema } from './index.js';
|
|
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-CLEFU3BA.mjs';export{c as extend,b as hookOriginal,a as s,a as schema}from'./chunk-QQOSS5DZ.mjs';
|
package/dist/string.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';var
|
|
1
|
+
'use strict';var E={abortEarly:true};function b(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 k(e,s,t){if(e.errors?.length)for(let r=0;r<e.errors.length;r++)s.push(b(e.errors[r],t));}function O(e){return {...E,...e}}var l=e=>typeof e=="string"||e instanceof String,d=e=>(typeof e=="number"||e instanceof Number)&&!Number.isNaN(e),y=e=>e===true||e===false,_=e=>e instanceof Date&&!Number.isNaN(e.getTime()),D=e=>typeof e=="function",N=e=>Array.isArray(e),x=e=>typeof e=="object"&&e!==null&&!Array.isArray(e),m={object(e,s){let t=S(x,{...s,type:"object"});return t._getDescription=()=>`object({ ${Object.entries(e).map(([a,c])=>`${a}: ${c._getDescription()}`).join(", ")} })`,h(t,"_parse",(r,a,c)=>{let n=r(a,c),{abortEarly:o}=O(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 I=b(p.error,f);return {success:false,error:I,errors:[I]}}k(p,u,f);}}return u.length>0?{success:false,error:u[0],errors:u}:{success:true,data:i}}),t},string(e){return S(l,{...e,type:"string"})},number(e){return S(d,{...e,type:"number"})},boolean(e){return S(y,{...e,type:"boolean"})},date(e){return S(_,{...e,type:"date"})},function(e){return S(D,{...e,type:"function"})},enum(e,s){let t=n=>e.includes(n),r=n=>`Invalid ${a} value. Expected ${e.map(o=>`"${o}"`).join(" | ")}, received "${n}".`,a="enum",c=S(t,{message:r,...s,type:a});return c._getDescription=()=>`enum(${e.map(n=>`"${n}"`).join(" | ")})`,c},array(e,s){let t=S(N,{...s,type:"array"});return t._getDescription=()=>`array(${e._getDescription()})`,h(t,"_parse",(r,a,c)=>{let n=r(a,c),{abortEarly:o}=O(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 I=b(p.error,f);return {success:false,error:I,errors:[I]}}k(p,u,f);}}return u.length>0?{success:false,error:u[0],errors:u}:{success:true,data:i}}),t},any(){return S(()=>true)},preprocess(e,s){return h(s,"_parse",(t,r)=>(r=e(r),t(r))),s},union(e,s){return S(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: ${x(c)?JSON.stringify(c):`"${c}"`}`,...s,type:"union"})},literal(e,s){let a=S(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},coerce:{string(e){return m.preprocess(s=>String(s),m.string(e))},number(e){let s=e?.message??(t=>`Cannot coerce "${t}" to a valid number.`);return m.preprocess(t=>Number(t),m.number({...e,message:s}))},boolean(e){return m.preprocess(s=>!!s,m.boolean(e))},date(e){let s=e?.message??(t=>`Cannot coerce "${t}" to a valid date.`);return m.preprocess(t=>new Date(t),m.date({...e,message:s}))}},cast:{boolean(e){let s=e?.message??(t=>`Cannot cast "${t}" to boolean. Accepted: true/false, 1/0, yes/no, on/off.`);return m.preprocess(t=>{let r;return l(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},m.boolean({...e,message:s}))},number(e){let s=e?.message??(t=>`Cannot cast "${t}" to a number. Expected a numeric string or number.`);return m.preprocess(t=>{if(y(t))return Number(t);if(l(t)){let r=t.trim();if(r==="")return t;let a=Number(r);if(Number.isFinite(a))return a}return t},m.number({...e,message:s}))},string(e){let s=e?.message??(t=>`Cannot cast "${t}" to string. Expected a string, number, or boolean.`);return m.preprocess(t=>y(t)||d(t)&&Number.isFinite(t)?String(t):t,m.string({...e,message:s}))},date(e){let s=e?.message??(t=>`Cannot cast "${t}" to a valid date.`);return m.preprocess(t=>{let r;return l(t)&&(r=t.trim()),d(t)&&Number.isFinite(t)||r?new Date(r??t):t},m.date({...e,message:s}))},json(e,s){let t=s?.message??(r=>`Cannot parse "${r}" as JSON.`);return h(e,"_parse",(r,a)=>{if(l(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}}};function P(e){return s=>`The value "${s}" must be type of ${e} but is type of "${typeof s}".`}function h(e,s,t){let r=e[s];e[s]=(...a)=>t(r,...a);}function S(e,{type:s="any",name:t,message:r}={}){r=r||P(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 h(this,"_parse",(o,i,u)=>{let f=o(i,u);return f.success&&(f.data=n(f.data)),f}),this},optional(){return h(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 h(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 h(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 h(this,"_parse",(o,i,u)=>(i===void 0&&(i=typeof n=="function"?n():n),o(i,u))),this},catch(n){return h(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 h(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||P(i),s=i),h(this,"_parse",(u,f,p)=>{let I=u(f,p);O(p);if(!I.success)return I;let g=n(I.data);if(g===true||typeof g=="object"&&g?.success===true)return I;let T={message:typeof o=="function"?o(f):o};return {success:false,error:T,errors:[T]}}),this}};return w.length>0?w.reduce((n,o)=>o(n,e,a)??n,c):c}var w=[];function $(e){if(typeof e!="function")throw new TypeError("extend() requires a function argument");w.push(e);}$((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.extend=$;exports.hookOriginal=h;exports.s=m;exports.schema=m;
|
package/dist/string.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s } from './index.mjs';
|
|
1
|
+
export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, FunctionSchemaInterface, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s, s as schema } from './index.mjs';
|
|
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, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s } from './index.js';
|
|
1
|
+
export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, FunctionSchemaInterface, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s, s as schema } from './index.js';
|
|
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-YUL3WJPU.mjs';export{c as extend,b as hookOriginal,a as s,a as schema}from'./chunk-QQOSS5DZ.mjs';
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@esmj/schema",
|
|
3
|
-
"version": "0.7.
|
|
3
|
+
"version": "0.7.3",
|
|
4
4
|
"description": "Tiny extendable package for schema validation.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"schema",
|
|
@@ -113,7 +113,7 @@
|
|
|
113
113
|
"joi": "^17.13.3",
|
|
114
114
|
"lint-staged": "^16.1.6",
|
|
115
115
|
"superstruct": "^2.0.2",
|
|
116
|
-
"tsup": "^8.5.
|
|
116
|
+
"tsup": "^8.5.1",
|
|
117
117
|
"yup": "^1.7.0",
|
|
118
118
|
"zod": "^3.25.42"
|
|
119
119
|
}
|
package/dist/chunk-2NCXW7ID.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
var k={abortEarly:true};function d(e,s){if(!s)return e;let i=e?.cause?.key?`${s}.${e.cause.key}`:`${s}`;return {message:`Error parsing key "${i}": ${e.message}`,cause:{key:i}}}function T(e,s,i){if(e.errors?.length)for(let o=0;o<e.errors.length;o++)s.push(d(e.errors[o],i));}function y(e){return {...k,...e}}var x=e=>typeof e=="string"||e instanceof String,w=e=>(typeof e=="number"||e instanceof Number)&&!Number.isNaN(e),P=e=>e===true||e===false,E=e=>e instanceof Date&&!Number.isNaN(e.getTime()),_=e=>Array.isArray(e),b=e=>typeof e=="object"&&e!==null&&!Array.isArray(e),$={object(e,s){let i=h(b,{...s,type:"object"});return i._getDescription=()=>`object({ ${Object.entries(e).map(([p,c])=>`${p}: ${c._getDescription()}`).join(", ")} })`,m(i,"_parse",(o,p,c)=>{let t=o(p,c),{abortEarly:n}=y(c);if(t.success===false)return t;let a={},r=[];for(let u in e){let f=e[u]._parse(t.data[u],c);if(f.success)a[u]=f.data;else {if(f=f,n!==false){let l=d(f.error,u);return {success:false,error:l,errors:[l]}}T(f,r,u);}}return r.length>0?{success:false,error:r[0],errors:r}:{success:true,data:a}}),i},string(e){return h(x,{...e,type:"string"})},number(e){return h(w,{...e,type:"number"})},boolean(e){return h(P,{...e,type:"boolean"})},date(e){return h(E,{...e,type:"date"})},enum(e,s){let i=t=>e.includes(t),o=t=>`Invalid ${p} value. Expected ${e.map(n=>`"${n}"`).join(" | ")}, received "${t}".`,p="enum",c=h(i,{message:o,...s,type:p});return c._getDescription=()=>`enum(${e.map(t=>`"${t}"`).join(" | ")})`,c},array(e,s){let i=h(_,{...s,type:"array"});return i._getDescription=()=>`array(${e._getDescription()})`,m(i,"_parse",(o,p,c)=>{let t=o(p,c),{abortEarly:n}=y(c);if(t.success===false)return t;let a=[],r=[];for(let u=0;u<t.data.length;u++){let f=e._parse(t.data[u],c);if(f.success)a.push(f.data);else {if(f=f,n!==false){let l=d(f.error,u);return {success:false,error:l,errors:[l]}}T(f,r,u);}}return r.length>0?{success:false,error:r[0],errors:r}:{success:true,data:a}}),i},any(){return h(()=>true)},preprocess(e,s){return m(s,"_parse",(i,o)=>(o=e(o),i(o))),s},union(e,s){return h(c=>{for(let t=0;t<e.length;t++){let n=e[t]._parse(c);if(n.success)return n}return false},{message:c=>`Invalid union value. Expected the value to match one of the schemas:${e.map((t,n)=>` ${n+1}. ${t._getDescription()}`).join(",")} but received "${typeof c}" with value: ${b(c)?JSON.stringify(c):`"${c}"`}`,...s,type:"union"})},literal(e,s){let p=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 p._getDescription=()=>`literal("${e}")`,p}};function O(e){return s=>`The value "${s}" must be type of ${e} but is type of "${typeof s}".`}function m(e,s,i){let o=e[s];e[s]=(...p)=>i(o,...p);}function h(e,{type:s="any",name:i,message:o}={}){o=o||O(s);let p={name:i,message:o,type:s},c={_getName(){return i},_getType(){return s},_getDescription(){return this._getName()??this._getType()},_parse(t,n){let a=e(t);if(a===true)return {success:true,data:t};if(typeof a=="object"&&a?.success===true)return a;let r={message:typeof o=="function"?o(t):o};return {success:false,error:r,errors:[r]}},parse(t,n){let a=c._parse(t,n);if(!a.success)throw a=a,new Error(a.error.message,{cause:a.error.cause});return a.data},safeParse(t,n){return c._parse(t,n)},transform(t){return m(this,"_parse",(n,a,r)=>{let u=n(a,r);return u.success&&(u.data=t(u.data)),u}),this},optional(){return m(this,"_parse",(t,n,a)=>{let r=t(n,a);return !r.success&&n===void 0&&(r.data=void 0,r.success=true),r}),this},nullable(){return m(this,"_parse",(t,n,a)=>{let r=t(n,a);return !r.success&&n===null&&(r.data=null,r.success=true),r}),this},nullish(){return m(this,"_parse",(t,n,a)=>{let r=t(n,a);return !r.success&&n==null&&(r.success=true,r.data=n),r}),this},default(t){return m(this,"_parse",(n,a,r)=>(a===void 0&&(a=typeof t=="function"?t():t),n(a,r))),this},catch(t){return m(this,"_parse",(n,a,r)=>{let u=n(a,r);return u.success?u:{success:true,data:typeof t=="function"?t({input:a,error:u.error}):t}}),this},pipe(t){return m(this,"_parse",(n,a,r)=>{let u=n(a,r);return u.success?t._parse(u.data,r):u}),this},refine(t,{message:n,type:a}={}){return a&&(n=n||O(a),s=a),m(this,"_parse",(r,u,f)=>{let l=r(u,f);y(f);if(!l.success)return l;let I=t(l.data);if(I===true||typeof I=="object"&&I?.success===true)return l;let g={message:typeof n=="function"?n(u):n};return {success:false,error:g,errors:[g]}}),this}};return S.length>0?S.reduce((t,n)=>n(t,e,p)??t,c):c}var S=[];function A(e){if(typeof e!="function")throw new TypeError("extend() requires a function argument");S.push(e);}export{$ as a,m as b,A as c};
|
package/dist/chunk-4JJPVRF6.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import {c}from'./chunk-2NCXW7ID.mjs';c((a,o,m)=>{if(m?.type==="array"){let r=a;r.min=function(e,{message:t}={}){return this.refine(n=>n.length>=e,{message:t||`Array must contain at least ${e} items.`})},r.max=function(e,{message:t}={}){return this.refine(n=>n.length<=e,{message:t||`Array must contain at most ${e} items.`})},r.length=function(e,{message:t}={}){return this.refine(n=>n.length===e,{message:t||`Array must contain exactly ${e} items.`})},r.nonEmpty=function({message:e}={}){return this.refine(t=>t.length>0,{message:e||"Array must not be empty."})},r.unique=function({message:e}={}){return this.refine(t=>{let n=new Set;try{return t.every(c=>{let s=JSON.stringify(c);return n.has(s)?!1:(n.add(s),!0)})}catch{return new Set(t).size===t.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 a});
|
package/dist/chunk-LZRUIXQL.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import {c}from'./chunk-2NCXW7ID.mjs';c((i,s,c)=>{if(c?.type==="string"){let r=i;r.min=function(t,{message:n}={}){return this.refine(e=>e.length>=t,{message:n||(e=>`String must be at least ${t} characters long (received ${e.length} characters: "${e}")`)})},r.max=function(t,{message:n}={}){return this.refine(e=>e.length<=t,{message:n||(e=>`String must be at most ${t} characters long (received ${e.length} characters: "${e}")`)})},r.length=function(t,{message:n}={}){return this.refine(e=>e.length===t,{message:n||(e=>`String must be exactly ${t} characters long (received ${e.length} characters: "${e}")`)})},r.nonEmpty=function({message:t}={}){return this.refine(n=>n.length>0,{message:t||"String must not be empty (received empty string)"})},r.startsWith=function(t,{message:n}={}){return this.refine(e=>e.startsWith(t),{message:n||(e=>`String must start with "${t}" (received: "${e}")`)})},r.endsWith=function(t,{message:n}={}){return this.refine(e=>e.endsWith(t),{message:n||(e=>`String must end with "${t}" (received: "${e}")`)})},r.includes=function(t,{message:n}={}){return this.refine(e=>e.includes(t),{message:n||(e=>`String must include "${t}" (received: "${e}")`)})},r.toLowerCase=function(){return this.transform(t=>t.toLowerCase())},r.toUpperCase=function(){return this.transform(t=>t.toUpperCase())},r.trim=function(){return this.transform(t=>t.trim())},r.padStart=function(t,n=" "){return this.transform(e=>e.padStart(t,n))},r.padEnd=function(t,n=" "){return this.transform(e=>e.padEnd(t,n))},r.replace=function(t,n){return this.transform(e=>e.replace(t,n))};}return i});
|
package/dist/chunk-PH4B25KI.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
import {c}from'./chunk-2NCXW7ID.mjs';c((i,c,m)=>{if(m?.type==="number"){let n=i;n.min=function(e,{message:t}={}){return this.refine(r=>r>=e,{message:t||`Number must be greater than or equal to ${e}.`})},n.max=function(e,{message:t}={}){return this.refine(r=>r<=e,{message:t||`Number must be less than or equal to ${e}.`})},n.positive=function({message:e}={}){return this.refine(t=>t>0,{message:e||"Number must be positive."})},n.negative=function({message:e}={}){return this.refine(t=>t<0,{message:e||"Number must be negative."})},n.int=function({message:e}={}){return this.refine(t=>Number.isInteger(t),{message:e||"Number must be an integer."})},n.float=function({message:e}={}){return this.refine(t=>Number.isFinite(t)&&!Number.isInteger(t),{message:e||"Number must be a floating point (non-integer)."})},n.multipleOf=function(e,{message:t}={}){return this.refine(r=>r%e===0,{message:t||`Number must be a multiple of ${e}.`})},n.finite=function({message:e}={}){return this.refine(t=>Number.isFinite(t),{message:e||"Number must be finite."})};}return i});
|