@esmj/schema 0.3.6 → 0.5.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +731 -19
- package/dist/array.cjs +1 -0
- package/dist/array.d.cts +13 -0
- package/dist/array.d.ts +13 -0
- package/dist/array.js +1 -0
- package/dist/chunk-5ARMWSHU.js +1 -0
- package/dist/chunk-6ABUMTDR.js +1 -0
- package/dist/chunk-JFGD5PND.js +1 -0
- package/dist/chunk-QEBUM44M.js +1 -0
- package/dist/full.cjs +1 -0
- package/dist/full.d.cts +4 -0
- package/dist/full.d.ts +4 -0
- package/dist/full.js +1 -0
- package/dist/index.cjs +1 -0
- package/dist/index.d.cts +270 -0
- package/dist/index.d.ts +201 -6
- package/dist/index.js +1 -1
- package/dist/number.cjs +1 -0
- package/dist/number.d.cts +14 -0
- package/dist/number.d.ts +14 -0
- package/dist/number.js +1 -0
- package/dist/string.cjs +1 -0
- package/dist/string.d.cts +19 -0
- package/dist/string.d.ts +19 -0
- package/dist/string.js +1 -0
- package/examples/advanced-forms.ts +138 -0
- package/examples/basic-usage.ts +41 -0
- package/examples/custom-extensions.ts +120 -0
- package/examples/custom-validation.ts +110 -0
- package/package.json +40 -11
- package/tsconfig.json +12 -0
- package/dist/index.d.mts +0 -75
- package/dist/index.mjs +0 -1
package/dist/index.d.mts
DELETED
|
@@ -1,75 +0,0 @@
|
|
|
1
|
-
type ErrorStructure = {
|
|
2
|
-
message: string;
|
|
3
|
-
cause?: {
|
|
4
|
-
key?: string;
|
|
5
|
-
};
|
|
6
|
-
};
|
|
7
|
-
type Valid<Output> = {
|
|
8
|
-
success: true;
|
|
9
|
-
data: Output;
|
|
10
|
-
};
|
|
11
|
-
type Invalid = {
|
|
12
|
-
success: false;
|
|
13
|
-
error: ErrorStructure;
|
|
14
|
-
};
|
|
15
|
-
type InternalParseOutput<Output> = Valid<Output> | Invalid;
|
|
16
|
-
interface SchemaInterface<Input, Output> {
|
|
17
|
-
_getType(): string;
|
|
18
|
-
_parse(value: Input | Partial<Input>): InternalParseOutput<Output>;
|
|
19
|
-
parse(value: Input | Partial<Input>): Output;
|
|
20
|
-
safeParse(value: Input | Partial<Input>): InternalParseOutput<Output>;
|
|
21
|
-
optional(): SchemaInterface<Input, Partial<Output> | undefined>;
|
|
22
|
-
transform<NewOutput>(callback: (value: Input) => NewOutput): SchemaInterface<Input, NewOutput>;
|
|
23
|
-
nullable(): SchemaInterface<Input, Output | null>;
|
|
24
|
-
nullish(): SchemaInterface<Input, Output | undefined | null>;
|
|
25
|
-
default(defaultValue: Partial<Input> | (() => Partial<Input>) | Partial<Output>): SchemaInterface<Input, Output>;
|
|
26
|
-
pipe<NewOutput>(schema: SchemaInterface<Output, NewOutput>): SchemaInterface<Output, NewOutput>;
|
|
27
|
-
refine(validation: (value: Output) => boolean, options?: SchemaInterfaceOptions): SchemaInterface<Input, Output>;
|
|
28
|
-
}
|
|
29
|
-
interface UnionSchemaInterface<T extends Array<SchemaInterface<unknown, unknown>>> extends SchemaInterface<ReturnType<T[number]['parse']>, ReturnType<T[number]['parse']>> {
|
|
30
|
-
}
|
|
31
|
-
interface EnumSchemaInterface<T extends string> extends SchemaInterface<string, T> {
|
|
32
|
-
}
|
|
33
|
-
interface StringSchemaInterface extends SchemaInterface<string, string> {
|
|
34
|
-
}
|
|
35
|
-
interface NumberSchemaInterface extends SchemaInterface<number, number> {
|
|
36
|
-
}
|
|
37
|
-
interface BooleanSchemaInterface extends SchemaInterface<boolean, boolean> {
|
|
38
|
-
}
|
|
39
|
-
interface DateSchemaInterface extends SchemaInterface<Date, Date> {
|
|
40
|
-
}
|
|
41
|
-
interface ArraySchemaInterface<T extends SchemaType> extends SchemaInterface<Array<T>, Array<ReturnType<T['parse']>>> {
|
|
42
|
-
}
|
|
43
|
-
interface ObjectSchemaInterface<T extends Record<string, SchemaType>> extends SchemaInterface<{
|
|
44
|
-
[Property in keyof T]: ReturnType<T[Property]['parse']>;
|
|
45
|
-
}, {
|
|
46
|
-
[Property in keyof T]: ReturnType<T[Property]['parse']>;
|
|
47
|
-
}> {
|
|
48
|
-
}
|
|
49
|
-
type SchemaType = 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>;
|
|
50
|
-
type ErrorMessage = string | ((value: unknown) => string);
|
|
51
|
-
type ExtenderType = (inter: SchemaType, validation: Function, options?: {
|
|
52
|
-
message: ErrorMessage;
|
|
53
|
-
type: string;
|
|
54
|
-
}) => SchemaType;
|
|
55
|
-
interface CreateSchemaInterfaceOptions {
|
|
56
|
-
type?: string;
|
|
57
|
-
message?: ErrorMessage;
|
|
58
|
-
}
|
|
59
|
-
type SchemaInterfaceOptions = Omit<CreateSchemaInterfaceOptions, 'type'>;
|
|
60
|
-
declare const s: {
|
|
61
|
-
object<T extends Record<string, SchemaType>>(definition: { [Property in keyof T]: T[Property]; }, options?: SchemaInterfaceOptions): ObjectSchemaInterface<T>;
|
|
62
|
-
string(options?: SchemaInterfaceOptions): StringSchemaInterface;
|
|
63
|
-
number(options?: SchemaInterfaceOptions): NumberSchemaInterface;
|
|
64
|
-
boolean(options?: SchemaInterfaceOptions): BooleanSchemaInterface;
|
|
65
|
-
date(options?: SchemaInterfaceOptions): DateSchemaInterface;
|
|
66
|
-
enum(definition: Readonly<Array<string>>, options?: SchemaInterfaceOptions): EnumSchemaInterface<(typeof definition)[number]>;
|
|
67
|
-
array<T extends SchemaType>(definition: T, options?: SchemaInterfaceOptions): ArraySchemaInterface<T>;
|
|
68
|
-
any(): any;
|
|
69
|
-
preprocess<T extends SchemaType>(callback: Function, schema: T): T;
|
|
70
|
-
union<T extends Array<SchemaType>>(definitions: T, options?: SchemaInterfaceOptions): UnionSchemaInterface<T>;
|
|
71
|
-
};
|
|
72
|
-
declare function extend(callback: ExtenderType): void;
|
|
73
|
-
type Infer<T> = T extends SchemaType ? ReturnType<T['parse']> : unknown;
|
|
74
|
-
|
|
75
|
-
export { type ArraySchemaInterface, type BooleanSchemaInterface, type DateSchemaInterface, type EnumSchemaInterface, type ExtenderType, type Infer, type NumberSchemaInterface, type ObjectSchemaInterface, type SchemaInterface, type SchemaInterfaceOptions, type SchemaType, type StringSchemaInterface, type UnionSchemaInterface, extend, s };
|
package/dist/index.mjs
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
var h=e=>typeof e=="string"||e instanceof String,I=e=>typeof e=="number"||e instanceof Number,y=e=>e===true||e===false,l=e=>e instanceof Date&&!Number.isNaN(e.getTime()),S=e=>Array.isArray(e),d=e=>typeof e=="object"&&e!==null&&!Array.isArray(e),b={object(e,c){let s=f(d,{...c,type:"object"});return i(s,"_parse",(u,...o)=>{let n=u(...o);if(n.success===false)return n;let t={};for(let r in e){let a=e[r]._parse(n.data[r]);if(a.success)t[r]=a.data;else {a=a;let p=a?.error?.cause?.key?`${r}.${a?.error?.cause?.key}`:r;return a.error.message=`Error parsing key "${p}": ${a.error.message}`,a.error.cause={key:p},a}}return {success:true,data:t}}),s},string(e){return f(h,{...e,type:"string"})},number(e){return f(I,{...e,type:"number"})},boolean(e){return f(y,{...e,type:"boolean"})},date(e){return f(l,{...e,type:"date"})},enum(e,c){let s=t=>e.includes(t),u=t=>`Invalid ${o} value. Expected ${e.map(r=>`"${r}"`).join(" | ")}, received "${t}".`,o="enum";return f(s,{message:u,...c,type:o})},array(e,c){let s=f(S,{...c,type:"array"});return i(s,"_parse",(u,...o)=>{let n=u(...o);if(n.success===false)return n;let t=[];for(let r=0;r<n.data.length;r++){let a=e._parse(n.data[r]);if(a.success)t.push(a.data);else {a=a;let p=a.error?.cause?.key?`${r}.${a.error.cause.key}`:`${r}`;return a.error.message=`Error parsing index "${r}": ${a.error.message}`,a.error.cause={key:p},a}}return {success:true,data:t}}),s},any(){return f(()=>true)},preprocess(e,c){return i(c,"_parse",(s,u)=>(u=e(u),s(u))),c},union(e,c){return f(n=>{for(let t=0;t<e.length;t++)if(e[t]._parse(n).success)return true;return false},{message:n=>`Invalid union value. Expected the value to match one of the schemas: ${e.map(t=>`"${t._getType()}"`).join(" | ")}, but received "${typeof n}" with value "${n}".`,...c,type:"union"})}};function T(e){return c=>`The value "${c}" must be type of ${e} but is type of "${typeof c}".`}function i(e,c,s){let u=e[c];e[c]=(...o)=>s(u,...o);}function f(e,{type:c="any",message:s}={}){s=s||T(c);let u={message:s,type:c},o={_getType(){return c},_parse(n){return e(n)?{success:true,data:n}:{success:false,error:{message:typeof s=="function"?s(n):s}}},parse(n){let t=o._parse(n);if(!t.success)throw t=t,new Error(t.error.message,{cause:t.error.cause});return t.data},safeParse(n){return o._parse(n)},transform(n){return i(this,"_parse",(t,r)=>{let a=t(r);return a.success&&(a.data=n(a.data)),a}),this},optional(){return i(this,"_parse",(n,t)=>{let r=n(t);return r.success||(r.data=void 0,r.success=true),r}),this},nullable(){return i(this,"_parse",(n,t)=>{let r=n(t);return r.success||(r.data=null,r.success=true),r}),this},nullish(){return i(this,"_parse",(n,t)=>{let r=n(t);return !r.success&&t==null&&(r.success=true,r.data=t),r}),this},default(n){return i(this,"_parse",(t,r)=>(r===void 0&&(r=n,r=typeof n=="function"?n():n),t(r))),this},pipe(n){return i(this,"_parse",(t,r)=>{let a=t(r);return a.success?n._parse(a.data):a}),this},refine(n,{message:t}={}){return i(this,"_parse",(r,a)=>{let p=r(a);return p.success?n(p.data)?p:{success:false,error:{message:typeof t=="function"?t(a):t}}:p}),this}};return m.length>0?m.reduce((n,t)=>t(n,e,u)??n,o):o}var m=[];function g(e){m.push(e);}export{g as extend,b as s};
|