-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(core): use formts validation and submit handling (#18)
* feat(core): useFormts validation handling p1 - store errors state as dictionary - expose getter/setter for field errors - expose list of all form errors - expose computed isValid property for fields and form * feat(core): useFormts validation handling p2 call validator when: - field value is set - field is touched - field validate method is called - form validate method is called * feat(core): useFormts submit handling * feat(core): simplify form.reset() and add tests * feat(core): useFormts basic async validation support Co-authored-by: Mikołaj Klaman <mklaman@virtuslab.com>
- Loading branch information
Showing
9 changed files
with
790 additions
and
63 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import { FieldErrors } from "../../types/formts-state"; | ||
|
||
import { resolveIsValid } from "./resolve-is-valid"; | ||
|
||
describe("resolveIsValid", () => { | ||
it("handles primitive field errors", () => { | ||
const errors: FieldErrors<string> = { | ||
foo: "error!", | ||
bar: undefined, | ||
}; | ||
|
||
expect(resolveIsValid(errors, "foo")).toBe(false); | ||
expect(resolveIsValid(errors, "bar")).toBe(true); | ||
expect(resolveIsValid(errors, "baz")).toBe(true); | ||
}); | ||
|
||
it("handles root array field errors", () => { | ||
const errors: FieldErrors<string> = { | ||
array: "error!", | ||
}; | ||
|
||
expect(resolveIsValid(errors, "array")).toBe(false); | ||
expect(resolveIsValid(errors, "array[42]")).toBe(true); | ||
}); | ||
|
||
it("handles array item field errors", () => { | ||
const errors: FieldErrors<string> = { | ||
"array[0]": "error!", | ||
}; | ||
|
||
expect(resolveIsValid(errors, "array")).toBe(false); | ||
expect(resolveIsValid(errors, "array[0]")).toBe(false); | ||
expect(resolveIsValid(errors, "array[42]")).toBe(true); | ||
}); | ||
|
||
it("handles root object field errors", () => { | ||
const errors: FieldErrors<string> = { | ||
object: "error!", | ||
}; | ||
|
||
expect(resolveIsValid(errors, "object")).toBe(false); | ||
expect(resolveIsValid(errors, "object.prop")).toBe(true); | ||
}); | ||
|
||
it("handles object property field errors", () => { | ||
const errors: FieldErrors<string> = { | ||
"object.prop": "error!", | ||
}; | ||
|
||
expect(resolveIsValid(errors, "object")).toBe(false); | ||
expect(resolveIsValid(errors, "object.prop")).toBe(false); | ||
expect(resolveIsValid(errors, "object.otherProp")).toBe(true); | ||
}); | ||
|
||
it("handles nested object and array fields", () => { | ||
const errors: FieldErrors<string> = { | ||
"nested.nestedArr[42].foo": "error!", | ||
}; | ||
|
||
expect(resolveIsValid(errors, "nested")).toBe(false); | ||
expect(resolveIsValid(errors, "nested.nestedArr")).toBe(false); | ||
expect(resolveIsValid(errors, "nested.nestedArr[42]")).toBe(false); | ||
expect(resolveIsValid(errors, "nested.nestedArr[42].foo")).toBe(false); | ||
|
||
expect(resolveIsValid(errors, "nested.otherProp")).toBe(true); | ||
expect(resolveIsValid(errors, "nested.nestedArr[43]")).toBe(true); | ||
expect(resolveIsValid(errors, "nested.nestedArr[42].otherProp")).toBe(true); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { entries } from "../../../utils"; | ||
import { FieldErrors } from "../../types/formts-state"; | ||
|
||
export const resolveIsValid = <Err>( | ||
errors: FieldErrors<Err>, | ||
path: string | ||
): boolean => { | ||
if (errors[path] != null) { | ||
return false; | ||
} | ||
|
||
return not( | ||
entries(errors).some( | ||
([errorPath, error]) => error != null && errorPath.startsWith(path) | ||
) | ||
); | ||
}; | ||
|
||
const not = (bool: boolean) => !bool; |
Oops, something went wrong.