Skip to content

Commit

Permalink
White space strings are no longer considered 0 in number schema
Browse files Browse the repository at this point in the history
  • Loading branch information
Bartmr committed Dec 24, 2021
1 parent e010c0c commit bb370c9
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 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.5",
"version": "4.3.6",
"description": "Easy and type-safe validation",
"main": "lib/index.js",
"types": "lib/types.d.ts",
Expand Down
18 changes: 18 additions & 0 deletions src/schemas/number/number-schema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,22 @@ describe("Number Schema", () => {
value: undefined,
});
});

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

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

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

expect(schema.validate(" ")).toEqual({
errors: false,
value: undefined,
});
});
});
14 changes: 14 additions & 0 deletions src/schemas/number/number-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,20 @@ class NumberSchemaImpl<_Output = number | 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;
}
};
}

integer(message?: string): this {
Expand Down

0 comments on commit bb370c9

Please sign in to comment.