1
0
Fork 0
mirror of synced 2024-08-02 03:42:08 +12:00

Fix issue with array field validation

This commit is contained in:
Andrew Kingston 2022-01-18 13:20:28 +00:00
parent c8121ed0e3
commit c3450a3e1e

View file

@ -52,21 +52,29 @@ exports.validate = async ({ appId, tableId, row, table }) => {
const constraints = cloneDeep(table.schema[fieldName].constraints) const constraints = cloneDeep(table.schema[fieldName].constraints)
const type = table.schema[fieldName].type const type = table.schema[fieldName].type
// special case for options, need to always allow unselected (null) // special case for options, need to always allow unselected (null)
if ( if (type === FieldTypes.OPTIONS && constraints.inclusion) {
(type === FieldTypes.OPTIONS || type === FieldTypes.ARRAY) &&
constraints.inclusion
) {
constraints.inclusion.push(null) constraints.inclusion.push(null)
} }
let res let res
// Validate.js doesn't seem to handle array // Validate.js doesn't seem to handle array
if (type === FieldTypes.ARRAY && row[fieldName] && row[fieldName].length) { if (type === FieldTypes.ARRAY) {
row[fieldName].map(val => { const hasValues =
if (!constraints.inclusion.includes(val)) { Array.isArray(row[fieldName]) && row[fieldName].length > 0
errors[fieldName] = "Field not in list"
} // Check values are valid if values are specified
}) if (hasValues) {
row[fieldName].map(val => {
if (!constraints.inclusion.includes(val)) {
errors[fieldName] = "Value not in list"
}
})
}
// Check for required constraint
if (constraints.presence === true && !hasValues) {
errors[fieldName] = "Required field"
}
} else if (type === FieldTypes.JSON && typeof row[fieldName] === "string") { } else if (type === FieldTypes.JSON && typeof row[fieldName] === "string") {
// this should only happen if there is an error // this should only happen if there is an error
try { try {