Update validation for array type

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

View File

@ -34,7 +34,7 @@
{:else if type === "boolean"} {:else if type === "boolean"}
<Toggle text={label} bind:value data-cy="{meta.name}-input" /> <Toggle text={label} bind:value data-cy="{meta.name}-input" />
{:else if type === "array"} {: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"} {:else if type === "link"}
<LinkedRowSelector bind:linkedRows={value} schema={meta} /> <LinkedRowSelector bind:linkedRows={value} schema={meta} />
{:else if type === "longform"} {:else if type === "longform"}

View File

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

View File

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

View File

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

View File

@ -27,7 +27,7 @@ export const FIELDS = {
}, },
}, },
ARRAY: { ARRAY: {
name: "List", name: "Multi-select",
type: "array", type: "array",
constraints: { constraints: {
type: "array", 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 // Validate.js doesn't seem to handle array of array very well
if (table.schema[fieldName].type === FieldTypes.ARRAY) { if (table.schema[fieldName].type === FieldTypes.ARRAY) {
row[fieldName].map(val => { row[fieldName].map(val => {
if (constraints.inclusion.includes(val)) { if (!constraints.inclusion.includes(val)) {
errors[fieldName] = "Field not in list" errors[fieldName] = "Field not in list"
} }
}) })

View File

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

View File

@ -64,9 +64,8 @@ export const createValidatorFromConstraints = (
// Inclusion constraint // Inclusion constraint
if ( if (
!schemaConstraints.type == "array" exists(schemaConstraints.inclusion) &&
? exists(schemaConstraints.inclusion) schemaConstraints.type !== "array"
: false
) { ) {
const options = schemaConstraints.inclusion || [] const options = schemaConstraints.inclusion || []
rules.push({ 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 ( if (
schemaConstraints.type == "array" && schemaConstraints.inclusion schemaConstraints.type === "array" &&
? exists(schemaConstraints.inclusion[0]) exists(schemaConstraints.inclusion)
: false
) { ) {
const options = schemaConstraints.inclusion[0] || [] const options = schemaConstraints.inclusion || []
rules.push({ rules.push({
type: "array", type: "array",
constraint: "inclusion", constraint: "inclusion",
@ -297,26 +294,18 @@ const notRegexHandler = (value, rule) => {
} }
// Evaluates a contains constraint // 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") const expectedValue = parseType(rule.value, "string")
return value && value.includes(expectedValue) 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 // Evaluates a not contains constraint
const notContainsHandler = (value, rule) => { const notContainsHandler = (value, rule) => {
const ruleValue = parseType(rule.value, "array") return !containsHandler(value, rule)
return value && !value.some(val => ruleValue.includes(val))
} }
/** /**
@ -333,8 +322,6 @@ const handlerMap = {
notEqual: notEqualHandler, notEqual: notEqualHandler,
regex: regexHandler, regex: regexHandler,
notRegex: notRegexHandler, notRegex: notRegexHandler,
containsRowID: containsRowIDHandler,
notContainsRowID: notContainsRowIDHandler,
contains: containsHandler, contains: containsHandler,
notContains: notContainsHandler, notContains: notContainsHandler,
} }