Skip to content

Commit

Permalink
feat: added zod and valibot validators
Browse files Browse the repository at this point in the history
  • Loading branch information
YannicEl committed Aug 22, 2023
1 parent 0ebaac7 commit b7e0b4a
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
4 changes: 3 additions & 1 deletion packages/lib/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@
"@vitejs/plugin-vue": "^4.3.3",
"@vuetils/form": "workspace:*",
"typescript": "^5.1.6",
"valibot": "^0.12.0",
"vite": "^4.4.9",
"vitest": "^0.34.2",
"vue": "^3.3.4",
"vue-tsc": "^1.8.8"
"vue-tsc": "^1.8.8",
"zod": "^3.22.2"
},
"peerDependencies": {
"vue": "3.x"
Expand Down
19 changes: 19 additions & 0 deletions packages/lib/src/validators/validators.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { BaseSchema as ValibotSchema } from 'valibot';
import type { Schema as ZodSchema } from 'zod';
import { defineValidator, defineValidatorWithArgs, isNumber, isString } from './utils';

export const required = defineValidator('required', (value) => !!value);
Expand Down Expand Up @@ -43,3 +45,20 @@ export const email = defineValidator('email', (value) => regex(emailRegex).valid
const emojiRegex = /^(\p{Extended_Pictographic}|\p{Emoji_Component})+$/u;

export const emoji = defineValidator('emoji', (value) => regex(emojiRegex).validate(value));

export const zodValidator = defineValidatorWithArgs(
'zodValidator',
(value, schema: ZodSchema) => schema.safeParse(value).success
);

export const valibotValidator = defineValidatorWithArgs(
'valibotValidator',
(value, schema: ValibotSchema) => {
try {
return !!schema.parse(value);
} catch (error) {
console.log('hi');
return false;
}
}
);
14 changes: 14 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b7e0b4a

Please sign in to comment.