From cb81e9c9d74fb744d13c86676e5d71964b1fd877 Mon Sep 17 00:00:00 2001 From: Michael Shanks Date: Sun, 29 Mar 2020 07:28:44 +0100 Subject: [PATCH] bugfix: datetime fields usable --- packages/core/src/types/datetime.js | 27 ++++++++++++------- .../templateApi.heirarchyValidation.spec.js | 4 +-- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/packages/core/src/types/datetime.js b/packages/core/src/types/datetime.js index 5a88c9a7b2..6f6a6b7958 100644 --- a/packages/core/src/types/datetime.js +++ b/packages/core/src/types/datetime.js @@ -6,7 +6,7 @@ import { parsedSuccess, getDefaultExport, } from "./typeHelpers" -import { switchCase, defaultCase, toDateOrNull } from "../common" +import { switchCase, defaultCase, toDateOrNull, isNonEmptyArray } from "../common" const dateFunctions = typeFunctions({ default: constant(null), @@ -21,23 +21,32 @@ const parseStringToDate = s => [defaultCase, parsedFailed] )(new Date(s)) +const isNullOrEmpty = d => + isNull(d) + || (d || "").toString() === "" + +const isDateOrEmpty = d => + isDate(d) + || isNullOrEmpty(d) + const dateTryParse = switchCase( - [isDate, parsedSuccess], + [isDateOrEmpty, parsedSuccess], [isString, parseStringToDate], - [isNull, parsedSuccess], [defaultCase, parsedFailed] ) const options = { maxValue: { - defaultValue: new Date(32503680000000), - isValid: isDate, + defaultValue: null, + //defaultValue: new Date(32503680000000), + isValid: isDateOrEmpty, requirementDescription: "must be a valid date", parse: toDateOrNull, }, minValue: { - defaultValue: new Date(-8520336000000), - isValid: isDate, + defaultValue: null, + //defaultValue: new Date(-8520336000000), + isValid: isDateOrEmpty, requirementDescription: "must be a valid date", parse: toDateOrNull, }, @@ -46,7 +55,7 @@ const options = { const typeConstraints = [ makerule( async (val, opts) => - val === null || opts.minValue === null || val >= opts.minValue, + val === null || isNullOrEmpty(opts.minValue) || val >= opts.minValue, (val, opts) => `value (${val.toString()}) must be greater than or equal to ${ opts.minValue @@ -54,7 +63,7 @@ const typeConstraints = [ ), makerule( async (val, opts) => - val === null || opts.maxValue === null || val <= opts.maxValue, + val === null || isNullOrEmpty(opts.maxValue) || val <= opts.maxValue, (val, opts) => `value (${val.toString()}) must be less than or equal to ${ opts.minValue diff --git a/packages/core/test/templateApi.heirarchyValidation.spec.js b/packages/core/test/templateApi.heirarchyValidation.spec.js index b016624111..ae535995b9 100644 --- a/packages/core/test/templateApi.heirarchyValidation.spec.js +++ b/packages/core/test/templateApi.heirarchyValidation.spec.js @@ -331,7 +331,7 @@ describe("hierarchy validation", () => { let validationResult = validateAll(hierarchy.root) expectInvalidField(validationResult, "typeOptions.maxValue", invalidField) - invalidField.typeOptions.maxValue = null + invalidField.typeOptions.maxValue = "hello" validationResult = validateAll(hierarchy.root) expectInvalidField(validationResult, "typeOptions.maxValue", invalidField) }) @@ -343,7 +343,7 @@ describe("hierarchy validation", () => { let validationResult = validateAll(hierarchy.root) expectInvalidField(validationResult, "typeOptions.minValue", invalidField) - invalidField.typeOptions.minValue = null + invalidField.typeOptions.minValue = "hello" validationResult = validateAll(hierarchy.root) expectInvalidField(validationResult, "typeOptions.minValue", invalidField) })