Skip to content

Commit

Permalink
feat: add Zod validation
Browse files Browse the repository at this point in the history
  • Loading branch information
jellydn committed May 26, 2021
1 parent fb7ccbb commit 3835f4c
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 2 deletions.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
"tslib": "2.2.0",
"typedoc": "0.20.36",
"typescript": "4.2.4",
"yup": "0.32.9"
"yup": "0.32.9",
"zod": "^3.1.0"
}
}
7 changes: 7 additions & 0 deletions src/resolvers/zod.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { z } from 'zod';

export function zodResolver<T extends z.ZodAny>(schema: T) {
return {
validate: (data: unknown) => schema.parse(data),
};
}
5 changes: 4 additions & 1 deletion src/validation.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { yupResolver } from './resolvers/yup';
import { fastestValidatorResolver } from './resolvers/fastest-validator';
import { joiResolver } from './resolvers/joi';
import { zodResolver } from './resolvers/zod';

export type SCHEMA_TYPE =
| 'Yup'
| 'FastestValidator'
| 'Joi'
| 'Zod' // TODO add Zod validation
| 'Zod'
| 'SuperStruct' // TODO SuperStruct SuperStruct validation
| 'Vest' // TODO SuperStruct Zod validation
| 'ClassValidator' // TODO SuperStruct ClassValidator validation
Expand All @@ -21,6 +22,8 @@ export function createResolver(type: SCHEMA_TYPE, schema: any) {
return fastestValidatorResolver(schema);
case 'Joi':
return joiResolver(schema);
case 'Zod':
return zodResolver(schema);

default:
throw new Error(`Does not support ${type} validation yet!`);
Expand Down
15 changes: 15 additions & 0 deletions test/__snapshots__/validation.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,18 @@ Object {
`;

exports[`Yup Validation should return false 1`] = `[ValidationError: age is a required field]`;

exports[`Zod Validation should return false 1`] = `
[Error: [
{
"code": "too_small",
"minimum": 8,
"type": "string",
"inclusive": true,
"message": "Should be at least 8 characters",
"path": [
"username"
]
}
]]
`;
37 changes: 37 additions & 0 deletions test/validation.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as yup from 'yup';
import Joi from 'joi';
import { z } from 'zod';

import { createResolver } from '../src/validation';

Expand Down Expand Up @@ -138,3 +139,39 @@ describe('Yoi Validation', () => {
}
});
});

describe('Zod Validation', () => {
it('should create zod resolve base on schema', () => {
try {
createResolver('Zod', {});
} catch (error) {
expect(error).toMatchSnapshot();
}
});

it('should return true', () => {
const schema = z.object({
username: z.string(),
});

const resolver = createResolver('Zod', schema);
const isValid = resolver.validate({
username: 'jellydn',
});
expect(isValid).toBeTruthy();
});

it('should return false', () => {
const schema = z.object({
username: z.string().min(8),
});
const resolver = createResolver('Zod', schema);
try {
resolver.validate({
username: 'jellydn',
});
} catch (error) {
expect(error).toMatchSnapshot();
}
});
});
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -11261,3 +11261,8 @@ [email protected]:
nanoclone "^0.2.1"
property-expr "^2.0.4"
toposort "^2.0.2"

zod@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/zod/-/zod-3.1.0.tgz#b9b6c0f949f9b54eb2c32cbbe81e9d0f24a143d8"
integrity sha512-qS0an8oo9EvVLVqIVxMZrQrfR2pVwBtlPp+BzTB/F19IyPTRaLLoFfdXRzgh626pxFR1efuTWV8bPoEE58KwqA==

0 comments on commit 3835f4c

Please sign in to comment.