Skip to content

Commit

Permalink
feat: Remove yup type dependency (#3704)
Browse files Browse the repository at this point in the history
  • Loading branch information
logaretm committed Jul 3, 2022
1 parent 3b50d89 commit e772f9a
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 25 deletions.
12 changes: 9 additions & 3 deletions packages/vee-validate/src/types.ts
@@ -1,5 +1,4 @@
import { ComputedRef, Ref } from 'vue';
import { SchemaOf, AnySchema, AnyObjectSchema } from 'yup';
import { FieldValidationMetaInfo } from '../../shared';

export type GenericFormValues = Record<string, unknown>;
Expand All @@ -9,7 +8,14 @@ export interface ValidationResult {
valid: boolean;
}

export type YupValidator = AnySchema | AnyObjectSchema;
export type YupValidator<TValue = any> = { validate(value: TValue, options: Record<string, any>): Promise<TValue> };

export interface YupValidationError extends Error {
value: any;
path?: string;
errors: string[];
inner: YupValidationError[];
}

export type Locator = { __locatorRef: string } & ((values: GenericFormValues) => unknown);

Expand Down Expand Up @@ -178,7 +184,7 @@ export interface PrivateFormContext<TValues extends Record<string, any> = Record
fieldsByPath: Ref<FieldPathLookup>;
fieldArrays: PrivateFieldArrayContext[];
submitCount: Ref<number>;
schema?: MaybeRef<RawFormSchema<TValues> | SchemaOf<TValues> | undefined>;
schema?: MaybeRef<RawFormSchema<TValues> | YupValidator<TValues> | undefined>;
errorBag: Ref<FormErrorBag<TValues>>;
errors: ComputedRef<FormErrors<TValues>>;
meta: ComputedRef<FormMeta<TValues>>;
Expand Down
4 changes: 1 addition & 3 deletions packages/vee-validate/src/useField.ts
Expand Up @@ -10,7 +10,6 @@ import {
nextTick,
getCurrentInstance,
} from 'vue';
import { BaseSchema } from 'yup';
import { klona as deepCopy } from 'klona/full';
import isEqual from 'fast-deep-equal/es6';
import { validate as validateValue } from './validate';
Expand Down Expand Up @@ -59,8 +58,7 @@ export type RuleExpression<TValue> =
| Record<string, unknown>
| GenericValidateFunction
| GenericValidateFunction[]
| YupValidator
| BaseSchema<TValue>
| YupValidator<TValue>
| undefined;

/**
Expand Down
3 changes: 1 addition & 2 deletions packages/vee-validate/src/useForm.ts
Expand Up @@ -14,7 +14,6 @@ import {
watchEffect,
} from 'vue';
import isEqual from 'fast-deep-equal/es6';
import type { SchemaOf } from 'yup';
import { klona as deepCopy } from 'klona/full';
import {
FieldMeta,
Expand Down Expand Up @@ -55,7 +54,7 @@ import { _useFieldValue } from './useFieldState';

interface FormOptions<TValues extends Record<string, any>> {
validationSchema?: MaybeRef<
Record<keyof TValues, GenericValidateFunction | string | Record<string, any>> | SchemaOf<TValues> | undefined
Record<keyof TValues, GenericValidateFunction | string | Record<string, any>> | any | undefined
>;
initialValues?: MaybeRef<TValues>;
initialErrors?: Record<keyof TValues, string | undefined>;
Expand Down
18 changes: 12 additions & 6 deletions packages/vee-validate/src/validate.ts
@@ -1,8 +1,14 @@
import type { SchemaOf, ValidationError } from 'yup';
import { resolveRule } from './defineRule';
import { isLocator, normalizeRules, isYupValidator, keysOf, getFromPath } from './utils';
import { getConfig } from './config';
import { ValidationResult, GenericValidateFunction, YupValidator, FormValidationResult, RawFormSchema } from './types';
import {
ValidationResult,
GenericValidateFunction,
YupValidator,
FormValidationResult,
RawFormSchema,
YupValidationError,
} from './types';
import { isCallable, FieldValidationMetaInfo } from '../../shared';

/**
Expand Down Expand Up @@ -137,7 +143,7 @@ async function validateFieldWithYup(value: unknown, validator: YupValidator, opt
abortEarly: opts.bails ?? true,
})
.then(() => [])
.catch((err: ValidationError) => {
.catch((err: YupValidationError) => {
// Yup errors have a name prop one them.
// https://github.com/jquense/yup#validationerrorerrors-string--arraystring-value-any-path-string
if (err.name === 'ValidationError') {
Expand Down Expand Up @@ -223,13 +229,13 @@ function fillTargetValues(params: unknown[] | Record<string, unknown>, crossTabl
}

export async function validateYupSchema<TValues>(
schema: SchemaOf<TValues>,
schema: YupValidator<TValues>,
values: TValues
): Promise<FormValidationResult<TValues>> {
const errorObjects: ValidationError[] = await (schema as YupValidator)
const errorObjects: YupValidationError[] = await (schema as YupValidator)
.validate(values, { abortEarly: false })
.then(() => [])
.catch((err: ValidationError) => {
.catch((err: YupValidationError) => {
// Yup errors have a name prop one them.
// https://github.com/jquense/yup#validationerrorerrors-string--arraystring-value-any-path-string
if (err.name !== 'ValidationError') {
Expand Down
9 changes: 4 additions & 5 deletions packages/zod/src/index.ts
@@ -1,13 +1,12 @@
import type { ZodObject, ZodType, ZodTypeDef, TypeOf as ZodTypeOf, ZodRawShape, ZodEffects } from 'zod';
import type { BaseSchema, SchemaOf } from 'yup';
import { isIndex } from '../../shared';

/**
* Transforms a Zod's base type schema to yup's base type schema
*/
export function toFieldValidator<TValue = unknown, TDef extends ZodTypeDef = ZodTypeDef, TInput = TValue>(
zodSchema: ZodType<TValue, TDef, TInput> | ZodEffects<ZodType<TValue, TDef, TInput>>
): BaseSchema<TValue> {
): any {
return {
async validate(value: TValue) {
const result = await zodSchema.safeParseAsync(value);
Expand All @@ -21,7 +20,7 @@ export function toFieldValidator<TValue = unknown, TDef extends ZodTypeDef = Zod

throw error;
},
} as BaseSchema<TValue>;
} as any;
}

