Browse Source

Merge pull request #4325 from finn-auto/fix-column-validations

Fix column validations
pull/4367/head
Pranav C 2 years ago committed by GitHub
parent
commit
a091fd7514
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      packages/nc-gui/composables/useColumnCreateStore.ts
  2. 16
      packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/BaseModelSqlv2.ts

4
packages/nc-gui/composables/useColumnCreateStore.ts

@ -193,6 +193,10 @@ const [useProvideColumnCreateStore, useColumnCreateStore] = createInjectionState
formState.value.table_name = meta.value?.table_name formState.value.table_name = meta.value?.table_name
// formState.value.title = formState.value.column_name // formState.value.title = formState.value.column_name
if (column.value) { if (column.value) {
// reset column validation if column is not to be validated
if (!columnToValidate.includes(formState.value.uidt)) {
formState.value.validate = ''
}
await $api.dbTableColumn.update(column.value?.id as string, formState.value) await $api.dbTableColumn.update(column.value?.id as string, formState.value)
// Column updated // Column updated
message.success(t('msg.success.columnUpdated')) message.success(t('msg.success.columnUpdated'))

16
packages/nocodb/src/lib/db/sql-data-mapper/lib/sql/BaseModelSqlv2.ts

@ -2236,11 +2236,13 @@ class BaseModelSqlv2 {
for (let i = 0; i < this.model.columns.length; ++i) { for (let i = 0; i < this.model.columns.length; ++i) {
const column = this.model.columns[i]; const column = this.model.columns[i];
// skip validation if `validate` is undefined or false // skip validation if `validate` is undefined or false
if (!column?.meta?.validate) continue; if (!column?.meta?.validate && !column?.validate) continue;
const validate = column.getValidators(); const validate = column.getValidators();
const cn = column.column_name; const cn = column.column_name;
const columnTitle = column.title;
if (!validate) continue; if (!validate) continue;
const { func, msg } = validate; const { func, msg } = validate;
for (let j = 0; j < func.length; ++j) { for (let j = 0; j < func.length; ++j) {
const fn = const fn =
@ -2249,17 +2251,17 @@ class BaseModelSqlv2 {
? customValidators[func[j]] ? customValidators[func[j]]
: Validator[func[j]] : Validator[func[j]]
: func[j]; : func[j];
const columnValue = columns?.[cn] || columns?.[columnTitle];
const arg = const arg =
typeof func[j] === 'string' ? columns[cn] + '' : columns[cn]; typeof func[j] === 'string' ? columnValue + '' : columnValue;
if ( if (
columns[cn] !== null && ![null, undefined, ''].includes(columnValue) &&
columns[cn] !== undefined &&
columns[cn] !== '' &&
cn in columns &&
!(fn.constructor.name === 'AsyncFunction' ? await fn(arg) : fn(arg)) !(fn.constructor.name === 'AsyncFunction' ? await fn(arg) : fn(arg))
) { ) {
NcError.badRequest( NcError.badRequest(
msg[j].replace(/\{VALUE}/g, columns[cn]).replace(/\{cn}/g, cn) msg[j]
.replace(/\{VALUE}/g, columnValue)
.replace(/\{cn}/g, columnTitle)
); );
} }
} }

Loading…
Cancel
Save