Skip to content

Commit

Permalink
White space strings are transformed to undefined on schemas that pars…
Browse files Browse the repository at this point in the history
…e strings
  • Loading branch information
Bartmr committed Feb 4, 2022
1 parent c9eb2e6 commit ba21121
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 3 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "not-me",
"version": "4.3.6",
"version": "4.3.7",
"description": "Easy and type-safe validation",
"main": "lib/index.js",
"types": "lib/types.d.ts",
Expand Down
22 changes: 22 additions & 0 deletions src/schemas/boolean/boolean-schema.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Schema } from "../schema";
import { boolean } from "./boolean-schema";

describe("Date Schema", () => {
it("Empty strings should be transformed to undefined", () => {
const schema: Schema<boolean | undefined | null> = boolean();

expect(schema.validate("")).toEqual({
errors: false,
value: undefined,
});
});

it("Strings with only whitespaces should be transformed to undefined", () => {
const schema: Schema<boolean | undefined | null> = boolean();

expect(schema.validate(" ")).toEqual({
errors: false,
value: undefined,
});
});
});
14 changes: 14 additions & 0 deletions src/schemas/boolean/boolean-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ class BooleanSchemaImpl<
};
}
});

this.wrapValueBeforeValidation = (input) => {
if (typeof input === "string") {
const trimmed = input.trim();

if (!trimmed) {
return undefined;
} else {
return trimmed;
}
} else {
return input;
}
};
}

required(message?: string): BooleanSchemaImpl<boolean> {
Expand Down
18 changes: 18 additions & 0 deletions src/schemas/date/date-schema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,22 @@ describe("Date Schema", () => {
expect(result.value.getTime()).toBe(dateInput.getTime());
}
});

it("Empty strings should be transformed to undefined", () => {
const schema: Schema<Date | undefined | null> = date();

expect(schema.validate("")).toEqual({
errors: false,
value: undefined,
});
});

it("Strings with only whitespaces should be transformed to undefined", () => {
const schema: Schema<Date | undefined | null> = date();

expect(schema.validate(" ")).toEqual({
errors: false,
value: undefined,
});
});
});
14 changes: 14 additions & 0 deletions src/schemas/date/date-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,20 @@ class DateSchemaImpl<_Output = Date | undefined | null> extends BaseSchema<
};
}
});

this.wrapValueBeforeValidation = (input) => {
if (typeof input === "string") {
const trimmed = input.trim();

if (!trimmed) {
return undefined;
} else {
return trimmed;
}
} else {
return input;
}
};
}

required(message?: string): DateSchemaImpl<Date> {
Expand Down
2 changes: 1 addition & 1 deletion src/schemas/number/number-schema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ describe("Number Schema", () => {
});
});

it("Strings with whitespaces should be transformed to undefined", () => {
it("Strings with only whitespaces should be transformed to undefined", () => {
const schema: Schema<number | undefined | null> = number();

expect(schema.validate(" ")).toEqual({
Expand Down

0 comments on commit ba21121

Please sign in to comment.