Skip to content

Commit

Permalink
refactor!: rename validator to validatorAdapter
Browse files Browse the repository at this point in the history
  • Loading branch information
crutchcorn committed Dec 3, 2023
1 parent 6c85951 commit 256d8cf
Show file tree
Hide file tree
Showing 24 changed files with 72 additions and 72 deletions.
2 changes: 1 addition & 1 deletion docs/guides/validation.md
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ import { z } from "zod";

const form = useForm({
// Either add the validator here or on `Field`
validator: zodValidator,
validatorAdapter: zodValidator,
// ...
});

Expand Down
2 changes: 1 addition & 1 deletion examples/react/valibot/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default function App() {
console.log(values);
},
// Add a validator to support Valibot usage in Form and Field
validator: valibotValidator,
validatorAdapter: valibotValidator,
});

return (
Expand Down
2 changes: 1 addition & 1 deletion examples/react/yup/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default function App() {
console.log(values);
},
// Add a validator to support Yup usage in Form and Field
validator: yupValidator,
validatorAdapter: yupValidator,
});

return (
Expand Down
2 changes: 1 addition & 1 deletion examples/react/zod/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default function App() {
console.log(values);
},
// Add a validator to support Zod usage in Form and Field
validator: zodValidator,
validatorAdapter: zodValidator,
});

return (
Expand Down
2 changes: 1 addition & 1 deletion examples/solid/valibot/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function App() {
console.log(values)
},
// Add a validator to support Valibot usage in Form and Field
validator: valibotValidator,
validatorAdapter: valibotValidator,
}))

return (
Expand Down
2 changes: 1 addition & 1 deletion examples/solid/yup/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function App() {
console.log(values)
},
// Add a validator to support Yup usage in Form and Field
validator: yupValidator,
validatorAdapter: yupValidator,
}))

return (
Expand Down
2 changes: 1 addition & 1 deletion examples/solid/zod/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function App() {
console.log(values)
},
// Add a validator to support Zod usage in Form and Field
validator: zodValidator,
validatorAdapter: zodValidator,
}))

return (
Expand Down
2 changes: 1 addition & 1 deletion examples/vue/valibot/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const form = useForm({
alert(JSON.stringify(values))
},
// Add a validator to support Valibot usage in Form and Field
validator: valibotValidator,
validatorAdapter: valibotValidator,
})
form.provideFormContext()
Expand Down
2 changes: 1 addition & 1 deletion examples/vue/yup/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const form = useForm({
alert(JSON.stringify(values))
},
// Add a validator to support Yup usage in Form and Field
validator: yupValidator,
validatorAdapter: yupValidator,
})
form.provideFormContext()
Expand Down
2 changes: 1 addition & 1 deletion examples/vue/zod/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const form = useForm({
alert(JSON.stringify(values))
},
// Add a validator to support Zod usage in Form and Field
validator: zodValidator,
validatorAdapter: zodValidator,
})
form.provideFormContext()
Expand Down
18 changes: 9 additions & 9 deletions packages/form-core/src/FieldApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export interface FieldOptions<
asyncDebounceMs?: number
asyncAlways?: boolean
preserveValue?: boolean
validator?: ValidatorType
validatorAdapter?: ValidatorType
validators?: FieldValidators<
TParentData,
TName,
Expand Down Expand Up @@ -373,15 +373,15 @@ export class FieldApi<
this.getInfo().validationCount = validationCount

