@esmj/schema 0.1.0 → 0.1.1
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 +32 -0
- package/dist/index.d.mts +5 -2
- package/dist/index.d.ts +5 -2
- package/dist/index.js +1 -1
- package/dist/index.mjs +1 -1
- package/package.json +8 -8
- package/biome.json +0 -50
package/README.md
CHANGED
|
@@ -18,6 +18,18 @@ npm install @esmj/schema
|
|
|
18
18
|
4. **Lightweight**: None dependencies and a small footprint make it ideal for projects where performance and simplicity are key.
|
|
19
19
|
5. **Customizable**: Offers fine-grained control over validation and error handling.
|
|
20
20
|
|
|
21
|
+
## Comparison with Similar Libraries
|
|
22
|
+
|
|
23
|
+
When choosing a schema validation library, bundle size can be an important factor, especially for frontend applications where minimizing JavaScript size is critical. Here's how `@esmj/schema` compares to other popular libraries:
|
|
24
|
+
|
|
25
|
+
| Library | Bundle Size (minified + gzipped) |
|
|
26
|
+
|------------------|---------------------------------|
|
|
27
|
+
| `@esmj/schema` | ~1 KB |
|
|
28
|
+
| Superstruct | ~3,2 KB |
|
|
29
|
+
| Yup | ~12,2 KB |
|
|
30
|
+
| Zod | ~14 KB |
|
|
31
|
+
| Joi | ~40 KB |
|
|
32
|
+
|
|
21
33
|
## Usage
|
|
22
34
|
|
|
23
35
|
### Basic Usage
|
|
@@ -125,6 +137,26 @@ Creates a schema that accepts any value.
|
|
|
125
137
|
const anySchema = s.any();
|
|
126
138
|
```
|
|
127
139
|
|
|
140
|
+
#### `s.enum(values)`
|
|
141
|
+
|
|
142
|
+
Creates an enum schema that validates against a predefined set of string values.
|
|
143
|
+
|
|
144
|
+
- **`values`**: An array of strings representing the allowed values for the enum. Each value must be a string.
|
|
145
|
+
|
|
146
|
+
```typescript
|
|
147
|
+
const enumSchema = s.enum(['admin', 'user', 'guest']);
|
|
148
|
+
|
|
149
|
+
const validResult = enumSchema.parse('admin');
|
|
150
|
+
console.log(validResult);
|
|
151
|
+
// 'admin'
|
|
152
|
+
|
|
153
|
+
const invalidResult = enumSchema.safeParse('invalidRole');
|
|
154
|
+
console.log(invalidResult.success);
|
|
155
|
+
// false
|
|
156
|
+
console.log(invalidResult.error.message);
|
|
157
|
+
// Error: Invalid enum value. Expected "admin" | "user" | "guest", received "invalidRole".
|
|
158
|
+
```
|
|
159
|
+
|
|
128
160
|
#### `s.preprocess(callback, schema)`
|
|
129
161
|
|
|
130
162
|
Creates a schema that preprocesses the input value using the provided callback before validating it with the given schema.
|
package/dist/index.d.mts
CHANGED
|
@@ -17,6 +17,8 @@ interface SchemaInterface<Input, Output> {
|
|
|
17
17
|
message: string;
|
|
18
18
|
}): SchemaInterface<Input, Output>;
|
|
19
19
|
}
|
|
20
|
+
interface EnumSchemaInterface<T extends string> extends SchemaInterface<string, T> {
|
|
21
|
+
}
|
|
20
22
|
interface StringSchemaInterface extends SchemaInterface<string, string> {
|
|
21
23
|
}
|
|
22
24
|
interface NumberSchemaInterface extends SchemaInterface<number, number> {
|
|
@@ -33,7 +35,7 @@ interface ObjectSchemaInterface<T extends Record<string, SchemaType>> extends Sc
|
|
|
33
35
|
[Property in keyof T]: ReturnType<T[Property]['parse']>;
|
|
34
36
|
}> {
|
|
35
37
|
}
|
|
36
|
-
type SchemaType = StringSchemaInterface | 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> | ArraySchemaInterface<StringSchemaInterface | ObjectSchemaInterface<Record<string, SchemaType>> | NumberSchemaInterface | BooleanSchemaInterface
|
|
38
|
+
type SchemaType = StringSchemaInterface | 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> | 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>;
|
|
37
39
|
type ExtenderType = (inter: SchemaType, validation: Function, options: {
|
|
38
40
|
message: string;
|
|
39
41
|
type: string;
|
|
@@ -44,6 +46,7 @@ declare const s: {
|
|
|
44
46
|
number(): NumberSchemaInterface;
|
|
45
47
|
boolean(): BooleanSchemaInterface;
|
|
46
48
|
date(): DateSchemaInterface;
|
|
49
|
+
enum(definition: Readonly<Array<string>>): EnumSchemaInterface<(typeof definition)[number]>;
|
|
47
50
|
array<T extends SchemaType>(definition: T): ArraySchemaInterface<T>;
|
|
48
51
|
any(): any;
|
|
49
52
|
preprocess<T extends SchemaType>(callback: Function, schema: T): T;
|
|
@@ -51,4 +54,4 @@ declare const s: {
|
|
|
51
54
|
declare function extend(callback: ExtenderType): void;
|
|
52
55
|
type Infer<T> = T extends SchemaType ? ReturnType<T['parse']> : unknown;
|
|
53
56
|
|
|
54
|
-
export { type ArraySchemaInterface, type BooleanSchemaInterface, type DateSchemaInterface, type ExtenderType, type Infer, type NumberSchemaInterface, type ObjectSchemaInterface, type SchemaInterface, type SchemaType, type StringSchemaInterface, extend, s };
|
|
57
|
+
export { type ArraySchemaInterface, type BooleanSchemaInterface, type DateSchemaInterface, type EnumSchemaInterface, type ExtenderType, type Infer, type NumberSchemaInterface, type ObjectSchemaInterface, type SchemaInterface, type SchemaType, type StringSchemaInterface, extend, s };
|
package/dist/index.d.ts
CHANGED
|
@@ -17,6 +17,8 @@ interface SchemaInterface<Input, Output> {
|
|
|
17
17
|
message: string;
|
|
18
18
|
}): SchemaInterface<Input, Output>;
|
|
19
19
|
}
|
|
20
|
+
interface EnumSchemaInterface<T extends string> extends SchemaInterface<string, T> {
|
|
21
|
+
}
|
|
20
22
|
interface StringSchemaInterface extends SchemaInterface<string, string> {
|
|
21
23
|
}
|
|
22
24
|
interface NumberSchemaInterface extends SchemaInterface<number, number> {
|
|
@@ -33,7 +35,7 @@ interface ObjectSchemaInterface<T extends Record<string, SchemaType>> extends Sc
|
|
|
33
35
|
[Property in keyof T]: ReturnType<T[Property]['parse']>;
|
|
34
36
|
}> {
|
|
35
37
|
}
|
|
36
|
-
type SchemaType = StringSchemaInterface | 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> | ArraySchemaInterface<StringSchemaInterface | ObjectSchemaInterface<Record<string, SchemaType>> | NumberSchemaInterface | BooleanSchemaInterface
|
|
38
|
+
type SchemaType = StringSchemaInterface | 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> | 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>;
|
|
37
39
|
type ExtenderType = (inter: SchemaType, validation: Function, options: {
|
|
38
40
|
message: string;
|
|
39
41
|
type: string;
|
|
@@ -44,6 +46,7 @@ declare const s: {
|
|
|
44
46
|
number(): NumberSchemaInterface;
|
|
45
47
|
boolean(): BooleanSchemaInterface;
|
|
46
48
|
date(): DateSchemaInterface;
|
|
49
|
+
enum(definition: Readonly<Array<string>>): EnumSchemaInterface<(typeof definition)[number]>;
|
|
47
50
|
array<T extends SchemaType>(definition: T): ArraySchemaInterface<T>;
|
|
48
51
|
any(): any;
|
|
49
52
|
preprocess<T extends SchemaType>(callback: Function, schema: T): T;
|
|
@@ -51,4 +54,4 @@ declare const s: {
|
|
|
51
54
|
declare function extend(callback: ExtenderType): void;
|
|
52
55
|
type Infer<T> = T extends SchemaType ? ReturnType<T['parse']> : unknown;
|
|
53
56
|
|
|
54
|
-
export { type ArraySchemaInterface, type BooleanSchemaInterface, type DateSchemaInterface, type ExtenderType, type Infer, type NumberSchemaInterface, type ObjectSchemaInterface, type SchemaInterface, type SchemaType, type StringSchemaInterface, extend, s };
|
|
57
|
+
export { type ArraySchemaInterface, type BooleanSchemaInterface, type DateSchemaInterface, type EnumSchemaInterface, type ExtenderType, type Infer, type NumberSchemaInterface, type ObjectSchemaInterface, type SchemaInterface, type SchemaType, type StringSchemaInterface, extend, s };
|
package/dist/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
'use strict';var y={object(n){let u=i(c=>typeof c=="object"&&c!==null&&!Array.isArray(c),{type:"object"});return s(u,"parse",(c,...o)=>{let e=c(...o);return n&&typeof e=="object"?Object.keys(n).reduce((t
|
|
1
|
+
'use strict';var y={object(n){let u=i(c=>typeof c=="object"&&c!==null&&!Array.isArray(c),{type:"object"});return s(u,"parse",(c,...o)=>{let e=c(...o);return n&&typeof e=="object"?Object.keys(n).reduce((r,t)=>{if(typeof n[t]?.parse=="function")try{r[t]=n[t].parse(e[t]);}catch(p){throw t=p.cause?.key?`${t}.${p.cause.key}`:t,new Error(`Error parsing key "${t}": ${p.message}`,{cause:{key:t}})}return r},{}):e}),u},string(){return i(a=>typeof a=="string",{type:"string"})},number(){return i(a=>typeof a=="number",{type:"number"})},boolean(){return i(a=>a===true||a===false,{type:"boolean"})},date(){return i(a=>a instanceof Date&&!Number.isNaN(a.getTime()),{type:"date"})},enum(n){let a=e=>n.includes(e),u=e=>`Invalid ${c} value. Expected ${n.map(r=>`"${r}"`).join(" | ")}, received "${e}".`,c="enum";return i(a,{type:c,message:u})},array(n){let u=i(c=>Array.isArray(c),{type:"array"});return s(u,"parse",(c,o)=>(o=c(o),n&&Array.isArray(o)?o.map((e,r)=>{try{return n.parse(e)}catch(t){let p=t.cause?.key?`${r}.${t.cause.key}`:r;throw new Error(`Error parsing index "${r}": ${t.message}`,{cause:{key:p}})}}):o)),u},any(){return i(()=>true)},preprocess(n,a){return s(a,"parse",(u,c)=>(c=n(c),u(c))),a}};function h(n){return a=>`The value "${a}" must be type of ${n} but is type of "${typeof a}".`}function s(n,a,u){let c=n[a];n[a]=(...o)=>u(c,...o);}function i(n,{type:a="any",message:u}={}){u=u||h(a);let c={message:u,type:a},o={parse(e){if(!n(e))throw new Error(u(e));return e},safeParse(e){try{return {success:!0,data:this.parse(e)}}catch(r){return {success:false,error:r}}},transform(e){return s(this,"parse",(r,t)=>e(r(t))),this},optional(){return s(this,"parse",(e,r)=>{try{return e(r)}catch{return}}),this},nullable(){return s(this,"parse",(e,r)=>{try{return e(r)}catch{return null}}),this},nullish(){return s(this,"parse",(e,r)=>{try{return e(r)}catch{return r==null?r:null}}),this},default(e){return s(this,"parse",(r,t)=>(t===void 0&&(t=e,t=typeof e=="function"?e():e),t=r(t),t)),this},pipe(e){return s(this,"parse",(r,t)=>e.parse(r(t))),this},refine(e,{message:r}){return s(this,"parse",(t,p)=>{let f=t(p);if(!e(f))throw new Error(r);return f}),this}};return m.reduce((e,r)=>r(e,n,c)??e,o)}var m=[];function I(n){m.push(n);}exports.extend=I;exports.s=y;
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
var y={object(n){let u=i(c=>typeof c=="object"&&c!==null&&!Array.isArray(c),{type:"object"});return s(u,"parse",(c,...o)=>{let e=c(...o);return n&&typeof e=="object"?Object.keys(n).reduce((t
|
|
1
|
+
var y={object(n){let u=i(c=>typeof c=="object"&&c!==null&&!Array.isArray(c),{type:"object"});return s(u,"parse",(c,...o)=>{let e=c(...o);return n&&typeof e=="object"?Object.keys(n).reduce((r,t)=>{if(typeof n[t]?.parse=="function")try{r[t]=n[t].parse(e[t]);}catch(p){throw t=p.cause?.key?`${t}.${p.cause.key}`:t,new Error(`Error parsing key "${t}": ${p.message}`,{cause:{key:t}})}return r},{}):e}),u},string(){return i(a=>typeof a=="string",{type:"string"})},number(){return i(a=>typeof a=="number",{type:"number"})},boolean(){return i(a=>a===true||a===false,{type:"boolean"})},date(){return i(a=>a instanceof Date&&!Number.isNaN(a.getTime()),{type:"date"})},enum(n){let a=e=>n.includes(e),u=e=>`Invalid ${c} value. Expected ${n.map(r=>`"${r}"`).join(" | ")}, received "${e}".`,c="enum";return i(a,{type:c,message:u})},array(n){let u=i(c=>Array.isArray(c),{type:"array"});return s(u,"parse",(c,o)=>(o=c(o),n&&Array.isArray(o)?o.map((e,r)=>{try{return n.parse(e)}catch(t){let p=t.cause?.key?`${r}.${t.cause.key}`:r;throw new Error(`Error parsing index "${r}": ${t.message}`,{cause:{key:p}})}}):o)),u},any(){return i(()=>true)},preprocess(n,a){return s(a,"parse",(u,c)=>(c=n(c),u(c))),a}};function h(n){return a=>`The value "${a}" must be type of ${n} but is type of "${typeof a}".`}function s(n,a,u){let c=n[a];n[a]=(...o)=>u(c,...o);}function i(n,{type:a="any",message:u}={}){u=u||h(a);let c={message:u,type:a},o={parse(e){if(!n(e))throw new Error(u(e));return e},safeParse(e){try{return {success:!0,data:this.parse(e)}}catch(r){return {success:false,error:r}}},transform(e){return s(this,"parse",(r,t)=>e(r(t))),this},optional(){return s(this,"parse",(e,r)=>{try{return e(r)}catch{return}}),this},nullable(){return s(this,"parse",(e,r)=>{try{return e(r)}catch{return null}}),this},nullish(){return s(this,"parse",(e,r)=>{try{return e(r)}catch{return r==null?r:null}}),this},default(e){return s(this,"parse",(r,t)=>(t===void 0&&(t=e,t=typeof e=="function"?e():e),t=r(t),t)),this},pipe(e){return s(this,"parse",(r,t)=>e.parse(r(t))),this},refine(e,{message:r}){return s(this,"parse",(t,p)=>{let f=t(p);if(!e(f))throw new Error(r);return f}),this}};return m.reduce((e,r)=>r(e,n,c)??e,o)}var m=[];function I(n){m.push(n);}export{I as extend,y as s};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@esmj/schema",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Tiny extendable package for schema validation.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"schema",
|
|
@@ -62,17 +62,17 @@
|
|
|
62
62
|
"homepage": "https://github.com/mjancarik/esmj-schema#readme",
|
|
63
63
|
"devDependencies": {
|
|
64
64
|
"@biomejs/biome": "1.9.4",
|
|
65
|
-
"@commitlint/cli": "^19.
|
|
66
|
-
"@commitlint/config-conventional": "^19.
|
|
67
|
-
"@typescript-eslint/eslint-plugin": "^8.
|
|
68
|
-
"@typescript-eslint/parser": "^8.
|
|
65
|
+
"@commitlint/cli": "^19.8.0",
|
|
66
|
+
"@commitlint/config-conventional": "^19.8.0",
|
|
67
|
+
"@typescript-eslint/eslint-plugin": "^8.31.0",
|
|
68
|
+
"@typescript-eslint/parser": "^8.31.0",
|
|
69
69
|
"commitizen": "^4.3.1",
|
|
70
70
|
"conventional-changelog-cli": "^5.0.0",
|
|
71
71
|
"cz-conventional-changelog": "^3.3.0",
|
|
72
72
|
"git-cz": "^4.9.0",
|
|
73
73
|
"husky": "^9.1.7",
|
|
74
|
-
"lint-staged": "^15.
|
|
75
|
-
"prettier": "^3.5.
|
|
76
|
-
"tsup": "^8.
|
|
74
|
+
"lint-staged": "^15.5.1",
|
|
75
|
+
"prettier": "^3.5.3",
|
|
76
|
+
"tsup": "^8.4.0"
|
|
77
77
|
}
|
|
78
78
|
}
|
package/biome.json
DELETED
|
@@ -1,50 +0,0 @@
|
|
|
1
|
-
{
|
|
2
|
-
"files": {
|
|
3
|
-
"include": ["src/**/*.mjs", "src/**/*.ts", "utils/**/*.mjs"],
|
|
4
|
-
"ignore": [".history/**/*"]
|
|
5
|
-
},
|
|
6
|
-
"formatter": {
|
|
7
|
-
"enabled": true,
|
|
8
|
-
"formatWithErrors": true,
|
|
9
|
-
"indentStyle": "space",
|
|
10
|
-
"indentWidth": 2,
|
|
11
|
-
"lineEnding": "lf",
|
|
12
|
-
"lineWidth": 80,
|
|
13
|
-
"attributePosition": "auto"
|
|
14
|
-
},
|
|
15
|
-
"organizeImports": { "enabled": true },
|
|
16
|
-
"linter": {
|
|
17
|
-
"enabled": true,
|
|
18
|
-
"rules": {
|
|
19
|
-
"recommended": true,
|
|
20
|
-
"complexity": {
|
|
21
|
-
"noForEach": "off",
|
|
22
|
-
"noBannedTypes": "off"
|
|
23
|
-
},
|
|
24
|
-
"style": {
|
|
25
|
-
"noParameterAssign": "off"
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
},
|
|
29
|
-
"javascript": {
|
|
30
|
-
"formatter": {
|
|
31
|
-
"jsxQuoteStyle": "double",
|
|
32
|
-
"quoteProperties": "asNeeded",
|
|
33
|
-
"trailingCommas": "all",
|
|
34
|
-
"semicolons": "always",
|
|
35
|
-
"arrowParentheses": "always",
|
|
36
|
-
"bracketSpacing": true,
|
|
37
|
-
"bracketSameLine": false,
|
|
38
|
-
"quoteStyle": "single",
|
|
39
|
-
"attributePosition": "auto"
|
|
40
|
-
}
|
|
41
|
-
},
|
|
42
|
-
"overrides": [
|
|
43
|
-
{
|
|
44
|
-
"include": ["*.json"],
|
|
45
|
-
"formatter": {
|
|
46
|
-
"indentWidth": 2
|
|
47
|
-
}
|
|
48
|
-
}
|
|
49
|
-
]
|
|
50
|
-
}
|