Fix form validation sometimes being applied erroneously

This commit is contained in:
Andrew Kingston 2021-02-03 10:05:32 +00:00
parent 3206b8dc14
commit 9ff40cc288
1 changed files with 6 additions and 5 deletions

View File

@ -1,4 +1,5 @@
import flatpickr from "flatpickr" import flatpickr from "flatpickr"
import { isEmpty } from "lodash/fp"
export const createValidatorFromConstraints = (constraints, field, table) => { export const createValidatorFromConstraints = (constraints, field, table) => {
let checks = [] let checks = []
@ -19,27 +20,27 @@ export const createValidatorFromConstraints = (constraints, field, table) => {
} }
// Min / max number constraint // Min / max number constraint
if (constraints.numericality?.greaterThanOrEqualTo != null) { if (!isEmpty(constraints.numericality?.greaterThanOrEqualTo)) {
const min = constraints.numericality.greaterThanOrEqualTo const min = constraints.numericality.greaterThanOrEqualTo
checks.push(numericalConstraint(x => x >= min, `Minimum value is ${min}`)) checks.push(numericalConstraint(x => x >= min, `Minimum value is ${min}`))
} }
if (constraints.numericality?.lessThanOrEqualTo != null) { if (!isEmpty(constraints.numericality?.lessThanOrEqualTo)) {
const max = constraints.numericality.lessThanOrEqualTo const max = constraints.numericality.lessThanOrEqualTo
checks.push(numericalConstraint(x => x <= max, `Maximum value is ${max}`)) checks.push(numericalConstraint(x => x <= max, `Maximum value is ${max}`))
} }
// Inclusion constraint // Inclusion constraint
if (constraints.inclusion != null) { if (!isEmpty(constraints.inclusion)) {
const options = constraints.inclusion const options = constraints.inclusion
checks.push(inclusionConstraint(options)) checks.push(inclusionConstraint(options))
} }
// Date constraint // Date constraint
if (constraints.datetime?.earliest != null) { if (!isEmpty(constraints.datetime?.earliest)) {
const limit = constraints.datetime.earliest const limit = constraints.datetime.earliest
checks.push(dateConstraint(limit, true)) checks.push(dateConstraint(limit, true))
} }
if (constraints.datetime?.latest != null) { if (!isEmpty(constraints.datetime?.latest)) {
const limit = constraints.datetime.latest const limit = constraints.datetime.latest
checks.push(dateConstraint(limit, false)) checks.push(dateConstraint(limit, false))
} }