Skip to content

Commit

Permalink
fix(validation): wrong condition for required field
Browse files Browse the repository at this point in the history
  • Loading branch information
jsulpis committed Jan 4, 2024
1 parent ea96f7c commit de7182b
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 4 deletions.
65 changes: 65 additions & 0 deletions libs/form-builder/src/lib/utils/__tests__/error.util.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { isStepInError } from '../error.util.ts';

describe('isStepInError', () => {
let schema;
let errors;
let fieldsToCheckByStep;
let dirtyFields;
let defaultValues;

beforeEach(() => {
schema = {
fields: {
myField: {
id: 'myField',
type: 'text',
validation: {
required: {
value: false,
key: 'required',
message: 'please fill this field',
},
},
},
},
stepsById: ['step1'],
steps: {
step1: {
id: 'step1',
fieldsById: ['myField'],
submit: { label: 'submit' },
},
},
};
errors = {};
fieldsToCheckByStep = ['myField'];
dirtyFields = {};
defaultValues = {};
});

it('should return false if the field is empty and not required', () => {
const isInError = isStepInError({
schema,
errors,
fieldsToCheckByStep,
dirtyFields,
defaultValues,
});

expect(isInError).toBe(false);
});

it('should return true if the field is empty and required', () => {
schema.fields.myField.validation.required.value = true;

const isInError = isStepInError({
schema,
errors,
fieldsToCheckByStep,
dirtyFields,
defaultValues,
});

expect(isInError).toBe(true);
});
});
8 changes: 4 additions & 4 deletions libs/form-builder/src/lib/utils/error.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ export const getFieldsToCheckByStep = ({
return fieldsToCheck;
};

export const isFieldInError = ({ fieldToCheck, errors }: { fieldToCheck: string; errors: FieldErrors }) =>
const isFieldInError = ({ fieldToCheck, errors }: { fieldToCheck: string; errors: FieldErrors }) =>
!!(errors && errors[fieldToCheck]);

export const isFieldRequired = ({ schema, fieldToCheck }: { schema: FormSchema; fieldToCheck: string }) =>
schema?.fields?.[fieldToCheck]?.validation?.[DEFAULT_RULES_NAMES.required];
const isFieldRequired = ({ schema, fieldToCheck }: { schema: FormSchema; fieldToCheck: string }): boolean =>
!!schema?.fields?.[fieldToCheck]?.validation?.[DEFAULT_RULES_NAMES.required]?.value;

export const isFieldNotDirtyAndEmpty = ({
const isFieldNotDirtyAndEmpty = ({
fieldToCheck,
dirtyFields,
defaultValues,
Expand Down

0 comments on commit de7182b

Please sign in to comment.