common-resolver
Advanced tools
| export * from '../superstruct/isSuperstructSchema'; | ||
| export * from '../superstruct/superstructParser'; | ||
| export * from '../superstruct/superstructResolver'; |
| export * from '../superstruct/isSuperstructSchema'; | ||
| export * from '../superstruct/superstructParser'; | ||
| export * from '../superstruct/superstructResolver'; |
| export * from '../utils/getResolver'; |
| export * from '../utils/getResolver'; |
| export * from '../yup/isYupSchema'; | ||
| export * from '../yup/yupParser'; | ||
| export * from '../yup/yupResolver'; |
| export * from '../yup/isYupSchema'; | ||
| export * from '../yup/yupParser'; | ||
| export * from '../yup/yupResolver'; |
| export * from '../zod/isZodSchema'; | ||
| export * from '../zod/zodParser'; | ||
| export * from '../zod/zodResolver'; |
| export * from '../zod/isZodSchema'; | ||
| export * from '../zod/zodParser'; | ||
| export * from '../zod/zodResolver'; |
| export declare function isSuperstructSchema<T>(validator: any): validator is import("superstruct").Struct<T, any>; |
| export function isSuperstructSchema(validator) { | ||
| return validator instanceof (require("superstruct").Struct); | ||
| } |
| export declare function superstructParser<T>(data: T, schema: import("superstruct").Struct<T, any>, options?: { | ||
| throwError: boolean; | ||
| }): T; |
| import { commonParser } from "../utils/commonParser"; | ||
| import { superstructResolver } from "./ssResolver"; | ||
| export function superstructParser(data, schema, options) { | ||
| return commonParser(data, schema, superstructResolver, options); | ||
| } |
| import { Resolver } from "../types"; | ||
| export declare function superstructResolver<T>(schema: import("superstruct").Struct<T, any>): Resolver<T>; |
| import { createErrorProxy, errorPathObjectify } from "../utils"; | ||
| export function superstructResolver(schema) { | ||
| const { validate } = require("superstruct"); | ||
| const formatter = (obj) => { | ||
| return obj.reduce((acc, { message, path }) => { | ||
| acc[path.join('.') || "root"] = message; | ||
| return acc; | ||
| }, {}); | ||
| }; | ||
| return { | ||
| validate: (state) => { | ||
| const [error] = validate(state, schema); | ||
| if (!error) { | ||
| return { | ||
| valid: true, | ||
| error: null, | ||
| data: state | ||
| }; | ||
| } | ||
| else { | ||
| return { | ||
| valid: false, | ||
| error: createErrorProxy(errorPathObjectify(formatter(error.failures()))), | ||
| data: null | ||
| }; | ||
| } | ||
| } | ||
| }; | ||
| } |
| export * from './ss/isSuperstructSchema'; | ||
| export * from './ss/ssParser'; | ||
| export * from './ss/ssResolver'; |
| export * from './ss/isSuperstructSchema'; | ||
| export * from './ss/ssParser'; | ||
| export * from './ss/ssResolver'; |
| export declare function isSuperstructSchema<T>(validator: any): validator is import("superstruct").Struct<T, any>; |
| export function isSuperstructSchema(validator) { | ||
| return validator instanceof (require("superstruct").Struct); | ||
| } |
| export declare function superstructParser<T>(data: T, schema: import("superstruct").Struct<T, any>, options?: { | ||
| throwError: boolean; | ||
| }): T; |
| import { commonParser } from "../utils/commonParser"; | ||
| import { superstructResolver } from "./superstructResolver"; | ||
| export function superstructParser(data, schema, options) { | ||
| return commonParser(data, schema, superstructResolver, options); | ||
| } |
| import { Resolver } from "../types"; | ||
| export declare function superstructResolver<T>(schema: import("superstruct").Struct<T, any>): Resolver<T>; |
| import { createErrorProxy, errorPathObjectify } from "../utils"; | ||
| export function superstructResolver(schema) { | ||
| const { validate } = require("superstruct"); | ||
| const formatter = (obj) => { | ||
| return obj.reduce((acc, { message, path }) => { | ||
| acc[path.join('.') || "root"] = message; | ||
| return acc; | ||
| }, {}); | ||
| }; | ||
| return { | ||
| validate: (state) => { | ||
| const [error] = validate(state, schema); | ||
| if (!error) { | ||
| return { | ||
| valid: true, | ||
| error: null, | ||
| data: state | ||
| }; | ||
| } | ||
| else { | ||
| return { | ||
| valid: false, | ||
| error: createErrorProxy(errorPathObjectify(formatter(error.failures()))), | ||
| data: null | ||
| }; | ||
| } | ||
| } | ||
| }; | ||
| } |
| export * from './utils/getResolver'; |
| export * from './utils/getResolver'; |
| export declare function commonParser<T>(data: T, schema: any, resolver: any, options?: { | ||
| throwError: boolean; | ||
| }): T; |
| export function commonParser(data, schema, resolver, options) { | ||
| const result = resolver(schema).validate(data); | ||
| if (!result.valid) { | ||
| if (options?.throwError) { | ||
| throw result.error; | ||
| } | ||
| else { | ||
| return data; | ||
| } | ||
| } | ||
| return data; | ||
| } |
| import { Resolver } from "../types"; | ||
| export type ValidateSchema<T> = { | ||
| zod: import("zod").ZodSchema<T>; | ||
| yup: import("yup").Schema<T>; | ||
| superstruct: import("superstruct").Struct<T, any>; | ||
| }; | ||
| export declare function getResolver<T>(validator: ValidateSchema<T>[keyof ValidateSchema<T>]): Resolver<T>; |
| import { isSuperstructSchema } from "../superstruct/isSuperstructSchema"; | ||
| import { superstructResolver } from "../superstruct/superstructResolver"; | ||
| import { isYupSchema } from "../yup/isYupSchema"; | ||
| import { yupResolver } from "../yup/yupResolver"; | ||
| import { isZodSchema } from "../zod/isZodSchema"; | ||
| import { zodResolver } from "../zod/zodResolver"; | ||
| export function getResolver(validator) { | ||
| if (isZodSchema(validator)) { | ||
| return zodResolver(validator); | ||
| } | ||
| else if (isYupSchema(validator)) { | ||
| return yupResolver(validator); | ||
| } | ||
| else if (isSuperstructSchema(validator)) { | ||
| return superstructResolver(validator); | ||
| } | ||
| else { | ||
| throw new Error("Unsupported validation library"); | ||
| } | ||
| } |
| export * from './zod/isZodSchema'; | ||
| export * from './zod/zodParser'; | ||
| export * from './zod/zodResolver'; |
| export * from './zod/isZodSchema'; | ||
| export * from './zod/zodParser'; | ||
| export * from './zod/zodResolver'; |
| export declare function isYupSchema<T>(validator: any): validator is import("yup").Schema<T>; |
| export function isYupSchema(validator) { | ||
| return validator instanceof (require("yup").Schema); | ||
| } |
| export declare function yupParser<T>(data: T, schema: import("yup").Schema<T>, options?: { | ||
| throwError: boolean; | ||
| }): T; |
| import { commonParser } from "../utils/commonParser"; | ||
| import { yupResolver } from "./yupResolver"; | ||
| export function yupParser(data, schema, options) { | ||
| return commonParser(data, schema, yupResolver, options); | ||
| } |
| import { Resolver } from "../types"; | ||
| export declare function yupResolver<T>(schema: import("yup").Schema<T>): Resolver<T>; |
| import { bracketIndexToDot, createErrorProxy, errorPathObjectify } from "../utils"; | ||
| export function yupResolver(schema) { | ||
| const formatter = (e) => { | ||
| return e.reduce((acc, err) => { | ||
| acc[bracketIndexToDot(err.path) || "root"] = err.errors[0]; | ||
| return acc; | ||
| }, {}); | ||
| }; | ||
| return { | ||
| validate: (state) => { | ||
| try { | ||
| schema.validateSync(state, { abortEarly: false }); | ||
| return { | ||
| valid: true, | ||
| error: null, | ||
| data: state | ||
| }; | ||
| } | ||
| catch (e) { | ||
| return { | ||
| valid: false, | ||
| error: createErrorProxy(errorPathObjectify(formatter(e.inner))), | ||
| data: null | ||
| }; | ||
| } | ||
| } | ||
| }; | ||
| } |
| export * from './zod/isZodSchema'; | ||
| export * from './zod/zodParser'; | ||
| export * from './zod/zodResolver'; |
| export * from './zod/isZodSchema'; | ||
| export * from './zod/zodParser'; | ||
| export * from './zod/zodResolver'; |
| export declare function isZodSchema<T>(validator: any): validator is import("zod").ZodSchema<T>; |
| export function isZodSchema(validator) { | ||
| return validator && typeof validator.parse === 'function' && typeof validator.safeParse === 'function'; | ||
| } |
| export declare function zodParser<T>(data: T, schema: import("zod").ZodSchema<T>, options?: { | ||
| throwError: boolean; | ||
| }): T; |
| import { commonParser } from "../utils/commonParser"; | ||
| import { zodResolver } from "./zodResolver"; | ||
| export function zodParser(data, schema, options) { | ||
| return commonParser(data, schema, zodResolver, options); | ||
| } |
| import { Resolver } from "../types"; | ||
| export declare function zodResolver<T>(schema: import("zod").ZodSchema<T>): Resolver<T>; |
| import { createErrorProxy, errorPathObjectify } from "../utils"; | ||
| export function zodResolver(schema) { | ||
| const formatter = (obj) => { | ||
| return obj.reduce((acc, { message, path }) => { | ||
| acc[path.join('.') || "root"] = message; | ||
| return acc; | ||
| }, {}); | ||
| }; | ||
| return { | ||
| validate: (state) => { | ||
| const result = schema.safeParse(state); | ||
| if (result.success) { | ||
| return { | ||
| valid: true, | ||
| error: null, | ||
| data: state | ||
| }; | ||
| } | ||
| else { | ||
| return { | ||
| valid: false, | ||
| error: createErrorProxy(errorPathObjectify(formatter(result.error.issues))), | ||
| data: null | ||
| }; | ||
| } | ||
| } | ||
| }; | ||
| } |
@@ -1,1 +0,1 @@ | ||
| export { ValidateSchema, Resolver } from "../type"; | ||
| export * from '../types'; |
@@ -1,1 +0,1 @@ | ||
| export {}; | ||
| export * from '../types'; |
+1
-22
@@ -1,22 +0,1 @@ | ||
| import { z } from "zod"; | ||
| import * as yup from "yup"; | ||
| import { Struct } from "superstruct"; | ||
| export type ValidateSchema<T> = { | ||
| zod: z.ZodSchema<T>; | ||
| yup: yup.Schema<T>; | ||
| superstruct: Struct<T, any>; | ||
| }; | ||
| export type RecursivePartial<T> = { | ||
| [P in keyof T]?: T[P] extends object ? RecursivePartial<T[P]> : T[P]; | ||
| }; | ||
| export type Resolver<T> = { | ||
| validate: (state: T, name?: string) => { | ||
| valid: true; | ||
| error: null; | ||
| } | { | ||
| valid: false; | ||
| error: RecursivePartial<T> & { | ||
| "root"?: string; | ||
| }; | ||
| }; | ||
| }; | ||
| export * from './types'; |
+1
-1
@@ -1,1 +0,1 @@ | ||
| export {}; | ||
| export * from './types'; |
@@ -1,12 +0,7 @@ | ||
| import { z } from "zod"; | ||
| import * as yup from "yup"; | ||
| import { Struct } from "superstruct"; | ||
| export type ValidateSchema<T> = { | ||
| zod: z.ZodSchema<T>; | ||
| yup: yup.Schema<T>; | ||
| superstruct: Struct<T, any>; | ||
| }; | ||
| export type RecursivePartial<T> = { | ||
| [P in keyof T]?: T[P] extends object ? RecursivePartial<T[P]> : T[P]; | ||
| [P in keyof T]?: T[P] extends object ? RecursivePartial<T[P]> : string; | ||
| }; | ||
| type CRES<T> = RecursivePartial<T> & { | ||
| "root"?: string; | ||
| }; | ||
| export type Resolver<T> = { | ||
@@ -16,11 +11,9 @@ validate: (state: T, name?: string) => { | ||
| error: null; | ||
| data: T; | ||
| } | { | ||
| valid: false; | ||
| error: RecursivePartial<T> & { | ||
| "root"?: string; | ||
| }; | ||
| error: CRES<T>; | ||
| data: null; | ||
| }; | ||
| }; | ||
| export declare function isZodSchema<T>(validator: any): validator is import("zod").ZodSchema<T>; | ||
| export declare function isYupSchema<T>(validator: any): validator is import("yup").Schema<T>; | ||
| export declare function isSuperstructSchema<T>(validator: any): validator is import("superstruct").Struct<T, any>; | ||
| export {}; |
@@ -1,9 +0,1 @@ | ||
| export function isZodSchema(validator) { | ||
| return validator && typeof validator.parse === 'function' && typeof validator.safeParse === 'function'; | ||
| } | ||
| export function isYupSchema(validator) { | ||
| return validator instanceof (require("yup").Schema); | ||
| } | ||
| export function isSuperstructSchema(validator) { | ||
| return validator instanceof (require("superstruct").Struct); | ||
| } | ||
| export {}; |
+1
-3
@@ -1,3 +0,1 @@ | ||
| export declare function bracketIndexToDot(path: string): string; | ||
| export declare function errorPathObjectify(errors: Record<string, string>): Record<string, any>; | ||
| export declare function createErrorProxy(obj: any, isRoot?: boolean): any; | ||
| export * from './utils/getResolver'; |
+1
-71
@@ -1,71 +0,1 @@ | ||
| export function bracketIndexToDot(path) { | ||
| let newPath = path.replace(/\[(\d+)\]/g, '.$1'); | ||
| newPath = newPath.replace(/\.{2,}/g, '.'); | ||
| if (newPath.startsWith('.')) | ||
| newPath = newPath.slice(1); | ||
| return newPath; | ||
| } | ||
| export function errorPathObjectify(errors) { | ||
| const result = {}; | ||
| for (const key in errors) { | ||
| const value = errors[key]; | ||
| const path = key.split("."); | ||
| let temp = result; | ||
| path.forEach((segment, idx) => { | ||
| if (idx === path.length - 1) { | ||
| if (segment === "root") { | ||
| temp[segment] = value; | ||
| } | ||
| else { | ||
| temp[segment] = { root: value }; | ||
| } | ||
| } | ||
| else { | ||
| if (!temp[segment] || typeof temp[segment] !== "object") { | ||
| temp[segment] = {}; | ||
| } | ||
| temp = temp[segment]; | ||
| } | ||
| }); | ||
| } | ||
| return result; | ||
| } | ||
| export function createErrorProxy(obj, isRoot = true) { | ||
| if (typeof obj !== "object" || obj === null) | ||
| return obj; | ||
| return new Proxy(obj, { | ||
| get(target, prop, receiver) { | ||
| if (isRoot && (prop === Symbol.toStringTag || prop === Symbol.iterator || prop === "constructor")) { | ||
| return Reflect.get(target, prop, receiver); | ||
| } | ||
| if (prop === Symbol.iterator) { | ||
| return undefined; | ||
| } | ||
| if (typeof prop === "string" && Object.prototype.hasOwnProperty.call(target, prop)) { | ||
| const value = target[prop]; | ||
| if (typeof value === "object" && value !== null) { | ||
| if ("root" in value) { | ||
| return value.root; | ||
| } | ||
| return createErrorProxy(value, false); | ||
| } | ||
| return value; | ||
| } | ||
| return Reflect.get(target, prop, receiver); | ||
| }, | ||
| set(target, prop, value, receiver) { | ||
| console.warn(`오류 객체의 "${String(prop)}" 속성은 수정할 수 없습니다.`); | ||
| return false; | ||
| }, | ||
| deleteProperty(target, prop) { | ||
| console.warn(`오류 객체의 "${String(prop)}" 속성은 삭제할 수 없습니다.`); | ||
| return false; | ||
| }, | ||
| ownKeys(target) { | ||
| return Reflect.ownKeys(target); | ||
| }, | ||
| getOwnPropertyDescriptor(target, prop) { | ||
| return Object.getOwnPropertyDescriptor(target, prop); | ||
| } | ||
| }); | ||
| } | ||
| export * from './utils/getResolver'; |
+14
-22
| { | ||
| "name": "common-resolver", | ||
| "version": "0.0.13", | ||
| "version": "0.0.14", | ||
| "scripts": { | ||
@@ -15,16 +15,16 @@ "build": "tsc" | ||
| "./zod": { | ||
| "import": "./dist/exports/zodResolver.js", | ||
| "types": "./dist/exports/zodResolver.d.ts" | ||
| "import": "./dist/exports/zod.js", | ||
| "types": "./dist/exports/zod.d.ts" | ||
| }, | ||
| "./yup": { | ||
| "import": "./dist/exports/yupResolver.js", | ||
| "types": "./dist/exports/yupResolver.d.ts" | ||
| "import": "./dist/exports/yup.js", | ||
| "types": "./dist/exports/yup.d.ts" | ||
| }, | ||
| "./superstruct": { | ||
| "import": "./dist/exports/ssResolver.js", | ||
| "types": "./dist/exports/ssResolver.d.ts" | ||
| "import": "./dist/exports/superstruct.js", | ||
| "types": "./dist/exports/superstruct.d.ts" | ||
| }, | ||
| "./getResolver": { | ||
| "import": "./dist/exports/getResolver.js", | ||
| "types": "./dist/exports/getResolver.d.ts" | ||
| "./utils": { | ||
| "import": "./dist/exports/utils.js", | ||
| "types": "./dist/exports/utils.d.ts" | ||
| }, | ||
@@ -48,13 +48,2 @@ "./types": { | ||
| }, | ||
| "peerDependenciesMeta": { | ||
| "zod": { | ||
| "optional": true | ||
| }, | ||
| "yup": { | ||
| "optional": true | ||
| }, | ||
| "superstruct": { | ||
| "optional": true | ||
| } | ||
| }, | ||
| "devDependencies": { | ||
@@ -70,4 +59,7 @@ "@types/node": "^22.9.0", | ||
| "vite": "^5.2.10", | ||
| "vite-plugin-compression": "^0.5.1" | ||
| "vite-plugin-compression": "^0.5.1", | ||
| "zod": "^3.0.0", | ||
| "yup": "^1.0.0", | ||
| "superstruct": "^2.0.0" | ||
| } | ||
| } |
| import { Resolver, ValidateSchema } from "./types"; | ||
| export declare function getResolver<T>(validator: ValidateSchema<T>[keyof ValidateSchema<T>]): Resolver<T>; | ||
| export declare function zodResolver<T>(schema: ValidateSchema<T>["zod"]): Resolver<T>; | ||
| export declare function yupResolver<T>(schema: ValidateSchema<T>["yup"]): Resolver<T>; | ||
| export declare function superstructResolver<T>(schema: ValidateSchema<T>["superstruct"]): Resolver<T>; |
-92
| import { isZodSchema, isYupSchema, isSuperstructSchema } from "./typeGuards"; | ||
| import { validate } from "superstruct"; | ||
| import { bracketIndexToDot, createErrorProxy, errorPathObjectify } from "./utils"; | ||
| export function getResolver(validator) { | ||
| if (isZodSchema(validator)) { | ||
| return zodResolver(validator); | ||
| } | ||
| else if (isYupSchema(validator)) { | ||
| return yupResolver(validator); | ||
| } | ||
| else if (isSuperstructSchema(validator)) { | ||
| return superstructResolver(validator); | ||
| } | ||
| else { | ||
| throw new Error("Unsupported validation library"); | ||
| } | ||
| } | ||
| export function zodResolver(schema) { | ||
| const formatter = (obj) => { | ||
| return obj.reduce((acc, { message, path }) => { | ||
| acc[path.join('.') || "root"] = message; | ||
| return acc; | ||
| }, {}); | ||
| }; | ||
| return { | ||
| validate: (state) => { | ||
| const result = schema.safeParse(state); | ||
| if (result.success) { | ||
| return { | ||
| valid: true, | ||
| error: null, | ||
| }; | ||
| } | ||
| else { | ||
| return { | ||
| valid: false, | ||
| error: createErrorProxy(errorPathObjectify(formatter(result.error.issues))), | ||
| }; | ||
| } | ||
| } | ||
| }; | ||
| } | ||
| export function yupResolver(schema) { | ||
| const formatter = (e) => { | ||
| return e.reduce((acc, err) => { | ||
| acc[bracketIndexToDot(err.path) || "root"] = err.errors[0]; | ||
| return acc; | ||
| }, {}); | ||
| }; | ||
| return { | ||
| validate: (state) => { | ||
| try { | ||
| schema.validateSync(state, { abortEarly: false }); | ||
| return { | ||
| valid: true, | ||
| error: null, | ||
| }; | ||
| } | ||
| catch (e) { | ||
| return { | ||
| valid: false, | ||
| error: createErrorProxy(errorPathObjectify(formatter(e.inner))) | ||
| }; | ||
| } | ||
| } | ||
| }; | ||
| } | ||
| export function superstructResolver(schema) { | ||
| const formatter = (obj) => { | ||
| return obj.reduce((acc, { message, path }) => { | ||
| acc[path.join('.') || "root"] = message; | ||
| return acc; | ||
| }, {}); | ||
| }; | ||
| return { | ||
| validate: (state) => { | ||
| const [error] = validate(state, schema); | ||
| if (!error) { | ||
| return { | ||
| valid: true, | ||
| error: null, | ||
| }; | ||
| } | ||
| else { | ||
| return { | ||
| valid: false, | ||
| error: createErrorProxy(errorPathObjectify(formatter(error.failures()))), | ||
| }; | ||
| } | ||
| } | ||
| }; | ||
| } |
| export * from "../resolver/getResolver"; |
| export * from "../resolver/getResolver"; |
| export { superstructResolver } from "../resolver/ssResolver"; | ||
| export { isSuperstructSchema } from "../type"; | ||
| export { superstructParser } from "../parser/ssParser"; |
| export { superstructResolver } from "../resolver/ssResolver"; | ||
| export { isSuperstructSchema } from "../type"; | ||
| export { superstructParser } from "../parser/ssParser"; |
| export { yupResolver } from "../resolver/yupResolver"; | ||
| export { isYupSchema } from "../type"; | ||
| export { yupParser } from "../parser/yupParser"; |
| export { yupResolver } from "../resolver/yupResolver"; | ||
| export { isYupSchema } from "../type"; | ||
| export { yupParser } from "../parser/yupParser"; |
| export { zodResolver } from "../resolver/zodResolver"; | ||
| export { isZodSchema } from "../type"; | ||
| export { zodParser } from "../parser/zodParser"; |
| export { zodResolver } from "../resolver/zodResolver"; | ||
| export { isZodSchema } from "../type"; | ||
| export { zodParser } from "../parser/zodParser"; |
| export * from "./core"; |
| export * from "./core"; |
| import { Resolver, ValidateSchema } from "../type"; | ||
| export declare function commonParser<T, K extends ValidateSchema<T>[keyof ValidateSchema<T>]>(data: T, schema: K, resolver: (schema: K) => Resolver<T>, options?: { | ||
| throwError: boolean; | ||
| }): T; |
| export function commonParser(data, schema, resolver, options) { | ||
| const result = resolver(schema).validate(data); | ||
| if (!result.valid) { | ||
| if (options?.throwError) { | ||
| throw result.error; | ||
| } | ||
| else { | ||
| return data; | ||
| } | ||
| } | ||
| return data; | ||
| } |
| import { ValidateSchema } from "../type"; | ||
| export declare function superstructParser<T>(data: T, schema: ValidateSchema<T>["superstruct"], options?: { | ||
| throwError: boolean; | ||
| }): T; |
| import { superstructResolver } from "../resolver/ssResolver"; | ||
| import { commonParser } from "./commonParser"; | ||
| export function superstructParser(data, schema, options) { | ||
| return commonParser(data, schema, superstructResolver, options); | ||
| } |
| import { ValidateSchema } from "../type"; | ||
| export declare function yupParser<T>(data: T, schema: ValidateSchema<T>["yup"], options?: { | ||
| throwError: boolean; | ||
| }): T; |
| import { yupResolver } from "../resolver/yupResolver"; | ||
| import { commonParser } from "./commonParser"; | ||
| export function yupParser(data, schema, options) { | ||
| return commonParser(data, schema, yupResolver, options); | ||
| } |
| import { ValidateSchema } from "../type"; | ||
| export declare function zodParser<T>(data: T, schema: ValidateSchema<T>["zod"], options?: { | ||
| throwError: boolean; | ||
| }): T; |
| import { zodResolver } from "../resolver/zodResolver"; | ||
| import { commonParser } from "./commonParser"; | ||
| export function zodParser(data, schema, options) { | ||
| return commonParser(data, schema, zodResolver, options); | ||
| } |
| import { ValidateSchema, Resolver } from "../type"; | ||
| export declare function getResolver<T>(validator: ValidateSchema<T>[keyof ValidateSchema<T>]): Resolver<T>; |
| import { isZodSchema, isYupSchema, isSuperstructSchema } from "../type"; | ||
| import { superstructResolver } from "./ssResolver"; | ||
| import { yupResolver } from "./yupResolver"; | ||
| import { zodResolver } from "./zodResolver"; | ||
| export function getResolver(validator) { | ||
| if (isZodSchema(validator)) { | ||
| return zodResolver(validator); | ||
| } | ||
| else if (isYupSchema(validator)) { | ||
| return yupResolver(validator); | ||
| } | ||
| else if (isSuperstructSchema(validator)) { | ||
| return superstructResolver(validator); | ||
| } | ||
| else { | ||
| throw new Error("Unsupported validation library"); | ||
| } | ||
| } |
| import { Resolver, ValidateSchema } from "../type"; | ||
| export declare function superstructResolver<T>(schema: ValidateSchema<T>["superstruct"]): Resolver<T>; |
| import { createErrorProxy, errorPathObjectify } from "../utils"; | ||
| export function superstructResolver(schema) { | ||
| const { validate } = require("superstruct"); | ||
| const formatter = (obj) => { | ||
| return obj.reduce((acc, { message, path }) => { | ||
| acc[path.join('.') || "root"] = message; | ||
| return acc; | ||
| }, {}); | ||
| }; | ||
| return { | ||
| validate: (state) => { | ||
| const [error] = validate(state, schema); | ||
| if (!error) { | ||
| return { | ||
| valid: true, | ||
| error: null, | ||
| data: state | ||
| }; | ||
| } | ||
| else { | ||
| return { | ||
| valid: false, | ||
| error: createErrorProxy(errorPathObjectify(formatter(error.failures()))), | ||
| data: null | ||
| }; | ||
| } | ||
| } | ||
| }; | ||
| } |
| import { ValidateSchema, Resolver } from "../type"; | ||
| export declare function yupResolver<T>(schema: ValidateSchema<T>["yup"]): Resolver<T>; |
| import { bracketIndexToDot, createErrorProxy, errorPathObjectify } from "../utils"; | ||
| export function yupResolver(schema) { | ||
| const formatter = (e) => { | ||
| return e.reduce((acc, err) => { | ||
| acc[bracketIndexToDot(err.path) || "root"] = err.errors[0]; | ||
| return acc; | ||
| }, {}); | ||
| }; | ||
| return { | ||
| validate: (state) => { | ||
| try { | ||
| schema.validateSync(state, { abortEarly: false }); | ||
| return { | ||
| valid: true, | ||
| error: null, | ||
| data: state | ||
| }; | ||
| } | ||
| catch (e) { | ||
| return { | ||
| valid: false, | ||
| error: createErrorProxy(errorPathObjectify(formatter(e.inner))), | ||
| data: null | ||
| }; | ||
| } | ||
| } | ||
| }; | ||
| } |
| import { Resolver, ValidateSchema } from "../type"; | ||
| export declare function zodResolver<T>(schema: ValidateSchema<T>["zod"]): Resolver<T>; |
| import { createErrorProxy, errorPathObjectify } from "../utils"; | ||
| export function zodResolver(schema) { | ||
| const formatter = (obj) => { | ||
| return obj.reduce((acc, { message, path }) => { | ||
| acc[path.join('.') || "root"] = message; | ||
| return acc; | ||
| }, {}); | ||
| }; | ||
| return { | ||
| validate: (state) => { | ||
| const result = schema.safeParse(state); | ||
| if (result.success) { | ||
| return { | ||
| valid: true, | ||
| error: null, | ||
| data: state | ||
| }; | ||
| } | ||
| else { | ||
| return { | ||
| valid: false, | ||
| error: createErrorProxy(errorPathObjectify(formatter(result.error.issues))), | ||
| data: null | ||
| }; | ||
| } | ||
| } | ||
| }; | ||
| } |
| export type ValidateSchema<T> = { | ||
| zod: import("zod").ZodSchema<T>; | ||
| yup: import("yup").Schema<T>; | ||
| superstruct: import("superstruct").Struct<T, any>; | ||
| }; | ||
| export type RecursivePartial<T> = { | ||
| [P in keyof T]?: T[P] extends object ? RecursivePartial<T[P]> : string; | ||
| }; | ||
| type CRES<T> = RecursivePartial<T> & { | ||
| "root"?: string; | ||
| }; | ||
| export type Resolver<T> = { | ||
| validate: (state: T, name?: string) => { | ||
| valid: true; | ||
| error: null; | ||
| data: T; | ||
| } | { | ||
| valid: false; | ||
| error: CRES<T>; | ||
| data: null; | ||
| }; | ||
| }; | ||
| export declare function isZodSchema<T>(validator: any): validator is import("zod").ZodSchema<T>; | ||
| export declare function isYupSchema<T>(validator: any): validator is import("yup").Schema<T>; | ||
| export declare function isSuperstructSchema<T>(validator: any): validator is import("superstruct").Struct<T, any>; | ||
| export {}; |
| export function isZodSchema(validator) { | ||
| return validator && typeof validator.parse === 'function' && typeof validator.safeParse === 'function'; | ||
| } | ||
| export function isYupSchema(validator) { | ||
| return validator instanceof (require("yup").Schema); | ||
| } | ||
| export function isSuperstructSchema(validator) { | ||
| return validator instanceof (require("superstruct").Struct); | ||
| } |
| export declare function isZodSchema<T>(validator: any): validator is import("zod").ZodSchema<T>; | ||
| export declare function isYupSchema<T>(validator: any): validator is import("yup").Schema<T>; | ||
| export declare function isSuperstructSchema<T>(validator: any): validator is import("superstruct").Struct<T, any>; |
| export function isZodSchema(validator) { | ||
| return validator && typeof validator.parse === 'function' && typeof validator.safeParse === 'function'; | ||
| } | ||
| export function isYupSchema(validator) { | ||
| return validator instanceof (require("yup").Schema); | ||
| } | ||
| export function isSuperstructSchema(validator) { | ||
| return validator instanceof (require("superstruct").Struct); | ||
| } |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
56
27.27%31928
-16.8%13
30%357
-33.02%1
Infinity%