Default values ignored for validations affected by conditions (#15310)

* added default values when validating items

* abstracted default value parsing to its own util

* Added a basic test

* updated tests

* fixed import

* implementing the same fix for conditions on the drawer-item
This commit is contained in:
Brainslug
2022-08-30 16:46:38 +02:00
committed by GitHub
parent a91e96d6d1
commit b2d127dcc8
5 changed files with 175 additions and 19 deletions

View File

@@ -0,0 +1,140 @@
import { test, expect } from 'vitest';
import { getDefaultValuesFromFields } from './get-default-values-from-fields';
test('Ignores PK default value', () => {
const values = getDefaultValuesFromFields([
{
collection: 'test_collection',
field: 'id',
type: 'integer',
schema: {
name: 'id',
table: 'test_collection',
schema: 'public',
data_type: 'integer',
is_nullable: false,
generation_expression: null,
default_value: "nextval('test_collection_id_seq'::regclass)",
is_generated: false,
max_length: null,
comment: null,
numeric_precision: 32,
numeric_scale: 0,
is_unique: true,
is_primary_key: true,
has_auto_increment: true,
foreign_key_schema: null,
foreign_key_table: null,
foreign_key_column: null,
},
meta: null,
name: 'ID',
},
]);
expect(values.value).toStrictEqual({});
});
test('Ignores schemaless fields', () => {
const values = getDefaultValuesFromFields([
{
collection: 'test_collection',
field: 'test',
type: 'integer',
schema: null,
meta: null,
name: 'Test',
},
]);
expect(values.value).toStrictEqual({});
});
test('Parses default values', () => {
const values = getDefaultValuesFromFields([
{
collection: 'test_collection',
field: 'condition',
type: 'string',
schema: {
name: 'condition',
table: 'test_collection',
schema: 'public',
data_type: 'character varying',
is_nullable: true,
generation_expression: null,
default_value: 'test1',
is_generated: false,
max_length: 255,
comment: null,
numeric_precision: null,
numeric_scale: null,
is_unique: false,
is_primary_key: false,
has_auto_increment: false,
foreign_key_schema: null,
foreign_key_table: null,
foreign_key_column: null,
},
meta: null,
name: 'Condition',
},
{
collection: 'test_collection',
field: 'test1',
type: 'string',
schema: {
name: 'test1',
table: 'test_collection',
schema: 'public',
data_type: 'character varying',
is_nullable: true,
generation_expression: null,
default_value: '---',
is_generated: false,
max_length: 255,
comment: null,
numeric_precision: null,
numeric_scale: null,
is_unique: false,
is_primary_key: false,
has_auto_increment: false,
foreign_key_schema: null,
foreign_key_table: null,
foreign_key_column: null,
},
meta: null,
name: 'Test 1',
},
{
collection: 'test_collection',
field: 'test2',
type: 'string',
schema: {
name: 'test2',
table: 'test_collection',
schema: 'public',
data_type: 'character varying',
is_nullable: true,
generation_expression: null,
default_value: null,
is_generated: false,
max_length: 255,
comment: null,
numeric_precision: null,
numeric_scale: null,
is_unique: false,
is_primary_key: false,
has_auto_increment: false,
foreign_key_schema: null,
foreign_key_table: null,
foreign_key_column: null,
},
meta: null,
name: 'Test2',
},
]);
expect(values.value).toStrictEqual({
condition: 'test1',
test1: '---',
test2: null,
});
});

View File

@@ -0,0 +1,21 @@
import { Field } from '@directus/shared/types';
import { Ref, unref, computed, ComputedRef } from 'vue';
export function getDefaultValuesFromFields(fields: Field[] | Ref<Field[]>): ComputedRef<Record<string, any>> {
return computed(() => {
return unref(fields).reduce(function (acc, field) {
if (
field.schema?.default_value !== undefined &&
// Ignore autoincremented integer PK field
!(
field.schema.is_primary_key &&
field.schema.data_type === 'integer' &&
typeof field.schema.default_value === 'string'
)
) {
acc[field.field] = field.schema.default_value;
}
return acc;
}, {} as Record<string, any>);
});
}