Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(validation): wrong condition for required field #99

Merged
merged 1 commit into from
Jan 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading