bugfix: datetime fields usable

This commit is contained in:
Michael Shanks 2020-03-29 07:28:44 +01:00
parent d18fd05022
commit cb81e9c9d7
2 changed files with 20 additions and 11 deletions

View File

@ -6,7 +6,7 @@ import {
parsedSuccess, parsedSuccess,
getDefaultExport, getDefaultExport,
} from "./typeHelpers" } from "./typeHelpers"
import { switchCase, defaultCase, toDateOrNull } from "../common" import { switchCase, defaultCase, toDateOrNull, isNonEmptyArray } from "../common"
const dateFunctions = typeFunctions({ const dateFunctions = typeFunctions({
default: constant(null), default: constant(null),
@ -21,23 +21,32 @@ const parseStringToDate = s =>
[defaultCase, parsedFailed] [defaultCase, parsedFailed]
)(new Date(s)) )(new Date(s))
const isNullOrEmpty = d =>
isNull(d)
|| (d || "").toString() === ""
const isDateOrEmpty = d =>
isDate(d)
|| isNullOrEmpty(d)
const dateTryParse = switchCase( const dateTryParse = switchCase(
[isDate, parsedSuccess], [isDateOrEmpty, parsedSuccess],
[isString, parseStringToDate], [isString, parseStringToDate],
[isNull, parsedSuccess],
[defaultCase, parsedFailed] [defaultCase, parsedFailed]
) )
const options = { const options = {
maxValue: { maxValue: {
defaultValue: new Date(32503680000000), defaultValue: null,
isValid: isDate, //defaultValue: new Date(32503680000000),
isValid: isDateOrEmpty,
requirementDescription: "must be a valid date", requirementDescription: "must be a valid date",
parse: toDateOrNull, parse: toDateOrNull,
}, },
minValue: { minValue: {
defaultValue: new Date(-8520336000000), defaultValue: null,
isValid: isDate, //defaultValue: new Date(-8520336000000),
isValid: isDateOrEmpty,
requirementDescription: "must be a valid date", requirementDescription: "must be a valid date",
parse: toDateOrNull, parse: toDateOrNull,
}, },
@ -46,7 +55,7 @@ const options = {
const typeConstraints = [ const typeConstraints = [
makerule( makerule(
async (val, opts) => async (val, opts) =>
val === null || opts.minValue === null || val >= opts.minValue, val === null || isNullOrEmpty(opts.minValue) || val >= opts.minValue,
(val, opts) => (val, opts) =>
`value (${val.toString()}) must be greater than or equal to ${ `value (${val.toString()}) must be greater than or equal to ${
opts.minValue opts.minValue
@ -54,7 +63,7 @@ const typeConstraints = [
), ),
makerule( makerule(
async (val, opts) => async (val, opts) =>
val === null || opts.maxValue === null || val <= opts.maxValue, val === null || isNullOrEmpty(opts.maxValue) || val <= opts.maxValue,
(val, opts) => (val, opts) =>
`value (${val.toString()}) must be less than or equal to ${ `value (${val.toString()}) must be less than or equal to ${
opts.minValue opts.minValue

View File

@ -331,7 +331,7 @@ describe("hierarchy validation", () => {
let validationResult = validateAll(hierarchy.root) let validationResult = validateAll(hierarchy.root)
expectInvalidField(validationResult, "typeOptions.maxValue", invalidField) expectInvalidField(validationResult, "typeOptions.maxValue", invalidField)
invalidField.typeOptions.maxValue = null invalidField.typeOptions.maxValue = "hello"
validationResult = validateAll(hierarchy.root) validationResult = validateAll(hierarchy.root)
expectInvalidField(validationResult, "typeOptions.maxValue", invalidField) expectInvalidField(validationResult, "typeOptions.maxValue", invalidField)
}) })
@ -343,7 +343,7 @@ describe("hierarchy validation", () => {
let validationResult = validateAll(hierarchy.root) let validationResult = validateAll(hierarchy.root)
expectInvalidField(validationResult, "typeOptions.minValue", invalidField) expectInvalidField(validationResult, "typeOptions.minValue", invalidField)
invalidField.typeOptions.minValue = null invalidField.typeOptions.minValue = "hello"
validationResult = validateAll(hierarchy.root) validationResult = validateAll(hierarchy.root)
expectInvalidField(validationResult, "typeOptions.minValue", invalidField) expectInvalidField(validationResult, "typeOptions.minValue", invalidField)
}) })