Skip to content

Commit

Permalink
feat: allow validation schema to accept other expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
logaretm committed Jun 12, 2020
1 parent e403731 commit ddeeaea
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 5 deletions.
2 changes: 1 addition & 1 deletion packages/core/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ export interface FieldComposite {
errors: Ref<string[]>;
errorMessage: Ref<string | undefined>;
meta: Record<Flag, Ref<boolean>>;
__setRules(fn: GenericValidateFunction): void;
__setRules(fn: GenericValidateFunction | string | Record<string, any>): void;
}

export interface FormController {
Expand Down
6 changes: 3 additions & 3 deletions packages/core/src/useField.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ export function useField(
): FieldComposite {
const { value, form, immediate, bails, disabled } = normalizeOptions(opts);
const { meta, errors, failedRules, onBlur, handleChange, reset, patch } = useValidationState(value);
let schemaValidation: GenericValidateFunction;
let schemaValidation: GenericValidateFunction | string | Record<string, any>;
const normalizedRules = computed(() => {
return schemaValidation || normalizeRules(unwrap(rules));
return normalizeRules(schemaValidation || unwrap(rules));
});

const runValidation = async (): Promise<ValidationResult> => {
Expand Down Expand Up @@ -79,7 +79,7 @@ export function useField(
handleChange,
onBlur,
disabled,
__setRules(fn: GenericValidateFunction) {
__setRules(fn: GenericValidateFunction | string | Record<string, any>) {
schemaValidation = fn;
},
};
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Flag, FormController, SubmissionHandler, GenericValidateFunction, Field
import { unwrap } from './utils/refs';

interface FormOptions {
validationSchema?: Record<string, GenericValidateFunction>;
validationSchema?: Record<string, GenericValidateFunction | string | Record<string, any>>;
initialValues?: Record<string, any>;
}

Expand Down
35 changes: 35 additions & 0 deletions packages/core/tests/form.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,4 +244,39 @@ describe('<Form />', () => {

expect(submitMock).toHaveBeenCalledTimes(1);
});

test('validation schema to validate form', async () => {
const wrapper = mountWithHoc({
setup() {
const schema = {
field: 'required',
other: 'required',
};

return {
schema,
};
},
template: `
<VForm @submit="submit" as="form" :validationSchema="schema" v-slot="{ errors }">
<Field name="field" as="input" />
<span id="field">{{ errors.field }}</span>
<Field name="other" as="input" />
<span id="other">{{ errors.other }}</span>
<button>Validate</button>
</VForm>
`,
});

const first = wrapper.$el.querySelector('#field');
const second = wrapper.$el.querySelector('#other');

wrapper.$el.querySelector('button').click();
await flushPromises();

expect(first.textContent).toBe(REQUIRED_MESSAGE);
expect(second.textContent).toBe(REQUIRED_MESSAGE);
});
});

0 comments on commit ddeeaea

Please sign in to comment.