Skip to content

Commit

Permalink
fix(core): Validate non-nullable custom fields
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelbromley committed Jan 28, 2020
1 parent f8060b5 commit f5dd95e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 1 deletion.
2 changes: 1 addition & 1 deletion packages/core/e2e/custom-fields.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ describe('Custom fields', () => {
}
}
`);
}, 'NOT NULL constraint failed: product.customFieldsNotnullable'),
}, "The custom field 'notNullable' value cannot be set to null"),
);

it(
Expand Down
7 changes: 7 additions & 0 deletions packages/core/src/api/common/validate-custom-field-value.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down
1 change: 1 addition & 0 deletions packages/core/src/i18n/messages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit f5dd95e

Please sign in to comment.