From f5dd95eeacd90bf4c8cc709c2aa7b54847650918 Mon Sep 17 00:00:00 2001 From: Michael Bromley Date: Tue, 28 Jan 2020 20:11:25 +0100 Subject: [PATCH] fix(core): Validate non-nullable custom fields --- packages/core/e2e/custom-fields.e2e-spec.ts | 2 +- .../core/src/api/common/validate-custom-field-value.ts | 7 +++++++ packages/core/src/i18n/messages/en.json | 1 + 3 files changed, 9 insertions(+), 1 deletion(-) diff --git a/packages/core/e2e/custom-fields.e2e-spec.ts b/packages/core/e2e/custom-fields.e2e-spec.ts index a6bd89fe89..49426b7473 100644 --- a/packages/core/e2e/custom-fields.e2e-spec.ts +++ b/packages/core/e2e/custom-fields.e2e-spec.ts @@ -262,7 +262,7 @@ describe('Custom fields', () => { } } `); - }, 'NOT NULL constraint failed: product.customFieldsNotnullable'), + }, "The custom field 'notNullable' value cannot be set to null"), ); it( diff --git a/packages/core/src/api/common/validate-custom-field-value.ts b/packages/core/src/api/common/validate-custom-field-value.ts index af281c2afc..3c25acf1f0 100644 --- a/packages/core/src/api/common/validate-custom-field-value.ts +++ b/packages/core/src/api/common/validate-custom-field-value.ts @@ -25,6 +25,13 @@ export function validateCustomFieldValue( if (config.readonly) { throw new UserInputError('error.field-invalid-readonly', { name: config.name }); } + if (config.nullable === false) { + if (value === null) { + throw new UserInputError('error.field-invalid-non-nullable', { + name: config.name, + }); + } + } switch (config.type) { case 'string': case 'localeString': diff --git a/packages/core/src/i18n/messages/en.json b/packages/core/src/i18n/messages/en.json index cc01ac73e2..5b173114e9 100644 --- a/packages/core/src/i18n/messages/en.json +++ b/packages/core/src/i18n/messages/en.json @@ -27,6 +27,7 @@ "entity-with-id-not-found": "No { entityName } with the id '{ id }' could be found", "field-invalid-datetime-range-max": "The custom field '{ name }' value [{ value }] is greater than the maximum [{ max }]", "field-invalid-datetime-range-min": "The custom field '{ name }' value [{ value }] is less than the minimum [{ min }]", + "field-invalid-non-nullable": "The custom field '{ name }' value cannot be set to null", "field-invalid-number-range-max": "The custom field '{ name }' value [{ value }] is greater than the maximum [{ max }]", "field-invalid-number-range-min": "The custom field '{ name }' value [{ value }] is less than the minimum [{ min }]", "field-invalid-readonly": "The custom field '{ name }' is readonly",