Skip to content

Commit

Permalink
fix: public form validator updates
Browse files Browse the repository at this point in the history
- all validators except for the requiredValidator, allow for blank values i.e. previous `optional` param is now true.
  • Loading branch information
chrisolsen committed Feb 5, 2025
1 parent 7385629 commit 97d937a
Show file tree
Hide file tree
Showing 2 changed files with 107 additions and 22 deletions.
106 changes: 93 additions & 13 deletions libs/common/src/lib/validation.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,97 @@ import {
emailValidator,
postalCodeValidator,
dateValidator,
requiredValidator,
numericValidator,
phoneNumberValidator,
} from "./validators";

describe("Validation", () => {
describe("Required", () => {
const validator = requiredValidator();
it("should pass", () => {
const err = validator("some value");
expect(err).toBeFalsy();
});

it("should fail", () => {
const err = validator("");
expect(err).toBeTruthy();
});
});

describe("Number", () => {
const validator = numericValidator({ min: 1, max: 10 });

[1, 4, 10, "1", "4", "10"].forEach((val) => {
it(`${val} is in the range of 1-10`, () => {
const err = validator(val);
expect(err).toBeFalsy();
});
});

[0, 11, "0", "11"].forEach((val) => {
it(`${val} is out of the range of 1-10`, () => {
const err = validator(val);
expect(err).toBeTruthy();
});
});
});

describe("Date", () => {
const validator = dateValidator({
min: new Date(2025, 0, 1),
max: new Date(2025, 2, 1),
});

[null, new Date(2025, 0, 1), new Date(2025, 2, 1), new Date(2025, 1, 1)].forEach(
(val) => {
it(`${val} is a valid date`, () => {
const err = validator(val);
expect(err).toBeFalsy();
});
},
);

[new Date(2024, 11, 31), new Date(2025, 3, 1)].forEach((val) => {
it(`${val} is an invalid date`, () => {
const err = validator(val);
expect(err).toBeTruthy();
});
});
});

describe("Phone", () => {
const validator = phoneNumberValidator();
const valid = [
"5551234567",
"(555) 1234567",
"555-1234567",
"555 123 4567",
"555-123-4567",
"(555)-123-4567",
"(555) 123-4567",
"(5) 123 4567",
"1-555-123-4567",
"(1) 555-123-4567",
"+1 555-123-4567",
"+1 555 123-4567",
"+1 (555) 123-4567",
];

valid.forEach((val) => {
it(`${val} is valid`, () => {
const err = validator(val);
expect(err).toBeFalsy();
});
});

it("should fail", () => {
const err = validator("123");
expect(err).toBeTruthy();
});
});

describe("Email", () => {
const validEmails = [
"",
Expand Down Expand Up @@ -114,7 +202,7 @@ describe("Validation", () => {
const validValues = ["", "123456"];
const invalidValues = ["12345"];

const validate = lengthValidator({ min: 6, optional: true });
const validate = lengthValidator({ min: 6 });

for (const val of validValues) {
it(`${val} should be valid`, () => {
Expand All @@ -131,10 +219,10 @@ describe("Validation", () => {
}
});

describe("Required", () => {
const validValues = ["123456"];
const invalidValues = ["", "12345"];
const validate = lengthValidator({ min: 6, optional: false });
describe("Length", () => {
const validValues = ["", "123456"];
const invalidValues = ["12345"];
const validate = lengthValidator({ min: 6 });

for (const val of validValues) {
it(`${val} should be valid`, () => {
Expand Down Expand Up @@ -184,12 +272,4 @@ describe("Validation", () => {
//
// })
});

describe("Regex", () => {
it("needs a test");
});

describe("Phone", () => {
it("needs a test");
});
});
23 changes: 14 additions & 9 deletions libs/common/src/lib/validators.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
export type FieldValidator = (value: unknown) => string;
export type FieldsetState = Record<string, FieldsetItemState>;
export type FieldsetItemState = {
name: string;
label: string;
value: string;
};

export type FieldValidator = (value: unknown) => string;
export type FieldsetState = Record<string, FieldsetItemState>;

export class FormValidator {
private readonly validators: Record<string, FieldValidator[]>;

Expand All @@ -24,8 +23,7 @@ export class FormValidator {
Object.entries(this.validators).forEach(([name, validators]) => {
const err = validators
.map((validatorFn) => {
const errMsg = validatorFn(data[name]);
return errMsg;
return validatorFn(data[name]);
})
.find((msg) => !!msg);
if (err) {
Expand Down Expand Up @@ -172,6 +170,11 @@ export function dateValidator({
return (date: unknown) => {
let _date: Date = new Date(0);

// allow empty value
if (`${date || ""}`.length === 0) {
return "";
}

if (typeof date === "string") {
_date = new Date(date);
}
Expand Down Expand Up @@ -212,6 +215,10 @@ export function numericValidator({
return (value: unknown) => {
let _value: number = Number.MAX_VALUE;

// empty value
if (`${value ?? ""}`.length === 0) {
return "";
}
if (typeof value === "string") {
_value = parseFloat(value);
}
Expand Down Expand Up @@ -240,7 +247,6 @@ interface LengthValidatorOptions {
maxMsg?: string;
max?: number;
min?: number;
optional?: boolean;
}

export function lengthValidator({
Expand All @@ -249,11 +255,10 @@ export function lengthValidator({
maxMsg,
min = -Number.MAX_VALUE,
max = Number.MAX_VALUE,
optional,
}: LengthValidatorOptions): FieldValidator {
return (value: unknown) => {
// valid if optional and blank
if (optional && `${value}`.length === 0) {
// valid if blank
if (`${value || ""}`.length === 0) {
return "";
}

Expand Down

0 comments on commit 97d937a

Please sign in to comment.