interface AggregatedZodError {
Expand All @@ -39,7 +38,7 @@ type ToBaseTypes<TShape extends ZodRawShape> = {
export function toFormValidator<
TShape extends ZodRawShape,
TValues extends Record<string, unknown> = ToBaseTypes<TShape>
>(zodSchema: ZodObject<TShape> | ZodEffects<ZodObject<TShape>>): SchemaOf<TValues> {
>(zodSchema: ZodObject<TShape> | ZodEffects<ZodObject<TShape>>) {
return {
async validate(value: TValues) {
const result = await zodSchema.safeParseAsync(value);
Expand All @@ -57,7 +56,7 @@ export function toFormValidator<

throw error;
},
} as SchemaOf<TValues>;
} as any;
}

/**
Expand Down
12 changes: 6 additions & 6 deletions yarn.lock
Expand Up @@ -292,9 +292,9 @@
"@babel/helper-plugin-utils" "^7.14.5"

"@babel/runtime@^7.15.4":
version "7.15.4"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.15.4.tgz#fd17d16bfdf878e6dd02d19753a39fa8a8d9c84a"
integrity sha512-99catp6bHCaxr4sJ/DbTGgHS4+Rs2RVd2g7iOap6SLGPDknRK9ztKNsE/Fg6QhSeh1FGE5f6gHGQmvvn3I3xhw==
version "7.17.2"
resolved "https://registry.yarnpkg.com/@babel/runtime/-/runtime-7.17.2.tgz#66f68591605e59da47523c631416b18508779941"
integrity sha512-hzeyJyMA1YGdJTuWU0e/j4wKXrU4OMFvY2MSlaI9B7VQb0r5cxTE3EAIS2Q7Tn2RIcDkRvTA/v2JsAEhxe99uw==
dependencies:
regenerator-runtime "^0.13.4"

Expand Down Expand Up @@ -1690,9 +1690,9 @@
integrity sha1-7ihweulOEdK4J7y+UnC86n8+ce4=

"@types/lodash@^4.14.175":
version "4.14.175"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.175.tgz#b78dfa959192b01fae0ad90e166478769b215f45"
integrity sha512-XmdEOrKQ8a1Y/yxQFOMbC47G/V2VDO1GvMRnl4O75M4GW/abC5tnfzadQYkqEveqRM1dEJGFFegfPNA2vvx2iw==
version "4.14.178"
resolved "https://registry.yarnpkg.com/@types/lodash/-/lodash-4.14.178.tgz#341f6d2247db528d4a13ddbb374bcdc80406f4f8"
integrity sha512-0d5Wd09ItQWH1qFbEyQ7oTQ3GZrMfth5JkbN3EvTKLXcHLRDSXeLnlvlOn0wvxVIwK5o2M8JzP/OWz7T3NRsbw==

"@types/minimatch@^3.0.3":
version "3.0.3"
Expand Down

0 comments on commit e772f9a

Please sign in to comment.