@esmj/schema 0.7.3 → 0.7.4
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 +78 -21
- package/dist/array.cjs +1 -1
- package/dist/array.d.mts +1 -1
- package/dist/array.d.ts +1 -1
- package/dist/array.mjs +1 -1
- package/dist/chunk-37YJYCTR.mjs +1 -0
- package/dist/{chunk-YYPP27RX.mjs → chunk-5XWCCG75.mjs} +1 -1
- package/dist/{chunk-CLEFU3BA.mjs → chunk-NCRS6NBV.mjs} +1 -1
- package/dist/{chunk-YUL3WJPU.mjs → chunk-NRGPACGD.mjs} +1 -1
- 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 +37 -262
- package/dist/index.d.ts +37 -262
- 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 +15 -5
- package/dist/chunk-QQOSS5DZ.mjs +0 -1
package/README.md
CHANGED
|
@@ -15,6 +15,7 @@ This small library provides a simple schema validation system for JavaScript/Typ
|
|
|
15
15
|
- [Number Extensions](#number-extensions-esmjschemanumber)
|
|
16
16
|
- [Array Extensions](#array-extensions-esmjschemaarray)
|
|
17
17
|
- [Full Extensions](#full-extensions-esmjschemafull)
|
|
18
|
+
- [Named Exports & Tree-Shaking](#named-exports--tree-shaking)
|
|
18
19
|
- [API Reference Summary](#api-reference-summary)
|
|
19
20
|
- [Schema Types](#schema-types)
|
|
20
21
|
- [s.coerce](#scoerce)
|
|
@@ -107,11 +108,13 @@ const result = schema.parse({
|
|
|
107
108
|
|
|
108
109
|
### Performance Highlights
|
|
109
110
|
|
|
110
|
-
- **Schema Creation**: Create schemas at up to 4
|
|
111
|
-
- **Parsing**: Parse data at up to
|
|
112
|
-
- **Error Handling**:
|
|
111
|
+
- **Schema Creation**: Create schemas at up to 4,370,618 ops/s (0.23 μs latency) with @sinclair/typebox, or 736,810 ops/s (1.36 μs latency) with @esmj/schema. Superstruct is also competitive for schema creation performance.
|
|
112
|
+
- **Parsing**: Parse data at up to **33,620,146 ops/s** (**0.03 μs** latency) with **AJV** (best result in this benchmark). For comparison, `@zod/mini` reached **4,627,714 ops/s** (**0.22 μs**, observed at 200% CPU), and `@esmj/schema` reached **3,142,587 ops/s** (**0.32 μs**), with ArkType and effect/Schema also performing strongly.
|
|
113
|
+
- **Error Handling**: Handle validation errors at up to **19,693,821 ops/s** (**0.05 μs** latency) with **AJV** (best result in this benchmark), while `@esmj/schema` delivered **2,428,049 ops/s** (**0.41 μs**) with a developer-friendly API.
|
|
113
114
|
|
|
114
|
-
|
|
115
|
+
Across the benchmark tables, `@esmj/schema` shows strong all-around results: fast schema creation, very competitive parsing and error-handling throughput, and a very small bundle size (`~1.6 KB` core).
|
|
116
|
+
|
|
117
|
+
For most TypeScript applications, it offers a practical balance of performance, size, and developer ergonomics. If absolute peak throughput in a single category is the only goal, some specialized options (for example, AJV or TypeBox in specific tests) can be faster.
|
|
115
118
|
|
|
116
119
|
## Comparison with Similar Libraries
|
|
117
120
|
|
|
@@ -125,6 +128,7 @@ When choosing a schema validation library, bundle size can be an important facto
|
|
|
125
128
|
| Yup | ~12.2 KB |
|
|
126
129
|
| Zod@3 | ~13 KB |
|
|
127
130
|
| @zod/mini | ~20.5 KB |
|
|
131
|
+
| AJV | ~31.4 KB |
|
|
128
132
|
| Joi | ~40.4 KB |
|
|
129
133
|
| Zod@4 | ~40.8 KB |
|
|
130
134
|
| ArkType | ~41.8 KB |
|
|
@@ -147,6 +151,7 @@ When choosing a schema validation library, bundle size can be an important facto
|
|
|
147
151
|
| `@sinclair/typebox` | `4 370 618.49 ± 1.23%` | `0.23 ± 1.23%` |
|
|
148
152
|
| ArkType | 16 282.69 ± 4.14% | 61.61 ± 4.38% |
|
|
149
153
|
| effect/Schema | 24 919.15 ± 4.31% | 40.31 ± 4.78% |
|
|
154
|
+
| AJV | 240.32 ± 4.67% | 4 164.31 ± 2.04% |
|
|
150
155
|
|
|
151
156
|
#### Parsing Performance
|
|
152
157
|
|
|
@@ -154,19 +159,20 @@ When choosing a schema validation library, bundle size can be an important facto
|
|
|
154
159
|
|-------------------|-------------------------------:|-------------------------:|
|
|
155
160
|
| @esmj/schema | 3 142 587.31 ± 0.97% | 0.32 ± 0.99% |
|
|
156
161
|
| zod@3 | 1 018 777.24 ± 0.64% | 0.98 ± 0.65% |
|
|
157
|
-
|
|
|
162
|
+
| @zod/mini. | 4 627 714.90 ± 2.23% | 0.22 ± 2.36% |
|
|
158
163
|
| Yup | 108 361.49 ± 0.50% | 9.23 ± 0.51% |
|
|
159
164
|
| Superstruct | 252 904.42 ± 2.20% | 3.96 ± 2.44% |
|
|
160
165
|
| Joi | 346 094.49 ± 0.65% | 2.89 ± 0.65% |
|
|
161
166
|
| @sinclair/typebox | 228 711.62 ± 2.03% | 4.38 ± 2.23% |
|
|
162
167
|
| ArkType | 1 677 066.00 ± 0.58% | 0.60 ± 0.59% |
|
|
163
168
|
| effect/Schema | 1 060 056.14 ± 0.61% | 0.94 ± 0.61% |
|
|
169
|
+
| `AJV` | `33 620 146.24 ± 1.19%` | `0.03 ± 1.26%` |
|
|
164
170
|
|
|
165
171
|
#### Error Handling Performance
|
|
166
172
|
|
|
167
173
|
| Library | Throughput average (ops/s) | Latency average (μs) |
|
|
168
174
|
|-------------------|-------------------------------:|-------------------------:|
|
|
169
|
-
|
|
|
175
|
+
| @esmj/schema | 2 428 049.34 ± 0.54% | 0.41 ± 0.53% |
|
|
170
176
|
| zod@3 | 641 504.22 ± 3.67% | 1.57 ± 4.38% |
|
|
171
177
|
| @zod/mini | 1 386 616.61 ± 0.60% | 0.72 ± 0.60% |
|
|
172
178
|
| Yup | 98 904.30 ± 0.61% | 10.11 ± 0.61% |
|
|
@@ -175,6 +181,7 @@ When choosing a schema validation library, bundle size can be an important facto
|
|
|
175
181
|
| @sinclair/typebox | 228 734.49 ± 0.55% | 4.37 ± 0.56% |
|
|
176
182
|
| ArkType | 258 685.33 ± 1.23% | 3.87 ± 1.23% |
|
|
177
183
|
| effect/Schema | 165 753.69 ± 0.99% | 6.03 ± 1.00% |
|
|
184
|
+
| `AJV` | `19 693 821.79 ± 1.81%` | `0.05 ± 1.83%` |
|
|
178
185
|
|
|
179
186
|
**Note:** During the performance tests, `@zod/mini` was observed to consume 200% CPU, while other libraries used only 100% CPU. This may affect the interpretation of the results, especially in multi-threaded environments.
|
|
180
187
|
|
|
@@ -250,6 +257,13 @@ import { s } from '@esmj/schema/array';
|
|
|
250
257
|
import '@esmj/schema/string';
|
|
251
258
|
import '@esmj/schema/number';
|
|
252
259
|
import { s } from '@esmj/schema';
|
|
260
|
+
|
|
261
|
+
// Tree-shakeable named exports — bundle only what you use
|
|
262
|
+
import { string, number, object, array } from '@esmj/schema';
|
|
263
|
+
import { coerce, cast } from '@esmj/schema';
|
|
264
|
+
|
|
265
|
+
// Named exports for reserved-word factories
|
|
266
|
+
import { functionSchema, enumSchema } from '@esmj/schema';
|
|
253
267
|
```
|
|
254
268
|
|
|
255
269
|
### Bundle Size Impact
|
|
@@ -262,7 +276,48 @@ import { s } from '@esmj/schema';
|
|
|
262
276
|
|
|
263
277
|
**Recommendation:** Import only the extensions you need to minimize bundle size.
|
|
264
278
|
|
|
265
|
-
###
|
|
279
|
+
### Named Exports & Tree-Shaking
|
|
280
|
+
|
|
281
|
+
Every factory in the core module is exported both as a named function **and** as a property on `s`. The two references are identical — no extra wrapper, no overhead.
|
|
282
|
+
|
|
283
|
+
```typescript
|
|
284
|
+
import { s } from '@esmj/schema';
|
|
285
|
+
import { string, number } from '@esmj/schema';
|
|
286
|
+
|
|
287
|
+
s.string === string; // true
|
|
288
|
+
```
|
|
289
|
+
|
|
290
|
+
When you import individual factories, bundlers (webpack, Rollup, Vite, esbuild) tree-shake everything else out:
|
|
291
|
+
|
|
292
|
+
```typescript
|
|
293
|
+
// Only `string` and `number` end up in the final bundle — object, array, coerce, cast, etc. are excluded
|
|
294
|
+
import { string, number } from '@esmj/schema';
|
|
295
|
+
|
|
296
|
+
const nameSchema = string().optional();
|
|
297
|
+
const ageSchema = number();
|
|
298
|
+
```
|
|
299
|
+
|
|
300
|
+
The `coerce` and `cast` namespaces are also individually exported:
|
|
301
|
+
|
|
302
|
+
```typescript
|
|
303
|
+
import { coerce } from '@esmj/schema';
|
|
304
|
+
|
|
305
|
+
const schema = coerce.number();
|
|
306
|
+
schema.parse('42'); // 42
|
|
307
|
+
```
|
|
308
|
+
|
|
309
|
+
Because `function` and `enum` are reserved words in JavaScript, their standalone names are `functionSchema` and `enumSchema`:
|
|
310
|
+
|
|
311
|
+
```typescript
|
|
312
|
+
import { functionSchema, enumSchema } from '@esmj/schema';
|
|
313
|
+
|
|
314
|
+
const cb = functionSchema();
|
|
315
|
+
const role = enumSchema(['admin', 'user', 'guest']);
|
|
316
|
+
```
|
|
317
|
+
|
|
318
|
+
They are still accessible as `s.function()` and `s.enum()` for full API compatibility.
|
|
319
|
+
|
|
320
|
+
|
|
266
321
|
|
|
267
322
|
String extensions provide common validation and transformation methods for string schemas.
|
|
268
323
|
|
|
@@ -422,20 +477,22 @@ const schema = s.object({
|
|
|
422
477
|
|
|
423
478
|
### Core Types
|
|
424
479
|
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
- `s.
|
|
428
|
-
- `s.
|
|
429
|
-
- `s.
|
|
430
|
-
- `s.
|
|
431
|
-
- `s.
|
|
432
|
-
- `s.
|
|
433
|
-
- `s.
|
|
434
|
-
- `s.
|
|
435
|
-
- `s.
|
|
436
|
-
- `s.
|
|
437
|
-
- `s.
|
|
438
|
-
- `s.
|
|
480
|
+
All factory functions below are available both as methods on `s` **and** as individual named exports for tree-shaking:
|
|
481
|
+
|
|
482
|
+
- `s.string()` / `import { string }` - String validation
|
|
483
|
+
- `s.number()` / `import { number }` - Number validation
|
|
484
|
+
- `s.boolean()` / `import { boolean }` - Boolean validation
|
|
485
|
+
- `s.date()` / `import { date }` - Date validation
|
|
486
|
+
- `s.function()` / `import { functionSchema }` - Function validation
|
|
487
|
+
- `s.object(def)` / `import { object }` - Object validation
|
|
488
|
+
- `s.array(def)` / `import { array }` - Array validation
|
|
489
|
+
- `s.literal(value)` / `import { literal }` - Literal value validation
|
|
490
|
+
- `s.enum(values)` / `import { enumSchema }` - Enum validation
|
|
491
|
+
- `s.union(schemas)` / `import { union }` - Union validation
|
|
492
|
+
- `s.any()` / `import { any }` - Any type
|
|
493
|
+
- `s.preprocess(fn, schema)` / `import { preprocess }` - Preprocess before validation
|
|
494
|
+
- `s.coerce` / `import { coerce }` - Coerce namespace
|
|
495
|
+
- `s.cast` / `import { cast }` - Cast namespace
|
|
439
496
|
|
|
440
497
|
### Modifiers
|
|
441
498
|
|
package/dist/array.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';var
|
|
1
|
+
'use strict';var N={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 $(e,a,n){if(e.errors?.length)for(let r=0;r<e.errors.length;r++)a.push(b(e.errors[r],n));}function O(e){return e?.abortEarly!==void 0?e:N}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,R=e=>e instanceof Date&&!Number.isNaN(e.getTime()),j=e=>typeof e=="function",F=e=>Array.isArray(e),_=e=>typeof e=="object"&&e!==null&&!Array.isArray(e);function C(e,a){let n=h(_,{...a,type:"object"});return n._getDescription=()=>`object({ ${Object.entries(e).map(([u,s])=>`${u}: ${s._getDescription()}`).join(", ")} })`,m(n,"_parse",(r,u,s)=>{let t=r(u,s),{abortEarly:c}=O(s);if(t.success===false)return t;let o={},i=[];for(let f in e){let p=e[f]._parse(t.data[f],s);if(p.success)o[f]=p.data;else {if(p=p,c!==false){let S=b(p.error,f);return {success:false,error:S,errors:[S]}}$(p,i,f);}}return i.length>0?{success:false,error:i[0],errors:i}:{success:true,data:o}}),n}function k(e){return h(y,{...e,type:"string"})}function w(e){return h(d,{...e,type:"number"})}function x(e){return h(g,{...e,type:"boolean"})}function A(e){return h(R,{...e,type:"date"})}function V(e){return h(j,{...e,type:"function"})}function B(e,a){let n=t=>e.includes(t),r=t=>`Invalid ${u} value. Expected ${e.map(c=>`"${c}"`).join(" | ")}, received "${t}".`,u="enum",s=h(n,{message:r,...a,type:u});return s._getDescription=()=>`enum(${e.map(t=>`"${t}"`).join(" | ")})`,s}function L(e,a){let n=h(F,{...a,type:"array"});return n._getDescription=()=>`array(${e._getDescription()})`,m(n,"_parse",(r,u,s)=>{let t=r(u,s),{abortEarly:c}=O(s);if(t.success===false)return t;let o=[],i=[];for(let f=0;f<t.data.length;f++){let p=e._parse(t.data[f],s);if(p.success)o.push(p.data);else {if(p=p,c!==false){let S=b(p.error,f);return {success:false,error:S,errors:[S]}}$(p,i,f);}}return i.length>0?{success:false,error:i[0],errors:i}:{success:true,data:o}}),n}function M(){return h(()=>true)}function I(e,a){return m(a,"_parse",(n,r)=>(r=e(r),n(r))),a}function q(e,a){return h(s=>{for(let t=0;t<e.length;t++){let c=e[t]._parse(s);if(c.success)return c}return false},{message:s=>`Invalid union value. Expected the value to match one of the schemas:${e.map((t,c)=>` ${c+1}. ${t._getDescription()}`).join(",")} but received "${typeof s}" with value: ${_(s)?JSON.stringify(s):`"${s}"`}`,...a,type:"union"})}function J(e,a){let u=h(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 u._getDescription=()=>`literal("${e}")`,u}var U={string(e){return I(a=>String(a),k(e))},number(e){let a=e?.message??(n=>`Cannot coerce "${n}" to a valid number.`);return I(n=>Number(n),w({...e,message:a}))},boolean(e){return I(a=>!!a,x(e))},date(e){let a=e?.message??(n=>`Cannot coerce "${n}" to a valid date.`);return I(n=>new Date(n),A({...e,message:a}))}},v={boolean(e){let a=e?.message??(n=>`Cannot cast "${n}" to boolean. Accepted: true/false, 1/0, yes/no, on/off.`);return I(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},x({...e,message:a}))},number(e){let a=e?.message??(n=>`Cannot cast "${n}" to a number. Expected a numeric string or number.`);return I(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},w({...e,message:a}))},string(e){let a=e?.message??(n=>`Cannot cast "${n}" to string. Expected a string, number, or boolean.`);return I(n=>g(n)||d(n)&&Number.isFinite(n)?String(n):n,k({...e,message:a}))},date(e){let a=e?.message??(n=>`Cannot cast "${n}" to a valid date.`);return I(n=>{let r;return y(n)&&(r=n.trim()),d(n)&&Number.isFinite(n)||r?new Date(r??n):n},A({...e,message:a}))},json(e,a){let n=a?.message??(r=>`Cannot parse "${r}" as JSON.`);return m(e,"_parse",(r,u)=>{if(y(u))try{u=JSON.parse(u);}catch{let s={message:typeof n=="function"?n(u):n};return {success:false,error:s,errors:[s]}}return r(u)}),e}},H={object:C,string:k,number:w,boolean:x,date:A,function:V,enum:B,array:L,any:M,preprocess:I,union:q,literal:J,coerce:U,cast:v};function E(e){return a=>`The value "${a}" must be type of ${e} but is type of "${typeof a}".`}function m(e,a,n){let r=e[a];e[a]=(...u)=>n(r,...u);}function h(e,{type:a="any",name:n,message:r}={}){r=r||E(a);let u={name:n,message:r,type:a},s={_getName(){return n},_getType(){return a},_getDescription(){return this._getName()??this._getType()},_parse(t,c){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,c){let o=s._parse(t,c);if(!o.success)throw o=o,new Error(o.error.message,{cause:o.error.cause});return o.data},safeParse(t,c){return s._parse(t,c)},transform(t){return m(this,"_parse",(c,o,i)=>{let f=c(o,i);return f.success&&(f.data=t(f.data)),f}),this},optional(){return m(this,"_parse",(t,c,o)=>{let i=t(c,o);return !i.success&&c===void 0&&(i.data=void 0,i.success=true),i}),this},nullable(){return m(this,"_parse",(t,c,o)=>{let i=t(c,o);return !i.success&&c===null&&(i.data=null,i.success=true),i}),this},nullish(){return m(this,"_parse",(t,c,o)=>{let i=t(c,o);return !i.success&&c==null&&(i.success=true,i.data=c),i}),this},default(t){return m(this,"_parse",(c,o,i)=>(o===void 0&&(o=typeof t=="function"?t():t),c(o,i))),this},catch(t){return m(this,"_parse",(c,o,i)=>{let f=c(o,i);return f.success?f:{success:true,data:typeof t=="function"?t({input:o,error:f.error}):t}}),this},pipe(t){return m(this,"_parse",(c,o,i)=>{let f=c(o,i);return f.success?t._parse(f.data,i):f}),this},refine(t,{message:c,type:o}={}){return o&&(c=c||E(o),a=o),m(this,"_parse",(i,f,p)=>{let S=i(f,p),{abortEarly:z}=O(p);if(!S.success)return S;let l=t(S.data);if(l===true||typeof l=="object"&&l?.success===true)return S;let P={message:typeof c=="function"?c(f):c};return {success:false,error:P,errors:[P]}}),this}};return T.length>0?T.reduce((t,c)=>c(t,e,u)??t,s):s}var T=[];function D(e){if(typeof e!="function")throw new TypeError("extend() requires a function argument");T.push(e);}D((e,a,n)=>{if(n?.type==="array"){let r=e;r.min=function(u,{message:s}={}){return this.refine(t=>t.length>=u,{message:s||`Array must contain at least ${u} items.`})},r.max=function(u,{message:s}={}){return this.refine(t=>t.length<=u,{message:s||`Array must contain at most ${u} items.`})},r.length=function(u,{message:s}={}){return this.refine(t=>t.length===u,{message:s||`Array must contain exactly ${u} items.`})},r.nonEmpty=function({message:u}={}){return this.refine(s=>s.length>0,{message:u||"Array must not be empty."})},r.unique=function({message:u}={}){return this.refine(s=>{let t=new Set;try{return s.every(c=>{let o=JSON.stringify(c);return t.has(o)?!1:(t.add(o),!0)})}catch{return new Set(s).size===s.length}},{message:u||"Array items must be unique."})},r.sort=function(){return this.transform(u=>[...u].sort())},r.reverse=function(){return this.transform(u=>[...u].reverse())};}return e});exports.any=M;exports.array=L;exports.boolean=x;exports.cast=v;exports.coerce=U;exports.date=A;exports.enumSchema=B;exports.extend=D;exports.functionSchema=V;exports.hookOriginal=m;exports.literal=J;exports.number=w;exports.object=C;exports.preprocess=I;exports.s=H;exports.schema=H;exports.string=k;exports.union=q;
|
package/dist/array.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, FunctionSchemaInterface, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s, s as schema } from './index.mjs';
|
|
1
|
+
export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, FunctionSchemaInterface, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, any, array, boolean, cast, coerce, date, enumSchema, extend, functionSchema, hookOriginal, literal, number, object, preprocess, s, s as schema, string, union } from './index.mjs';
|
|
2
2
|
|
|
3
3
|
declare module './index.ts' {
|
|
4
4
|
interface ArraySchemaInterface<T extends SchemaType> {
|
package/dist/array.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, FunctionSchemaInterface, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s, s as schema } from './index.js';
|
|
1
|
+
export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, FunctionSchemaInterface, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, any, array, boolean, cast, coerce, date, enumSchema, extend, functionSchema, hookOriginal, literal, number, object, preprocess, s, s as schema, string, union } from './index.js';
|
|
2
2
|
|
|
3
3
|
declare module './index.ts' {
|
|
4
4
|
interface ArraySchemaInterface<T extends SchemaType> {
|
package/dist/array.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import'./chunk-
|
|
1
|
+
import'./chunk-5XWCCG75.mjs';export{i as any,h as array,d as boolean,n as cast,m as coerce,e as date,g as enumSchema,q as extend,f as functionSchema,p as hookOriginal,l as literal,c as number,a as object,j as preprocess,o as s,o as schema,b as string,k as union}from'./chunk-37YJYCTR.mjs';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
var N={abortEarly:true};function T(e,t){if(!t)return e;let n=e?.cause?.key?`${t}.${e.cause.key}`:`${t}`;return {message:`Error parsing key "${n}": ${e.message}`,cause:{key:n}}}function _(e,t,n){if(e.errors?.length)for(let a=0;a<e.errors.length;a++)t.push(T(e.errors[a],n));}function O(e){return e?.abortEarly!==void 0?e:N}var S=e=>typeof e=="string"||e instanceof String,y=e=>(typeof e=="number"||e instanceof Number)&&!Number.isNaN(e),g=e=>e===true||e===false,R=e=>e instanceof Date&&!Number.isNaN(e.getTime()),A=e=>typeof e=="function",j=e=>Array.isArray(e),$=e=>typeof e=="object"&&e!==null&&!Array.isArray(e);function F(e,t){let n=h($,{...t,type:"object"});return n._getDescription=()=>`object({ ${Object.entries(e).map(([f,o])=>`${f}: ${o._getDescription()}`).join(", ")} })`,m(n,"_parse",(a,f,o)=>{let r=a(f,o),{abortEarly:c}=O(o);if(r.success===false)return r;let u={},s=[];for(let i in e){let p=e[i]._parse(r.data[i],o);if(p.success)u[i]=p.data;else {if(p=p,c!==false){let I=T(p.error,i);return {success:false,error:I,errors:[I]}}_(p,s,i);}}return s.length>0?{success:false,error:s[0],errors:s}:{success:true,data:u}}),n}function k(e){return h(S,{...e,type:"string"})}function w(e){return h(y,{...e,type:"number"})}function x(e){return h(g,{...e,type:"boolean"})}function P(e){return h(R,{...e,type:"date"})}function C(e){return h(A,{...e,type:"function"})}function V(e,t){let n=r=>e.includes(r),a=r=>`Invalid ${f} value. Expected ${e.map(c=>`"${c}"`).join(" | ")}, received "${r}".`,f="enum",o=h(n,{message:a,...t,type:f});return o._getDescription=()=>`enum(${e.map(r=>`"${r}"`).join(" | ")})`,o}function B(e,t){let n=h(j,{...t,type:"array"});return n._getDescription=()=>`array(${e._getDescription()})`,m(n,"_parse",(a,f,o)=>{let r=a(f,o),{abortEarly:c}=O(o);if(r.success===false)return r;let u=[],s=[];for(let i=0;i<r.data.length;i++){let p=e._parse(r.data[i],o);if(p.success)u.push(p.data);else {if(p=p,c!==false){let I=T(p.error,i);return {success:false,error:I,errors:[I]}}_(p,s,i);}}return s.length>0?{success:false,error:s[0],errors:s}:{success:true,data:u}}),n}function L(){return h(()=>true)}function l(e,t){return m(t,"_parse",(n,a)=>(a=e(a),n(a))),t}function M(e,t){return h(o=>{for(let r=0;r<e.length;r++){let c=e[r]._parse(o);if(c.success)return c}return false},{message:o=>`Invalid union value. Expected the value to match one of the schemas:${e.map((r,c)=>` ${c+1}. ${r._getDescription()}`).join(",")} but received "${typeof o}" with value: ${$(o)?JSON.stringify(o):`"${o}"`}`,...t,type:"union"})}function U(e,t){let f=h(o=>o===e,{message:o=>t?.message?typeof t.message=="function"?t.message(o):t.message:`Expected literal value "${e}", received "${o}"`,name:t?.name,type:"literal"});return f._getDescription=()=>`literal("${e}")`,f}var J={string(e){return l(t=>String(t),k(e))},number(e){let t=e?.message??(n=>`Cannot coerce "${n}" to a valid number.`);return l(n=>Number(n),w({...e,message:t}))},boolean(e){return l(t=>!!t,x(e))},date(e){let t=e?.message??(n=>`Cannot coerce "${n}" to a valid date.`);return l(n=>new Date(n),P({...e,message:t}))}},q={boolean(e){let t=e?.message??(n=>`Cannot cast "${n}" to boolean. Accepted: true/false, 1/0, yes/no, on/off.`);return l(n=>{let a;return S(n)&&(a=n.toLowerCase()),a==="true"||a==="yes"||a==="on"||a==="1"||n===1?true:a==="false"||a==="no"||a==="off"||a==="0"||n===0?false:n},x({...e,message:t}))},number(e){let t=e?.message??(n=>`Cannot cast "${n}" to a number. Expected a numeric string or number.`);return l(n=>{if(g(n))return Number(n);if(S(n)){let a=n.trim();if(a==="")return n;let f=Number(a);if(Number.isFinite(f))return f}return n},w({...e,message:t}))},string(e){let t=e?.message??(n=>`Cannot cast "${n}" to string. Expected a string, number, or boolean.`);return l(n=>g(n)||y(n)&&Number.isFinite(n)?String(n):n,k({...e,message:t}))},date(e){let t=e?.message??(n=>`Cannot cast "${n}" to a valid date.`);return l(n=>{let a;return S(n)&&(a=n.trim()),y(n)&&Number.isFinite(n)||a?new Date(a??n):n},P({...e,message:t}))},json(e,t){let n=t?.message??(a=>`Cannot parse "${a}" as JSON.`);return m(e,"_parse",(a,f)=>{if(S(f))try{f=JSON.parse(f);}catch{let o={message:typeof n=="function"?n(f):n};return {success:false,error:o,errors:[o]}}return a(f)}),e}},H={object:F,string:k,number:w,boolean:x,date:P,function:C,enum:V,array:B,any:L,preprocess:l,union:M,literal:U,coerce:J,cast:q};function D(e){return t=>`The value "${t}" must be type of ${e} but is type of "${typeof t}".`}function m(e,t,n){let a=e[t];e[t]=(...f)=>n(a,...f);}function h(e,{type:t="any",name:n,message:a}={}){a=a||D(t);let f={name:n,message:a,type:t},o={_getName(){return n},_getType(){return t},_getDescription(){return this._getName()??this._getType()},_parse(r,c){let u=e(r);if(u===true)return {success:true,data:r};if(typeof u=="object"&&u?.success===true)return u;let s={message:typeof a=="function"?a(r):a};return {success:false,error:s,errors:[s]}},parse(r,c){let u=o._parse(r,c);if(!u.success)throw u=u,new Error(u.error.message,{cause:u.error.cause});return u.data},safeParse(r,c){return o._parse(r,c)},transform(r){return m(this,"_parse",(c,u,s)=>{let i=c(u,s);return i.success&&(i.data=r(i.data)),i}),this},optional(){return m(this,"_parse",(r,c,u)=>{let s=r(c,u);return !s.success&&c===void 0&&(s.data=void 0,s.success=true),s}),this},nullable(){return m(this,"_parse",(r,c,u)=>{let s=r(c,u);return !s.success&&c===null&&(s.data=null,s.success=true),s}),this},nullish(){return m(this,"_parse",(r,c,u)=>{let s=r(c,u);return !s.success&&c==null&&(s.success=true,s.data=c),s}),this},default(r){return m(this,"_parse",(c,u,s)=>(u===void 0&&(u=typeof r=="function"?r():r),c(u,s))),this},catch(r){return m(this,"_parse",(c,u,s)=>{let i=c(u,s);return i.success?i:{success:true,data:typeof r=="function"?r({input:u,error:i.error}):r}}),this},pipe(r){return m(this,"_parse",(c,u,s)=>{let i=c(u,s);return i.success?r._parse(i.data,s):i}),this},refine(r,{message:c,type:u}={}){return u&&(c=c||D(u),t=u),m(this,"_parse",(s,i,p)=>{let I=s(i,p),{abortEarly:z}=O(p);if(!I.success)return I;let d=r(I.data);if(d===true||typeof d=="object"&&d?.success===true)return I;let E={message:typeof c=="function"?c(i):c};return {success:false,error:E,errors:[E]}}),this}};return b.length>0?b.reduce((r,c)=>c(r,e,f)??r,o):o}var b=[];function Q(e){if(typeof e!="function")throw new TypeError("extend() requires a function argument");b.push(e);}export{F as a,k as b,w as c,x as d,P as e,C as f,V as g,B as h,L as i,l as j,M as k,U as l,J as m,q as n,H as o,m as p,Q as q};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {q}from'./chunk-37YJYCTR.mjs';q((t,h,i)=>{if(i?.type==="array"){let r=t;r.min=function(e,{message:n}={}){return this.refine(a=>a.length>=e,{message:n||`Array must contain at least ${e} items.`})},r.max=function(e,{message:n}={}){return this.refine(a=>a.length<=e,{message:n||`Array must contain at most ${e} items.`})},r.length=function(e,{message:n}={}){return this.refine(a=>a.length===e,{message:n||`Array must contain exactly ${e} items.`})},r.nonEmpty=function({message:e}={}){return this.refine(n=>n.length>0,{message:e||"Array must not be empty."})},r.unique=function({message:e}={}){return this.refine(n=>{let a=new Set;try{return n.every(c=>{let s=JSON.stringify(c);return a.has(s)?!1:(a.add(s),!0)})}catch{return new Set(n).size===n.length}},{message:e||"Array items must be unique."})},r.sort=function(){return this.transform(e=>[...e].sort())},r.reverse=function(){return this.transform(e=>[...e].reverse())};}return t});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {q}from'./chunk-37YJYCTR.mjs';q((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});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {q}from'./chunk-37YJYCTR.mjs';q((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});
|
package/dist/full.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';var A={abortEarly:true};function O(e,c){if(!c)return e;let s=e?.cause?.key?`${c}.${e.cause.key}`:`${c}`;return {message:`Error parsing key "${s}": ${e.message}`,cause:{key:s}}}function x(e,c,s){if(e.errors?.length)for(let a=0;a<e.errors.length;a++)c.push(O(e.errors[a],s));}function T(e){return {...A,...e}}var l=e=>typeof e=="string"||e instanceof String,d=e=>(typeof e=="number"||e instanceof Number)&&!Number.isNaN(e),b=e=>e===true||e===false,P=e=>e instanceof Date&&!Number.isNaN(e.getTime()),E=e=>typeof e=="function",_=e=>Array.isArray(e),N=e=>typeof e=="object"&&e!==null&&!Array.isArray(e),p={object(e,c){let s=S(N,{...c,type:"object"});return s._getDescription=()=>`object({ ${Object.entries(e).map(([t,r])=>`${t}: ${r._getDescription()}`).join(", ")} })`,h(s,"_parse",(a,t,r)=>{let n=a(t,r),{abortEarly:o}=T(r);if(n.success===false)return n;let u={},i=[];for(let m in e){let f=e[m]._parse(n.data[m],r);if(f.success)u[m]=f.data;else {if(f=f,o!==false){let I=O(f.error,m);return {success:false,error:I,errors:[I]}}x(f,i,m);}}return i.length>0?{success:false,error:i[0],errors:i}:{success:true,data:u}}),s},string(e){return S(l,{...e,type:"string"})},number(e){return S(d,{...e,type:"number"})},boolean(e){return S(b,{...e,type:"boolean"})},date(e){return S(P,{...e,type:"date"})},function(e){return S(E,{...e,type:"function"})},enum(e,c){let s=n=>e.includes(n),a=n=>`Invalid ${t} value. Expected ${e.map(o=>`"${o}"`).join(" | ")}, received "${n}".`,t="enum",r=S(s,{message:a,...c,type:t});return r._getDescription=()=>`enum(${e.map(n=>`"${n}"`).join(" | ")})`,r},array(e,c){let s=S(_,{...c,type:"array"});return s._getDescription=()=>`array(${e._getDescription()})`,h(s,"_parse",(a,t,r)=>{let n=a(t,r),{abortEarly:o}=T(r);if(n.success===false)return n;let u=[],i=[];for(let m=0;m<n.data.length;m++){let f=e._parse(n.data[m],r);if(f.success)u.push(f.data);else {if(f=f,o!==false){let I=O(f.error,m);return {success:false,error:I,errors:[I]}}x(f,i,m);}}return i.length>0?{success:false,error:i[0],errors:i}:{success:true,data:u}}),s},any(){return S(()=>true)},preprocess(e,c){return h(c,"_parse",(s,a)=>(a=e(a),s(a))),c},union(e,c){return S(r=>{for(let n=0;n<e.length;n++){let o=e[n]._parse(r);if(o.success)return o}return false},{message:r=>`Invalid union value. Expected the value to match one of the schemas:${e.map((n,o)=>` ${o+1}. ${n._getDescription()}`).join(",")} but received "${typeof r}" with value: ${N(r)?JSON.stringify(r):`"${r}"`}`,...c,type:"union"})},literal(e,c){let t=S(r=>r===e,{message:r=>c?.message?typeof c.message=="function"?c.message(r):c.message:`Expected literal value "${e}", received "${r}"`,name:c?.name,type:"literal"});return t._getDescription=()=>`literal("${e}")`,t},coerce:{string(e){return p.preprocess(c=>String(c),p.string(e))},number(e){let c=e?.message??(s=>`Cannot coerce "${s}" to a valid number.`);return p.preprocess(s=>Number(s),p.number({...e,message:c}))},boolean(e){return p.preprocess(c=>!!c,p.boolean(e))},date(e){let c=e?.message??(s=>`Cannot coerce "${s}" to a valid date.`);return p.preprocess(s=>new Date(s),p.date({...e,message:c}))}},cast:{boolean(e){let c=e?.message??(s=>`Cannot cast "${s}" to boolean. Accepted: true/false, 1/0, yes/no, on/off.`);return p.preprocess(s=>{let a;return l(s)&&(a=s.toLowerCase()),a==="true"||a==="yes"||a==="on"||a==="1"||s===1?true:a==="false"||a==="no"||a==="off"||a==="0"||s===0?false:s},p.boolean({...e,message:c}))},number(e){let c=e?.message??(s=>`Cannot cast "${s}" to a number. Expected a numeric string or number.`);return p.preprocess(s=>{if(b(s))return Number(s);if(l(s)){let a=s.trim();if(a==="")return s;let t=Number(a);if(Number.isFinite(t))return t}return s},p.number({...e,message:c}))},string(e){let c=e?.message??(s=>`Cannot cast "${s}" to string. Expected a string, number, or boolean.`);return p.preprocess(s=>b(s)||d(s)&&Number.isFinite(s)?String(s):s,p.string({...e,message:c}))},date(e){let c=e?.message??(s=>`Cannot cast "${s}" to a valid date.`);return p.preprocess(s=>{let a;return l(s)&&(a=s.trim()),d(s)&&Number.isFinite(s)||a?new Date(a??s):s},p.date({...e,message:c}))},json(e,c){let s=c?.message??(a=>`Cannot parse "${a}" as JSON.`);return h(e,"_parse",(a,t)=>{if(l(t))try{t=JSON.parse(t);}catch{let r={message:typeof s=="function"?s(t):s};return {success:false,error:r,errors:[r]}}return a(t)}),e}}};function $(e){return c=>`The value "${c}" must be type of ${e} but is type of "${typeof c}".`}function h(e,c,s){let a=e[c];e[c]=(...t)=>s(a,...t);}function S(e,{type:c="any",name:s,message:a}={}){a=a||$(c);let t={name:s,message:a,type:c},r={_getName(){return s},_getType(){return c},_getDescription(){return this._getName()??this._getType()},_parse(n,o){let u=e(n);if(u===true)return {success:true,data:n};if(typeof u=="object"&&u?.success===true)return u;let i={message:typeof a=="function"?a(n):a};return {success:false,error:i,errors:[i]}},parse(n,o){let u=r._parse(n,o);if(!u.success)throw u=u,new Error(u.error.message,{cause:u.error.cause});return u.data},safeParse(n,o){return r._parse(n,o)},transform(n){return h(this,"_parse",(o,u,i)=>{let m=o(u,i);return m.success&&(m.data=n(m.data)),m}),this},optional(){return h(this,"_parse",(n,o,u)=>{let i=n(o,u);return !i.success&&o===void 0&&(i.data=void 0,i.success=true),i}),this},nullable(){return h(this,"_parse",(n,o,u)=>{let i=n(o,u);return !i.success&&o===null&&(i.data=null,i.success=true),i}),this},nullish(){return h(this,"_parse",(n,o,u)=>{let i=n(o,u);return !i.success&&o==null&&(i.success=true,i.data=o),i}),this},default(n){return h(this,"_parse",(o,u,i)=>(u===void 0&&(u=typeof n=="function"?n():n),o(u,i))),this},catch(n){return h(this,"_parse",(o,u,i)=>{let m=o(u,i);return m.success?m:{success:true,data:typeof n=="function"?n({input:u,error:m.error}):n}}),this},pipe(n){return h(this,"_parse",(o,u,i)=>{let m=o(u,i);return m.success?n._parse(m.data,i):m}),this},refine(n,{message:o,type:u}={}){return u&&(o=o||$(u),c=u),h(this,"_parse",(i,m,f)=>{let I=i(m,f);T(f);if(!I.success)return I;let y=n(I.data);if(y===true||typeof y=="object"&&y?.success===true)return I;let k={message:typeof o=="function"?o(m):o};return {success:false,error:k,errors:[k]}}),this}};return w.length>0?w.reduce((n,o)=>o(n,e,t)??n,r):r}var w=[];function g(e){if(typeof e!="function")throw new TypeError("extend() requires a function argument");w.push(e);}g((e,c,s)=>{if(s?.type==="string"){let a=e;a.min=function(t,{message:r}={}){return this.refine(n=>n.length>=t,{message:r||(n=>`String must be at least ${t} characters long (received ${n.length} characters: "${n}")`)})},a.max=function(t,{message:r}={}){return this.refine(n=>n.length<=t,{message:r||(n=>`String must be at most ${t} characters long (received ${n.length} characters: "${n}")`)})},a.length=function(t,{message:r}={}){return this.refine(n=>n.length===t,{message:r||(n=>`String must be exactly ${t} characters long (received ${n.length} characters: "${n}")`)})},a.nonEmpty=function({message:t}={}){return this.refine(r=>r.length>0,{message:t||"String must not be empty (received empty string)"})},a.startsWith=function(t,{message:r}={}){return this.refine(n=>n.startsWith(t),{message:r||(n=>`String must start with "${t}" (received: "${n}")`)})},a.endsWith=function(t,{message:r}={}){return this.refine(n=>n.endsWith(t),{message:r||(n=>`String must end with "${t}" (received: "${n}")`)})},a.includes=function(t,{message:r}={}){return this.refine(n=>n.includes(t),{message:r||(n=>`String must include "${t}" (received: "${n}")`)})},a.toLowerCase=function(){return this.transform(t=>t.toLowerCase())},a.toUpperCase=function(){return this.transform(t=>t.toUpperCase())},a.trim=function(){return this.transform(t=>t.trim())},a.padStart=function(t,r=" "){return this.transform(n=>n.padStart(t,r))},a.padEnd=function(t,r=" "){return this.transform(n=>n.padEnd(t,r))},a.replace=function(t,r){return this.transform(n=>n.replace(t,r))};}return e});g((e,c,s)=>{if(s?.type==="number"){let a=e;a.min=function(t,{message:r}={}){return this.refine(n=>n>=t,{message:r||`Number must be greater than or equal to ${t}.`})},a.max=function(t,{message:r}={}){return this.refine(n=>n<=t,{message:r||`Number must be less than or equal to ${t}.`})},a.positive=function({message:t}={}){return this.refine(r=>r>0,{message:t||"Number must be positive."})},a.negative=function({message:t}={}){return this.refine(r=>r<0,{message:t||"Number must be negative."})},a.int=function({message:t}={}){return this.refine(r=>Number.isInteger(r),{message:t||"Number must be an integer."})},a.float=function({message:t}={}){return this.refine(r=>Number.isFinite(r)&&!Number.isInteger(r),{message:t||"Number must be a floating point (non-integer)."})},a.multipleOf=function(t,{message:r}={}){return this.refine(n=>n%t===0,{message:r||`Number must be a multiple of ${t}.`})},a.finite=function({message:t}={}){return this.refine(r=>Number.isFinite(r),{message:t||"Number must be finite."})};}return e});g((e,c,s)=>{if(s?.type==="array"){let a=e;a.min=function(t,{message:r}={}){return this.refine(n=>n.length>=t,{message:r||`Array must contain at least ${t} items.`})},a.max=function(t,{message:r}={}){return this.refine(n=>n.length<=t,{message:r||`Array must contain at most ${t} items.`})},a.length=function(t,{message:r}={}){return this.refine(n=>n.length===t,{message:r||`Array must contain exactly ${t} items.`})},a.nonEmpty=function({message:t}={}){return this.refine(r=>r.length>0,{message:t||"Array must not be empty."})},a.unique=function({message:t}={}){return this.refine(r=>{let n=new Set;try{return r.every(o=>{let u=JSON.stringify(o);return n.has(u)?!1:(n.add(u),!0)})}catch{return new Set(r).size===r.length}},{message:t||"Array items must be unique."})},a.sort=function(){return this.transform(t=>[...t].sort())},a.reverse=function(){return this.transform(t=>[...t].reverse())};}return e});exports.extend=g;exports.hookOriginal=h;exports.s=p;exports.schema=p;
|
|
1
|
+
'use strict';var D={abortEarly:true};function T(e,s){if(!s)return e;let c=e?.cause?.key?`${s}.${e.cause.key}`:`${s}`;return {message:`Error parsing key "${c}": ${e.message}`,cause:{key:c}}}function E(e,s,c){if(e.errors?.length)for(let a=0;a<e.errors.length;a++)s.push(T(e.errors[a],c));}function w(e){return e?.abortEarly!==void 0?e:D}var g=e=>typeof e=="string"||e instanceof String,d=e=>(typeof e=="number"||e instanceof Number)&&!Number.isNaN(e),b=e=>e===true||e===false,R=e=>e instanceof Date&&!Number.isNaN(e.getTime()),j=e=>typeof e=="function",F=e=>Array.isArray(e),_=e=>typeof e=="object"&&e!==null&&!Array.isArray(e);function C(e,s){let c=h(_,{...s,type:"object"});return c._getDescription=()=>`object({ ${Object.entries(e).map(([t,r])=>`${t}: ${r._getDescription()}`).join(", ")} })`,p(c,"_parse",(a,t,r)=>{let n=a(t,r),{abortEarly:o}=w(r);if(n.success===false)return n;let u={},i=[];for(let m in e){let f=e[m]._parse(n.data[m],r);if(f.success)u[m]=f.data;else {if(f=f,o!==false){let S=T(f.error,m);return {success:false,error:S,errors:[S]}}E(f,i,m);}}return i.length>0?{success:false,error:i[0],errors:i}:{success:true,data:u}}),c}function k(e){return h(g,{...e,type:"string"})}function x(e){return h(d,{...e,type:"number"})}function N(e){return h(b,{...e,type:"boolean"})}function $(e){return h(R,{...e,type:"date"})}function v(e){return h(j,{...e,type:"function"})}function V(e,s){let c=n=>e.includes(n),a=n=>`Invalid ${t} value. Expected ${e.map(o=>`"${o}"`).join(" | ")}, received "${n}".`,t="enum",r=h(c,{message:a,...s,type:t});return r._getDescription=()=>`enum(${e.map(n=>`"${n}"`).join(" | ")})`,r}function L(e,s){let c=h(F,{...s,type:"array"});return c._getDescription=()=>`array(${e._getDescription()})`,p(c,"_parse",(a,t,r)=>{let n=a(t,r),{abortEarly:o}=w(r);if(n.success===false)return n;let u=[],i=[];for(let m=0;m<n.data.length;m++){let f=e._parse(n.data[m],r);if(f.success)u.push(f.data);else {if(f=f,o!==false){let S=T(f.error,m);return {success:false,error:S,errors:[S]}}E(f,i,m);}}return i.length>0?{success:false,error:i[0],errors:i}:{success:true,data:u}}),c}function B(){return h(()=>true)}function I(e,s){return p(s,"_parse",(c,a)=>(a=e(a),c(a))),s}function M(e,s){return h(r=>{for(let n=0;n<e.length;n++){let o=e[n]._parse(r);if(o.success)return o}return false},{message:r=>`Invalid union value. Expected the value to match one of the schemas:${e.map((n,o)=>` ${o+1}. ${n._getDescription()}`).join(",")} but received "${typeof r}" with value: ${_(r)?JSON.stringify(r):`"${r}"`}`,...s,type:"union"})}function U(e,s){let t=h(r=>r===e,{message:r=>s?.message?typeof s.message=="function"?s.message(r):s.message:`Expected literal value "${e}", received "${r}"`,name:s?.name,type:"literal"});return t._getDescription=()=>`literal("${e}")`,t}var q={string(e){return I(s=>String(s),k(e))},number(e){let s=e?.message??(c=>`Cannot coerce "${c}" to a valid number.`);return I(c=>Number(c),x({...e,message:s}))},boolean(e){return I(s=>!!s,N(e))},date(e){let s=e?.message??(c=>`Cannot coerce "${c}" to a valid date.`);return I(c=>new Date(c),$({...e,message:s}))}},W={boolean(e){let s=e?.message??(c=>`Cannot cast "${c}" to boolean. Accepted: true/false, 1/0, yes/no, on/off.`);return I(c=>{let a;return g(c)&&(a=c.toLowerCase()),a==="true"||a==="yes"||a==="on"||a==="1"||c===1?true:a==="false"||a==="no"||a==="off"||a==="0"||c===0?false:c},N({...e,message:s}))},number(e){let s=e?.message??(c=>`Cannot cast "${c}" to a number. Expected a numeric string or number.`);return I(c=>{if(b(c))return Number(c);if(g(c)){let a=c.trim();if(a==="")return c;let t=Number(a);if(Number.isFinite(t))return t}return c},x({...e,message:s}))},string(e){let s=e?.message??(c=>`Cannot cast "${c}" to string. Expected a string, number, or boolean.`);return I(c=>b(c)||d(c)&&Number.isFinite(c)?String(c):c,k({...e,message:s}))},date(e){let s=e?.message??(c=>`Cannot cast "${c}" to a valid date.`);return I(c=>{let a;return g(c)&&(a=c.trim()),d(c)&&Number.isFinite(c)||a?new Date(a??c):c},$({...e,message:s}))},json(e,s){let c=s?.message??(a=>`Cannot parse "${a}" as JSON.`);return p(e,"_parse",(a,t)=>{if(g(t))try{t=JSON.parse(t);}catch{let r={message:typeof c=="function"?c(t):c};return {success:false,error:r,errors:[r]}}return a(t)}),e}},G={object:C,string:k,number:x,boolean:N,date:$,function:v,enum:V,array:L,any:B,preprocess:I,union:M,literal:U,coerce:q,cast:W};function P(e){return s=>`The value "${s}" must be type of ${e} but is type of "${typeof s}".`}function p(e,s,c){let a=e[s];e[s]=(...t)=>c(a,...t);}function h(e,{type:s="any",name:c,message:a}={}){a=a||P(s);let t={name:c,message:a,type:s},r={_getName(){return c},_getType(){return s},_getDescription(){return this._getName()??this._getType()},_parse(n,o){let u=e(n);if(u===true)return {success:true,data:n};if(typeof u=="object"&&u?.success===true)return u;let i={message:typeof a=="function"?a(n):a};return {success:false,error:i,errors:[i]}},parse(n,o){let u=r._parse(n,o);if(!u.success)throw u=u,new Error(u.error.message,{cause:u.error.cause});return u.data},safeParse(n,o){return r._parse(n,o)},transform(n){return p(this,"_parse",(o,u,i)=>{let m=o(u,i);return m.success&&(m.data=n(m.data)),m}),this},optional(){return p(this,"_parse",(n,o,u)=>{let i=n(o,u);return !i.success&&o===void 0&&(i.data=void 0,i.success=true),i}),this},nullable(){return p(this,"_parse",(n,o,u)=>{let i=n(o,u);return !i.success&&o===null&&(i.data=null,i.success=true),i}),this},nullish(){return p(this,"_parse",(n,o,u)=>{let i=n(o,u);return !i.success&&o==null&&(i.success=true,i.data=o),i}),this},default(n){return p(this,"_parse",(o,u,i)=>(u===void 0&&(u=typeof n=="function"?n():n),o(u,i))),this},catch(n){return p(this,"_parse",(o,u,i)=>{let m=o(u,i);return m.success?m:{success:true,data:typeof n=="function"?n({input:u,error:m.error}):n}}),this},pipe(n){return p(this,"_parse",(o,u,i)=>{let m=o(u,i);return m.success?n._parse(m.data,i):m}),this},refine(n,{message:o,type:u}={}){return u&&(o=o||P(u),s=u),p(this,"_parse",(i,m,f)=>{let S=i(m,f),{abortEarly:J}=w(f);if(!S.success)return S;let y=n(S.data);if(y===true||typeof y=="object"&&y?.success===true)return S;let A={message:typeof o=="function"?o(m):o};return {success:false,error:A,errors:[A]}}),this}};return O.length>0?O.reduce((n,o)=>o(n,e,t)??n,r):r}var O=[];function l(e){if(typeof e!="function")throw new TypeError("extend() requires a function argument");O.push(e);}l((e,s,c)=>{if(c?.type==="string"){let a=e;a.min=function(t,{message:r}={}){return this.refine(n=>n.length>=t,{message:r||(n=>`String must be at least ${t} characters long (received ${n.length} characters: "${n}")`)})},a.max=function(t,{message:r}={}){return this.refine(n=>n.length<=t,{message:r||(n=>`String must be at most ${t} characters long (received ${n.length} characters: "${n}")`)})},a.length=function(t,{message:r}={}){return this.refine(n=>n.length===t,{message:r||(n=>`String must be exactly ${t} characters long (received ${n.length} characters: "${n}")`)})},a.nonEmpty=function({message:t}={}){return this.refine(r=>r.length>0,{message:t||"String must not be empty (received empty string)"})},a.startsWith=function(t,{message:r}={}){return this.refine(n=>n.startsWith(t),{message:r||(n=>`String must start with "${t}" (received: "${n}")`)})},a.endsWith=function(t,{message:r}={}){return this.refine(n=>n.endsWith(t),{message:r||(n=>`String must end with "${t}" (received: "${n}")`)})},a.includes=function(t,{message:r}={}){return this.refine(n=>n.includes(t),{message:r||(n=>`String must include "${t}" (received: "${n}")`)})},a.toLowerCase=function(){return this.transform(t=>t.toLowerCase())},a.toUpperCase=function(){return this.transform(t=>t.toUpperCase())},a.trim=function(){return this.transform(t=>t.trim())},a.padStart=function(t,r=" "){return this.transform(n=>n.padStart(t,r))},a.padEnd=function(t,r=" "){return this.transform(n=>n.padEnd(t,r))},a.replace=function(t,r){return this.transform(n=>n.replace(t,r))};}return e});l((e,s,c)=>{if(c?.type==="number"){let a=e;a.min=function(t,{message:r}={}){return this.refine(n=>n>=t,{message:r||`Number must be greater than or equal to ${t}.`})},a.max=function(t,{message:r}={}){return this.refine(n=>n<=t,{message:r||`Number must be less than or equal to ${t}.`})},a.positive=function({message:t}={}){return this.refine(r=>r>0,{message:t||"Number must be positive."})},a.negative=function({message:t}={}){return this.refine(r=>r<0,{message:t||"Number must be negative."})},a.int=function({message:t}={}){return this.refine(r=>Number.isInteger(r),{message:t||"Number must be an integer."})},a.float=function({message:t}={}){return this.refine(r=>Number.isFinite(r)&&!Number.isInteger(r),{message:t||"Number must be a floating point (non-integer)."})},a.multipleOf=function(t,{message:r}={}){return this.refine(n=>n%t===0,{message:r||`Number must be a multiple of ${t}.`})},a.finite=function({message:t}={}){return this.refine(r=>Number.isFinite(r),{message:t||"Number must be finite."})};}return e});l((e,s,c)=>{if(c?.type==="array"){let a=e;a.min=function(t,{message:r}={}){return this.refine(n=>n.length>=t,{message:r||`Array must contain at least ${t} items.`})},a.max=function(t,{message:r}={}){return this.refine(n=>n.length<=t,{message:r||`Array must contain at most ${t} items.`})},a.length=function(t,{message:r}={}){return this.refine(n=>n.length===t,{message:r||`Array must contain exactly ${t} items.`})},a.nonEmpty=function({message:t}={}){return this.refine(r=>r.length>0,{message:t||"Array must not be empty."})},a.unique=function({message:t}={}){return this.refine(r=>{let n=new Set;try{return r.every(o=>{let u=JSON.stringify(o);return n.has(u)?!1:(n.add(u),!0)})}catch{return new Set(r).size===r.length}},{message:t||"Array items must be unique."})},a.sort=function(){return this.transform(t=>[...t].sort())},a.reverse=function(){return this.transform(t=>[...t].reverse())};}return e});exports.any=B;exports.array=L;exports.boolean=N;exports.cast=W;exports.coerce=q;exports.date=$;exports.enumSchema=V;exports.extend=l;exports.functionSchema=v;exports.hookOriginal=p;exports.literal=U;exports.number=x;exports.object=C;exports.preprocess=I;exports.s=G;exports.schema=G;exports.string=k;exports.union=M;
|
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, FunctionSchemaInterface, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, extend, hookOriginal, s, s as schema } from './index.mjs';
|
|
4
|
+
export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, FunctionSchemaInterface, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, any, array, boolean, cast, coerce, date, enumSchema, extend, functionSchema, hookOriginal, literal, number, object, preprocess, s, s as schema, string, union } from './index.mjs';
|
package/dist/full.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
1
|
import './string.js';
|
|
2
2
|
import './number.js';
|
|
3
3
|
import './array.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';
|
|
4
|
+
export { ArraySchemaInterface, BooleanSchemaInterface, DateSchemaInterface, EnumSchemaInterface, ErrorStructure, ExtenderType, FunctionSchemaInterface, Infer, Invalid, LiteralSchemaInterface, NumberSchemaInterface, ObjectSchemaInterface, SchemaInterface, SchemaInterfaceOptions, SchemaType, StringSchemaInterface, UnionSchemaInterface, Valid, any, array, boolean, cast, coerce, date, enumSchema, extend, functionSchema, hookOriginal, literal, number, object, preprocess, s, s as schema, string, union } from './index.js';
|
package/dist/full.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import'./chunk-
|
|
1
|
+
import'./chunk-5XWCCG75.mjs';import'./chunk-NCRS6NBV.mjs';import'./chunk-NRGPACGD.mjs';export{i as any,h as array,d as boolean,n as cast,m as coerce,e as date,g as enumSchema,q as extend,f as functionSchema,p as hookOriginal,l as literal,c as number,a as object,j as preprocess,o as s,o as schema,b as string,k as union}from'./chunk-37YJYCTR.mjs';
|
package/dist/index.cjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';var
|
|
1
|
+
'use strict';var N={abortEarly:true};function T(e,t){if(!t)return e;let n=e?.cause?.key?`${t}.${e.cause.key}`:`${t}`;return {message:`Error parsing key "${n}": ${e.message}`,cause:{key:n}}}function _(e,t,n){if(e.errors?.length)for(let a=0;a<e.errors.length;a++)t.push(T(e.errors[a],n));}function O(e){return e?.abortEarly!==void 0?e:N}var S=e=>typeof e=="string"||e instanceof String,y=e=>(typeof e=="number"||e instanceof Number)&&!Number.isNaN(e),g=e=>e===true||e===false,R=e=>e instanceof Date&&!Number.isNaN(e.getTime()),A=e=>typeof e=="function",j=e=>Array.isArray(e),$=e=>typeof e=="object"&&e!==null&&!Array.isArray(e);function F(e,t){let n=h($,{...t,type:"object"});return n._getDescription=()=>`object({ ${Object.entries(e).map(([f,o])=>`${f}: ${o._getDescription()}`).join(", ")} })`,m(n,"_parse",(a,f,o)=>{let r=a(f,o),{abortEarly:c}=O(o);if(r.success===false)return r;let u={},s=[];for(let i in e){let p=e[i]._parse(r.data[i],o);if(p.success)u[i]=p.data;else {if(p=p,c!==false){let I=T(p.error,i);return {success:false,error:I,errors:[I]}}_(p,s,i);}}return s.length>0?{success:false,error:s[0],errors:s}:{success:true,data:u}}),n}function k(e){return h(S,{...e,type:"string"})}function w(e){return h(y,{...e,type:"number"})}function x(e){return h(g,{...e,type:"boolean"})}function P(e){return h(R,{...e,type:"date"})}function C(e){return h(A,{...e,type:"function"})}function V(e,t){let n=r=>e.includes(r),a=r=>`Invalid ${f} value. Expected ${e.map(c=>`"${c}"`).join(" | ")}, received "${r}".`,f="enum",o=h(n,{message:a,...t,type:f});return o._getDescription=()=>`enum(${e.map(r=>`"${r}"`).join(" | ")})`,o}function B(e,t){let n=h(j,{...t,type:"array"});return n._getDescription=()=>`array(${e._getDescription()})`,m(n,"_parse",(a,f,o)=>{let r=a(f,o),{abortEarly:c}=O(o);if(r.success===false)return r;let u=[],s=[];for(let i=0;i<r.data.length;i++){let p=e._parse(r.data[i],o);if(p.success)u.push(p.data);else {if(p=p,c!==false){let I=T(p.error,i);return {success:false,error:I,errors:[I]}}_(p,s,i);}}return s.length>0?{success:false,error:s[0],errors:s}:{success:true,data:u}}),n}function L(){return h(()=>true)}function l(e,t){return m(t,"_parse",(n,a)=>(a=e(a),n(a))),t}function M(e,t){return h(o=>{for(let r=0;r<e.length;r++){let c=e[r]._parse(o);if(c.success)return c}return false},{message:o=>`Invalid union value. Expected the value to match one of the schemas:${e.map((r,c)=>` ${c+1}. ${r._getDescription()}`).join(",")} but received "${typeof o}" with value: ${$(o)?JSON.stringify(o):`"${o}"`}`,...t,type:"union"})}function U(e,t){let f=h(o=>o===e,{message:o=>t?.message?typeof t.message=="function"?t.message(o):t.message:`Expected literal value "${e}", received "${o}"`,name:t?.name,type:"literal"});return f._getDescription=()=>`literal("${e}")`,f}var J={string(e){return l(t=>String(t),k(e))},number(e){let t=e?.message??(n=>`Cannot coerce "${n}" to a valid number.`);return l(n=>Number(n),w({...e,message:t}))},boolean(e){return l(t=>!!t,x(e))},date(e){let t=e?.message??(n=>`Cannot coerce "${n}" to a valid date.`);return l(n=>new Date(n),P({...e,message:t}))}},q={boolean(e){let t=e?.message??(n=>`Cannot cast "${n}" to boolean. Accepted: true/false, 1/0, yes/no, on/off.`);return l(n=>{let a;return S(n)&&(a=n.toLowerCase()),a==="true"||a==="yes"||a==="on"||a==="1"||n===1?true:a==="false"||a==="no"||a==="off"||a==="0"||n===0?false:n},x({...e,message:t}))},number(e){let t=e?.message??(n=>`Cannot cast "${n}" to a number. Expected a numeric string or number.`);return l(n=>{if(g(n))return Number(n);if(S(n)){let a=n.trim();if(a==="")return n;let f=Number(a);if(Number.isFinite(f))return f}return n},w({...e,message:t}))},string(e){let t=e?.message??(n=>`Cannot cast "${n}" to string. Expected a string, number, or boolean.`);return l(n=>g(n)||y(n)&&Number.isFinite(n)?String(n):n,k({...e,message:t}))},date(e){let t=e?.message??(n=>`Cannot cast "${n}" to a valid date.`);return l(n=>{let a;return S(n)&&(a=n.trim()),y(n)&&Number.isFinite(n)||a?new Date(a??n):n},P({...e,message:t}))},json(e,t){let n=t?.message??(a=>`Cannot parse "${a}" as JSON.`);return m(e,"_parse",(a,f)=>{if(S(f))try{f=JSON.parse(f);}catch{let o={message:typeof n=="function"?n(f):n};return {success:false,error:o,errors:[o]}}return a(f)}),e}},H={object:F,string:k,number:w,boolean:x,date:P,function:C,enum:V,array:B,any:L,preprocess:l,union:M,literal:U,coerce:J,cast:q};function D(e){return t=>`The value "${t}" must be type of ${e} but is type of "${typeof t}".`}function m(e,t,n){let a=e[t];e[t]=(...f)=>n(a,...f);}function h(e,{type:t="any",name:n,message:a}={}){a=a||D(t);let f={name:n,message:a,type:t},o={_getName(){return n},_getType(){return t},_getDescription(){return this._getName()??this._getType()},_parse(r,c){let u=e(r);if(u===true)return {success:true,data:r};if(typeof u=="object"&&u?.success===true)return u;let s={message:typeof a=="function"?a(r):a};return {success:false,error:s,errors:[s]}},parse(r,c){let u=o._parse(r,c);if(!u.success)throw u=u,new Error(u.error.message,{cause:u.error.cause});return u.data},safeParse(r,c){return o._parse(r,c)},transform(r){return m(this,"_parse",(c,u,s)=>{let i=c(u,s);return i.success&&(i.data=r(i.data)),i}),this},optional(){return m(this,"_parse",(r,c,u)=>{let s=r(c,u);return !s.success&&c===void 0&&(s.data=void 0,s.success=true),s}),this},nullable(){return m(this,"_parse",(r,c,u)=>{let s=r(c,u);return !s.success&&c===null&&(s.data=null,s.success=true),s}),this},nullish(){return m(this,"_parse",(r,c,u)=>{let s=r(c,u);return !s.success&&c==null&&(s.success=true,s.data=c),s}),this},default(r){return m(this,"_parse",(c,u,s)=>(u===void 0&&(u=typeof r=="function"?r():r),c(u,s))),this},catch(r){return m(this,"_parse",(c,u,s)=>{let i=c(u,s);return i.success?i:{success:true,data:typeof r=="function"?r({input:u,error:i.error}):r}}),this},pipe(r){return m(this,"_parse",(c,u,s)=>{let i=c(u,s);return i.success?r._parse(i.data,s):i}),this},refine(r,{message:c,type:u}={}){return u&&(c=c||D(u),t=u),m(this,"_parse",(s,i,p)=>{let I=s(i,p),{abortEarly:z}=O(p);if(!I.success)return I;let d=r(I.data);if(d===true||typeof d=="object"&&d?.success===true)return I;let E={message:typeof c=="function"?c(i):c};return {success:false,error:E,errors:[E]}}),this}};return b.length>0?b.reduce((r,c)=>c(r,e,f)??r,o):o}var b=[];function Q(e){if(typeof e!="function")throw new TypeError("extend() requires a function argument");b.push(e);}exports.any=L;exports.array=B;exports.boolean=x;exports.cast=q;exports.coerce=J;exports.date=P;exports.enumSchema=V;exports.extend=Q;exports.functionSchema=C;exports.hookOriginal=m;exports.literal=U;exports.number=w;exports.object=F;exports.preprocess=l;exports.s=H;exports.schema=H;exports.string=k;exports.union=M;
|