Fix form constraints not being applied correctly

This commit is contained in:
Andrew Kingston 2021-02-04 15:23:26 +00:00
parent 4d9f4cc211
commit d1df8aca41
1 changed files with 8 additions and 7 deletions

View File

@ -1,5 +1,4 @@
import flatpickr from "flatpickr"
import { isEmpty } from "lodash/fp"
export const createValidatorFromConstraints = (constraints, field, table) => {
let checks = []
@ -14,33 +13,33 @@ export const createValidatorFromConstraints = (constraints, field, table) => {
}
// String length constraint
if (constraints.length?.maximum) {
if (exists(constraints.length?.maximum)) {
const length = constraints.length.maximum
checks.push(lengthConstraint(length))
}
// Min / max number constraint
if (!isEmpty(constraints.numericality?.greaterThanOrEqualTo)) {
if (exists(constraints.numericality?.greaterThanOrEqualTo)) {
const min = constraints.numericality.greaterThanOrEqualTo
checks.push(numericalConstraint(x => x >= min, `Minimum value is ${min}`))
}
if (!isEmpty(constraints.numericality?.lessThanOrEqualTo)) {
if (exists(constraints.numericality?.lessThanOrEqualTo)) {
const max = constraints.numericality.lessThanOrEqualTo
checks.push(numericalConstraint(x => x <= max, `Maximum value is ${max}`))
}
// Inclusion constraint
if (!isEmpty(constraints.inclusion)) {
if (exists(constraints.inclusion)) {
const options = constraints.inclusion
checks.push(inclusionConstraint(options))
}
// Date constraint
if (!isEmpty(constraints.datetime?.earliest)) {
if (exists(constraints.datetime?.earliest)) {
const limit = constraints.datetime.earliest
checks.push(dateConstraint(limit, true))
}
if (!isEmpty(constraints.datetime?.latest)) {
if (exists(constraints.datetime?.latest)) {
const limit = constraints.datetime.latest
checks.push(dateConstraint(limit, false))
}
@ -58,6 +57,8 @@ export const createValidatorFromConstraints = (constraints, field, table) => {
}
}
const exists = value => value != null && value !== ""
const presenceConstraint = value => {
let invalid
if (Array.isArray(value)) {