Skip to content

Commit

Permalink
feat: added warnings for non existent fields and allow reactive paths
Browse files Browse the repository at this point in the history
  • Loading branch information
logaretm committed Dec 18, 2020
1 parent 8cce17a commit 4182d2f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 16 deletions.
11 changes: 7 additions & 4 deletions packages/vee-validate/src/useIsFieldDirty.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { computed } from 'vue';
import { computed, unref } from 'vue';
import { FormSymbol } from './symbols';
import { injectWithSelf, normalizeField } from './utils';
import { MaybeReactive } from './types';
import { injectWithSelf, normalizeField, warn } from './utils';

/**
* If a field is dirty or not
*/
export function useIsFieldDirty(path: string) {
export function useIsFieldDirty(path: MaybeReactive<string>) {
const form = injectWithSelf(FormSymbol);

return computed(() => {
const field = normalizeField(form?.fields.value[path]);
const field = normalizeField(form?.fields.value[unref(path)]);
if (!field) {
warn(`field with name ${unref(path)} was not found`);

return undefined;
}

Expand Down
11 changes: 7 additions & 4 deletions packages/vee-validate/src/useIsFieldTouched.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { computed } from 'vue';
import { computed, unref } from 'vue';
import { FormSymbol } from './symbols';
import { injectWithSelf, normalizeField } from './utils';
import { MaybeReactive } from './types';
import { injectWithSelf, normalizeField, warn } from './utils';

/**
* If a field is touched or not
*/
export function useIsFieldTouched(path: string) {
export function useIsFieldTouched(path: MaybeReactive<string>) {
const form = injectWithSelf(FormSymbol);

return computed(() => {
const field = normalizeField(form?.fields.value[path]);
const field = normalizeField(form?.fields.value[unref(path)]);
if (!field) {
warn(`field with name ${unref(path)} was not found`);

return undefined;
}

Expand Down
11 changes: 7 additions & 4 deletions packages/vee-validate/src/useIsFieldValid.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
import { computed } from 'vue';
import { computed, unref } from 'vue';
import { FormSymbol } from './symbols';
import { injectWithSelf, normalizeField } from './utils';
import { MaybeReactive } from './types';
import { injectWithSelf, normalizeField, warn } from './utils';

/**
* If a field is validated and is valid
*/
export function useIsFieldValid(path: string) {
export function useIsFieldValid(path: MaybeReactive<string>) {
const form = injectWithSelf(FormSymbol);

return computed(() => {
const field = normalizeField(form?.fields.value[path]);
const field = normalizeField(form?.fields.value[unref(path)]);
if (!field) {
warn(`field with name ${unref(path)} was not found`);

return undefined;
}

Expand Down
10 changes: 6 additions & 4 deletions packages/vee-validate/src/useValidateField.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
import { unref } from 'vue';
import { FormSymbol } from './symbols';
import { ValidationResult } from './types';
import { MaybeReactive, ValidationResult } from './types';
import { injectWithSelf, normalizeField, warn } from './utils';

/**
* Validates a single field
*/
export function useValidateField(path: string) {
export function useValidateField(path: MaybeReactive<string>) {
const form = injectWithSelf(FormSymbol);

return function validateField(): Promise<ValidationResult> {
const field = normalizeField(form?.fields.value[path]);
const field = normalizeField(form?.fields.value[unref(path)]);
if (!field) {
warn(`field with name ${path} was not found`);
warn(`field with name ${unref(path)} was not found`);

return Promise.resolve({
errors: [],
});
Expand Down

0 comments on commit 4182d2f

Please sign in to comment.