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

Require bugfix when the attr is not specified #9

Merged
merged 2 commits into from
Jul 23, 2020
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
6 changes: 3 additions & 3 deletions src/core/ValidationActions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isNullOrUndefined, isBlank, isFalse } from "./utils";
import { isNullOrUndefined, isBlank, isFalse, hasProperty } from "./utils";
import { BoundComponentProps } from "../components/InputElements";

let re_weburl: RegExp;
Expand Down Expand Up @@ -39,7 +39,7 @@ export const ValidationActions = {
value: any,
props: BoundComponentProps
): Promise<ValidationResult> {
if (isBlank(value) && !isFalse(props.required)) {
if (isBlank(value) && hasProperty(props, 'required') && !isFalse(props.required)) {
return "required";
}
},
Expand All @@ -48,7 +48,7 @@ export const ValidationActions = {
value: number,
props: BoundComponentProps
): Promise<ValidationResult> {
if (isNullOrUndefined(value) && !isFalse(props.required)) {
if (isNullOrUndefined(value) && hasProperty(props, 'required') && !isFalse(props.required)) {
icoloma marked this conversation as resolved.
Show resolved Hide resolved
return "required";
}
},
Expand Down
4 changes: 4 additions & 0 deletions src/core/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ export function isFalse(value: boolean): boolean {
return value === false;
}

export function hasProperty(obj: object, property: string): boolean {
return property in obj;
}

/**
* Sets a nested property such as "foo.bar.baz", initializing intermediate nodes if needed
*/
Expand Down
16 changes: 16 additions & 0 deletions test/ValidationActionsTest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ describe("ValidationActions", function () {
await failRequired(false);
await failRequired("");
await failRequired(" \t");
async function passRequired(value: any) {
expect(await ValidationActions.required(value, {})).toBeUndefined();
}
await passRequired(null);
await passRequired(undefined);
await passRequired(false);
await passRequired("foo");
});

it("number.required should reject null and undefined, but acept 0", async function () {
Expand All @@ -31,6 +38,15 @@ describe("ValidationActions", function () {
).toBeUndefined();
await failRequired(null);
await failRequired(undefined);
async function passRequired(value: any) {
expect(
await ValidationActions.number_required(value, {})
).toBeUndefined();
}
await passRequired(null);
await passRequired(undefined);
await passRequired(false);
await passRequired(0);
});

it("Number min value", async function () {
Expand Down