Improve nullable, optional, and shape
This commit is contained in:
parent
77f0afe6e3
commit
fced3e7c65
3 changed files with 64 additions and 3 deletions
|
@ -157,6 +157,56 @@ describe(Parse.shape, () => {
|
|||
});
|
||||
});
|
||||
|
||||
test("allows nullable entries", () => {
|
||||
const parseThing = Parse.shape({
|
||||
name: Parse.string,
|
||||
another: Parse.nullable(Parse.string),
|
||||
});
|
||||
|
||||
expect(parseThing({ name: "hello", another: null })).toEqual({
|
||||
name: "hello",
|
||||
another: null,
|
||||
});
|
||||
});
|
||||
|
||||
test("nullable entries can accpet undefined when configured as such", () => {
|
||||
const parseThing = Parse.shape({
|
||||
name: Parse.string,
|
||||
another: Parse.nullable(Parse.string, true),
|
||||
});
|
||||
|
||||
const obj = {
|
||||
name: "hello",
|
||||
another: undefined,
|
||||
};
|
||||
|
||||
const result = parseThing(obj);
|
||||
|
||||
expect(result).toEqual({
|
||||
name: "hello",
|
||||
another: null,
|
||||
});
|
||||
});
|
||||
|
||||
test("optional entries can accpet null when configured as such", () => {
|
||||
const parseThing = Parse.shape({
|
||||
name: Parse.string,
|
||||
another: Parse.optional(Parse.string, true),
|
||||
});
|
||||
|
||||
const obj = {
|
||||
name: "hello",
|
||||
another: null,
|
||||
};
|
||||
|
||||
const result = parseThing(obj);
|
||||
|
||||
expect(result).toEqual({
|
||||
name: "hello",
|
||||
another: undefined,
|
||||
});
|
||||
});
|
||||
|
||||
test("throws when a required value is missing", () => {
|
||||
const parseThing = Parse.shape({
|
||||
name: Parse.string,
|
||||
|
@ -195,12 +245,20 @@ describe(Parse.optional, () => {
|
|||
test("Parses undefined values", () => {
|
||||
expect(Parse.optional(Parse.string)(undefined)).toBeUndefined();
|
||||
});
|
||||
|
||||
test("Parses null values when configured", () => {
|
||||
expect(Parse.optional(Parse.string, true)(null)).toBeUndefined();
|
||||
});
|
||||
});
|
||||
|
||||
describe(Parse.nullable, () => {
|
||||
test("Parses null values", () => {
|
||||
expect(Parse.nullable(Parse.string)(null)).toBeNull();
|
||||
});
|
||||
|
||||
test("Parses undefined values when configured", () => {
|
||||
expect(Parse.nullable(Parse.string, true)(undefined)).toBeNull();
|
||||
});
|
||||
});
|
||||
|
||||
describe(Parse.defaulted, () => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue