Skip to content

Commit

Permalink
Require bugfix when the attr is not specified
Browse files Browse the repository at this point in the history
  • Loading branch information
ccoloma committed Jul 7, 2020
1 parent 3d9c0bf commit 3ca5eff
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 3 deletions.
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)) {
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
14 changes: 14 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 worksNotSpecifyingRequiredProp(value: any) {
expect(await ValidationActions.required(value, { })).toBeUndefined();
}
await worksNotSpecifyingRequiredProp(null);
await worksNotSpecifyingRequiredProp(undefined);
await worksNotSpecifyingRequiredProp(false);
await worksNotSpecifyingRequiredProp("foo");
});

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

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

0 comments on commit 3ca5eff

Please sign in to comment.