const doValidate = (validate: (typeof validates)[number]['validate']) => {
if (this.options.validator && typeof validate !== 'function') {
return (this.options.validator as Validator<TData>)().validate(
if (this.options.validatorAdapter && typeof validate !== 'function') {
return (this.options.validatorAdapter as Validator<TData>)().validate(
value,
validate,
)
}

if (this.form.options.validator && typeof validate !== 'function') {
return (this.form.options.validator as Validator<TData>)().validate(
if (this.form.options.validatorAdapter && typeof validate !== 'function') {
return (this.form.options.validatorAdapter as Validator<TData>)().validate(
value,
validate,
)
Expand Down Expand Up @@ -508,16 +508,16 @@ export class FieldApi<
}

const doValidate = () => {
if (this.options.validator && typeof validate !== 'function') {
return (this.options.validator as Validator<TData>)().validateAsync(
if (this.options.validatorAdapter && typeof validate !== 'function') {
return (this.options.validatorAdapter as Validator<TData>)().validateAsync(
value,
validate,
)
}

if (this.form.options.validator && typeof validate !== 'function') {
if (this.form.options.validatorAdapter && typeof validate !== 'function') {
return (
this.form.options.validator as Validator<TData>
this.form.options.validatorAdapter as Validator<TData>
)().validateAsync(value, validate)
}

Expand Down
14 changes: 7 additions & 7 deletions packages/form-core/src/FormApi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export type FormOptions<TData, ValidatorType> = {
defaultState?: Partial<FormState<TData>>
asyncAlways?: boolean
asyncDebounceMs?: number
validator?: ValidatorType
validatorAdapter?: ValidatorType
validators?: FormValidators<TData, ValidatorType>
onSubmit?: (
values: TData,
Expand Down Expand Up @@ -188,10 +188,10 @@ export class FormApi<TFormData, ValidatorType> {
mount = () => {
const doValidate = () => {
if (
this.options.validator &&
this.options.validatorAdapter &&
typeof this.options.validators?.onMount !== 'function'
) {
return (this.options.validator as Validator<TFormData>)().validate(
return (this.options.validatorAdapter as Validator<TFormData>)().validate(
this.state.values,
this.options.validators?.onMount,
)
Expand Down Expand Up @@ -283,8 +283,8 @@ export class FormApi<TFormData, ValidatorType> {

const errorMapKey = getErrorMapKey(cause)
const doValidate = () => {
if (this.options.validator && typeof validate !== 'function') {
return (this.options.validator as Validator<TFormData>)().validate(
if (this.options.validatorAdapter && typeof validate !== 'function') {
return (this.options.validatorAdapter as Validator<TFormData>)().validate(
this.state.values,
validate,
)
Expand Down Expand Up @@ -378,8 +378,8 @@ export class FormApi<TFormData, ValidatorType> {
if (typeof validate === 'function') {
return validate(this.state.values, this) as ValidationError
}
if (this.options.validator && typeof validate !== 'function') {
return (this.options.validator as Validator<TFormData>)().validateAsync(
if (this.options.validatorAdapter && typeof validate !== 'function') {
return (this.options.validatorAdapter as Validator<TFormData>)().validateAsync(
this.state.values,
validate,
)
Expand Down
8 changes: 4 additions & 4 deletions packages/valibot-form-adapter/src/tests/FieldApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('valibot field api', () => {

const field = new FieldApi({
form,
validator: valibotValidator,
validatorAdapter: valibotValidator,
name: 'name',
validators: {
onChange: string([
Expand Down Expand Up @@ -44,7 +44,7 @@ describe('valibot field api', () => {

const field = new FieldApi({
form,
validator: valibotValidator,
validatorAdapter: valibotValidator,
name: 'name',
validators: {
onChange: (val) => (val === 'a' ? 'Test' : undefined),
Expand All @@ -69,7 +69,7 @@ describe('valibot field api', () => {

const field = new FieldApi({
form,
validator: valibotValidator,
validatorAdapter: valibotValidator,
name: 'name',
validators: {
onChangeAsync: stringAsync([
Expand Down Expand Up @@ -99,7 +99,7 @@ describe('valibot field api', () => {

const field = new FieldApi({
form,
validator: valibotValidator,
validatorAdapter: valibotValidator,
name: 'name',
validators: {
onChangeAsync: async (val) => (val === 'a' ? 'Test' : undefined),
Expand Down
10 changes: 5 additions & 5 deletions packages/valibot-form-adapter/src/tests/FieldApi.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ it('should allow a Valibot validator to be passed in', () => {
const field = new FieldApi({
form,
name: 'name',
validator: valibotValidator,
validatorAdapter: valibotValidator,
} as const)
})

Expand All @@ -27,7 +27,7 @@ it('should allow a Valibot validator to handle the correct Valibot type', () =>
const field = new FieldApi({
form,
name: 'name',
validator: valibotValidator,
validatorAdapter: valibotValidator,
validators: {
onChange: string(),
},
Expand All @@ -44,7 +44,7 @@ it('should allow a Valibot validator to handle the correct Valibot type for an a
const field = new FieldApi({
form,
name: 'name',
validator: valibotValidator,
validatorAdapter: valibotValidator,
validators: {
onChangeAsync: string(),
},
Expand All @@ -61,7 +61,7 @@ it('should allow a functional onChange to be passed when using a validator', ()
const field = new FieldApi({
form,
name: 'name',
validator: valibotValidator,
validatorAdapter: valibotValidator,
validators: {
onChange: (val) => {
assertType<'test'>(val)
Expand Down Expand Up @@ -97,7 +97,7 @@ it.skip('should allow not a Valibot validator with the wrong Valibot type', () =
const field = new FieldApi({
form,
name: 'name',
validator: valibotValidator,
validatorAdapter: valibotValidator,
validators: {
onChange: object({}),
},
Expand Down
4 changes: 2 additions & 2 deletions packages/valibot-form-adapter/src/tests/FormApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ describe('valibot form api', () => {
defaultValues: {
name: '',
},
validator: valibotValidator,
validatorAdapter: valibotValidator,
})

const field = new FieldApi({
Expand Down Expand Up @@ -39,7 +39,7 @@ describe('valibot form api', () => {
defaultValues: {
name: '',
},
validator: valibotValidator,
validatorAdapter: valibotValidator,
})

const field = new FieldApi({
Expand Down
10 changes: 5 additions & 5 deletions packages/valibot-form-adapter/src/tests/FormApi.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ it('should allow a Valibot validator to be passed in', () => {
defaultValues: {
name: 'test',
},
validator: valibotValidator,
validatorAdapter: valibotValidator,
} as const)
})

Expand All @@ -17,7 +17,7 @@ it('should allow a Valibot validator to handle the correct Valibot type', () =>
defaultValues: {
name: 'test',
},
validator: valibotValidator,
validatorAdapter: valibotValidator,
})

const field = new FieldApi({
Expand All @@ -34,7 +34,7 @@ it('should allow a Valibot validator to handle the correct Valibot type on async
defaultValues: {
name: 'test',
},
validator: valibotValidator,
validatorAdapter: valibotValidator,
})

const field = new FieldApi({
Expand All @@ -51,7 +51,7 @@ it('should allow a functional onChange to be passed when using a validator', ()
defaultValues: {
name: 'test',
},
validator: valibotValidator,
validatorAdapter: valibotValidator,
})

const field = new FieldApi({
Expand Down Expand Up @@ -92,7 +92,7 @@ it.skip('should allow not a Valibot validator with the wrong Valibot type', () =
const field = new FieldApi({
form,
name: 'name',
validator: valibotValidator,
validatorAdapter: valibotValidator,
validators: {
onChange: object({}),
},
Expand Down
8 changes: 4 additions & 4 deletions packages/yup-form-adapter/src/tests/FieldApi.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('yup field api', () => {

const field = new FieldApi({
form,
validator: yupValidator,
validatorAdapter: yupValidator,
name: 'name',
validators: {
onChange: yup.string().min(3, 'You must have a length of at least 3'),
Expand All @@ -42,7 +42,7 @@ describe('yup field api', () => {

const field = new FieldApi({
form,
validator: yupValidator,
validatorAdapter: yupValidator,
name: 'name',
validators: {
onChange: (val) => (val === 'a' ? 'Test' : undefined),
Expand All @@ -67,7 +67,7 @@ describe('yup field api', () => {

const field = new FieldApi({
form,
validator: yupValidator,
validatorAdapter: yupValidator,
name: 'name',
validators: {
onChangeAsync: yup
Expand Down Expand Up @@ -99,7 +99,7 @@ describe('yup field api', () => {

const field = new FieldApi({
form,
validator: yupValidator,
validatorAdapter: yupValidator,
name: 'name',
validators: {
onChangeAsync: async (val) => (val === 'a' ? 'Test' : undefined),
Expand Down
8 changes: 4 additions & 4 deletions packages/yup-form-adapter/src/tests/FieldApi.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ it('should allow a Zod validator to be passed in', () => {
const field = new FieldApi({
form,
name: 'name',
validator: yupValidator,
validatorAdapter: yupValidator,
} as const)
})

Expand All @@ -27,7 +27,7 @@ it('should allow a Zod validator to handle the correct Zod type', () => {
const field = new FieldApi({
form,
name: 'name',
validator: yupValidator,
validatorAdapter: yupValidator,
validators: {
onChange: yup.string(),
},
Expand All @@ -44,7 +44,7 @@ it('should allow a functional onChange to be passed when using a validator', ()
const field = new FieldApi({
form,
name: 'name',
validator: yupValidator,
validatorAdapter: yupValidator,
validators: {
onChange: (val) => {
assertType<'test'>(val)
Expand Down Expand Up @@ -80,7 +80,7 @@ it.skip('should allow not a Zod validator with the wrong Zod type', () => {
const field = new FieldApi({
form,
name: 'name',
validator: yupValidator,
validatorAdapter: yupValidator,
validators: {
onChange: yup.object({}),
},
Expand Down
Loading

0 comments on commit 256d8cf

Please sign in to comment.