Update validation for array type

This commit is contained in:
Peter Clement 2021-08-26 14:04:18 +01:00
parent 624080800f
commit 16270b46cf
8 changed files with 21 additions and 55 deletions

View File

@ -34,7 +34,7 @@
{:else if type === "boolean"}
<Toggle text={label} bind:value data-cy="{meta.name}-input" />
{:else if type === "array"}
<Multiselect bind:value {label} options={meta.constraints.inclusion.flat()} />
<Multiselect bind:value {label} options={meta.constraints.inclusion} />
{:else if type === "link"}
<LinkedRowSelector bind:linkedRows={value} schema={meta} />
{:else if type === "longform"}

View File

@ -29,7 +29,6 @@
const AUTO_TYPE = "auto"
const FORMULA_TYPE = FIELDS.FORMULA.type
const ARRAY_TYPE = FIELDS.ARRAY.type
const LINK_TYPE = FIELDS.LINK.type
let fieldDefinitions = cloneDeep(FIELDS)
const { hide } = getContext(Context.Modal)
@ -82,12 +81,6 @@
if (field.type === AUTO_TYPE) {
field = buildAutoColumn($tables.draft.name, field.name, field.subtype)
}
if (field.type === ARRAY_TYPE) {
let arr = field.constraints.inclusion
let newArr = []
newArr.push(arr)
field.constraints.inclusion = newArr
}
tables.saveField({
originalName,
field,
@ -272,7 +265,7 @@
{:else if field.type === "array"}
<ValuesList
label="Options (one per line)"
bind:values={field.constraints.inclusion[0]}
bind:values={field.constraints.inclusion}
/>
{:else if field.type === "datetime"}
<DatePicker

View File

@ -83,10 +83,7 @@
const getFieldOptions = field => {
const schema = schemaFields.find(x => x.name === field)
const opt =
schema.type == "array"
? schema?.constraints?.inclusion[0]
: schema?.constraints?.inclusion || []
const opt = schema?.constraints?.inclusion || []
return opt
}
@ -136,7 +133,7 @@
/>
{:else if ["string", "longform", "number"].includes(filter.type)}
<Input disabled={filter.noValue} bind:value={filter.value} />
{:else if filter.type === "options" || filter.type === "array"}
{:else if filter.type === "options"}
<Combobox
disabled={filter.noValue}
options={getFieldOptions(filter.field)}

View File

@ -58,20 +58,12 @@
label: "Must not match regex",
value: "notRegex",
},
ContainsRowID: {
label: "Must contain row ID",
value: "contains",
},
NotContainsRowID: {
label: "Must not contain row ID",
value: "notContains",
},
Contains: {
label: "Must contain one of",
label: "Must contain one",
value: "contains",
},
NotContains: {
label: "Must not contain one of",
label: "Must not contain ",
value: "notContains",
},
}
@ -291,7 +283,7 @@
disabled={rule.constraint === "required"}
on:change={e => (rule.value = e.detail)}
/>
{:else if ["maxLength", "minLength", "regex", "notRegex", "containsRowID", "notContainsRowID"].includes(rule.constraint)}
{:else if ["maxLength", "minLength", "regex", "notRegex"].includes(rule.constraint)}
<!-- Certain constraints always need string values-->
<Input
bind:value={rule.value}
@ -309,7 +301,7 @@
<Multiselect
disabled={rule.constraint === "required"}
options={dataSourceSchema.schema[field].constraints
.inclusion[0]}
.inclusion}
getOptionLabel={x => x}
getOptionValue={x => x}
on:change={e => (rule.value = e.detail)}

View File

@ -27,7 +27,7 @@ export const FIELDS = {
},
},
ARRAY: {
name: "List",
name: "Multi-select",
type: "array",
constraints: {
type: "array",

View File

@ -69,7 +69,7 @@ exports.validate = async ({ appId, tableId, row, table }) => {
// Validate.js doesn't seem to handle array of array very well
if (table.schema[fieldName].type === FieldTypes.ARRAY) {
row[fieldName].map(val => {
if (constraints.inclusion.includes(val)) {
if (!constraints.inclusion.includes(val)) {
errors[fieldName] = "Field not in list"
}
})

View File

@ -9,9 +9,6 @@ export const getOptions = (
const isArray = fieldSchema?.type === "array"
// Take options from schema
if (optionsSource == null || optionsSource === "schema") {
if (isArray) {
return fieldSchema?.constraints?.inclusion[0] ?? []
}
return fieldSchema?.constraints?.inclusion ?? []
}

View File

@ -64,9 +64,8 @@ export const createValidatorFromConstraints = (
// Inclusion constraint
if (
!schemaConstraints.type == "array"
? exists(schemaConstraints.inclusion)
: false
exists(schemaConstraints.inclusion) &&
schemaConstraints.type !== "array"
) {
const options = schemaConstraints.inclusion || []
rules.push({
@ -77,13 +76,11 @@ export const createValidatorFromConstraints = (
})
}
// Handle the array type but link also returns as an array, so handle via the inclusion check
if (
schemaConstraints.type == "array" && schemaConstraints.inclusion
? exists(schemaConstraints.inclusion[0])
: false
schemaConstraints.type === "array" &&
exists(schemaConstraints.inclusion)
) {
const options = schemaConstraints.inclusion[0] || []
const options = schemaConstraints.inclusion || []
rules.push({
type: "array",
constraint: "inclusion",
@ -297,26 +294,18 @@ const notRegexHandler = (value, rule) => {
}
// Evaluates a contains constraint
const containsRowIDHandler = (value, rule) => {
const containsHandler = (value, rule) => {
if (rule.type == "array") {
const expectedValue = parseType(rule.value, "array")
return value && value.some(val => expectedValue.includes(val))
}
const expectedValue = parseType(rule.value, "string")
return value && value.includes(expectedValue)
}
// Evaluates a not contains constraint
const notContainsRowIDHandler = (value, rule) => {
return !containsHandler(value, rule)
}
// Evaluates a contains constraint
const containsHandler = (value, rule) => {
const ruleValue = parseType(rule.value, "array")
return value && value.some(val => ruleValue.includes(val))
}
// Evaluates a not contains constraint
const notContainsHandler = (value, rule) => {
const ruleValue = parseType(rule.value, "array")
return value && !value.some(val => ruleValue.includes(val))
return !containsHandler(value, rule)
}
/**
@ -333,8 +322,6 @@ const handlerMap = {
notEqual: notEqualHandler,
regex: regexHandler,
notRegex: notRegexHandler,
containsRowID: containsRowIDHandler,
notContainsRowID: notContainsRowIDHandler,
contains: containsHandler,
notContains: notContainsHandler,
}