-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
JB AUBREE
committed
Nov 4, 2024
1 parent
097b745
commit b50325a
Showing
21 changed files
with
264 additions
and
290 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import type { Awaitable, FieldErrors, Form, Validator } from '../types' | ||
import { Joi } from './joi' | ||
import { SuperStruct } from './superstruct' | ||
import { Valibot } from './valibot' | ||
import { Yup } from './yup' | ||
import { Zod } from './zod' | ||
|
||
export const validators: Record<Validator, { | ||
check: (schema: unknown) => boolean | ||
errorCheck?: (error: unknown) => boolean | ||
getErrors: <F extends Form>(schema: any, form: F) => Awaitable<FieldErrors<F>> | ||
}> = { | ||
Joi, | ||
SuperStruct, | ||
Valibot, | ||
Yup, | ||
Zod, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import type { ValidationError as JoiError, Schema as JoiSchema } from 'joi' | ||
import type { FieldErrors, Form } from '../types' | ||
import { isNonNullObject } from '../utils' | ||
|
||
export const Joi = { | ||
check: (schema: unknown): schema is JoiSchema => isNonNullObject(schema) && 'validateAsync' in schema, | ||
errorCheck: (error: unknown): error is JoiError => isNonNullObject(error) && error.isJoi === true, | ||
async getErrors<F extends Form>(schema: JoiSchema, form: F): Promise<FieldErrors<F>> { | ||
const errors: FieldErrors<F> = {} | ||
try { | ||
await schema.validateAsync(form, { abortEarly: false }) | ||
} | ||
catch (error) { | ||
if (Joi.errorCheck(error)) { | ||
error.details.forEach(i => errors[i.path[0] as keyof F] = i.message) | ||
} | ||
} | ||
return errors | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import type { Struct } from 'superstruct' | ||
import type { FieldErrors, Form } from '../types' | ||
import { isNonNullObject } from '../utils' | ||
|
||
export const SuperStruct = { | ||
check: (schema: unknown) => isNonNullObject(schema) && 'validator' in schema, | ||
getErrors<S, F extends Form>(schema: Struct<F, S>, form: F): FieldErrors<F> { | ||
const errors: FieldErrors<F> = {} | ||
const [structError] = schema.validate(form) | ||
structError?.failures().forEach(i => errors[i.path[0] as keyof F] = i.message) | ||
return errors | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import type { BaseIssue, BaseSchema } from 'valibot' | ||
import type { FieldErrors, Form } from '../types' | ||
import { isNonNullObject } from '../utils' | ||
|
||
type ValibotSchema = BaseSchema<unknown, unknown, BaseIssue<unknown>> | ||
|
||
export const Valibot = { | ||
check: (schema: unknown) => isNonNullObject(schema) && '_run' in schema, | ||
getErrors<F extends Form>(schema: ValibotSchema, form: F): FieldErrors<F> { | ||
const errors: FieldErrors<F> = {} | ||
const result = schema._run({ typed: false, value: form }, {}) | ||
result.issues?.forEach(i => errors[i.path?.[0].key as keyof F] = i.message) | ||
return errors | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import type { ObjectSchema, ValidationError as YupError } from 'yup' | ||
import type { FieldErrors, Form } from '../types' | ||
import { isNonNullObject } from '../utils' | ||
|
||
export const Yup = { | ||
check: (schema: unknown) => isNonNullObject(schema) && '__isYupSchema__' in schema, | ||
errorCheck: (error: unknown): error is YupError => isNonNullObject(error) && !!error.inner, | ||
async getErrors<F extends Form>(schema: ObjectSchema<F>, form: F): Promise<FieldErrors<F>> { | ||
const errors: FieldErrors<F> = {} | ||
try { | ||
await schema.validate(form, { abortEarly: false }) | ||
} | ||
catch (error) { | ||
if (Yup.errorCheck(error)) { | ||
error.inner.forEach(i => errors[i.path?.split('.')[0] as keyof F] = i.message) | ||
} | ||
} | ||
return errors | ||
}, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import type { ZodSchema } from 'zod' | ||
import type { FieldErrors, Form } from '../types' | ||
import { isNonNullObject } from '../utils' | ||
|
||
export const Zod = { | ||
check: (schema: unknown): schema is ZodSchema => isNonNullObject(schema) && !!schema.safeParseAsync, | ||
async getErrors<F extends Form>(schema: ZodSchema, form: F): Promise<FieldErrors<F>> { | ||
const errors: FieldErrors<F> = {} | ||
const result = await schema.safeParseAsync(form) | ||
result.error?.issues.forEach(i => errors[i.path[0] as keyof F] = i.message) | ||
return errors | ||
}, | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.