Skip to content

Commit

Permalink
fix(core): Fix validation for nullable custom string fields with options
Browse files Browse the repository at this point in the history
Fixes #1083
  • Loading branch information
michaelbromley committed Sep 13, 2021
1 parent 46659c7 commit 9afa145
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
25 changes: 23 additions & 2 deletions packages/core/e2e/custom-fields.e2e-spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,12 @@ const customConfig = mergeConfig(testConfig, {
type: 'string',
options: [{ value: 'small' }, { value: 'medium' }, { value: 'large' }],
},
{
name: 'nullableStringWithOptions',
type: 'string',
nullable: true,
options: [{ value: 'small' }, { value: 'medium' }, { value: 'large' }],
},
{
name: 'nonPublic',
type: 'string',
Expand Down Expand Up @@ -228,6 +234,7 @@ describe('Custom fields', () => {
{ name: 'validateFn4', type: 'string', list: false },
{ name: 'validateRelation', type: 'relation', list: false },
{ name: 'stringWithOptions', type: 'string', list: false },
{ name: 'nullableStringWithOptions', type: 'string', list: false },
{ name: 'nonPublic', type: 'string', list: false },
{ name: 'public', type: 'string', list: false },
{ name: 'longString', type: 'string', list: false },
Expand Down Expand Up @@ -388,7 +395,7 @@ describe('Custom fields', () => {
const longString = Array.from({ length: 500 }, v => 'hello there!').join(' ');
const result = await adminClient.query(
gql`
mutation($stringValue: String!) {
mutation ($stringValue: String!) {
updateProduct(input: { id: "T_1", customFields: { longString: $stringValue } }) {
id
customFields {
Expand All @@ -407,7 +414,7 @@ describe('Custom fields', () => {
const longString = Array.from({ length: 500 }, v => 'hello there!').join(' ');
const result = await adminClient.query(
gql`
mutation($stringValue: String!) {
mutation ($stringValue: String!) {
updateProduct(
input: {
id: "T_1"
Expand Down Expand Up @@ -470,6 +477,20 @@ describe('Custom fields', () => {
expect(updateProduct.customFields.stringWithOptions).toBe('medium');
});

it('nullable string option with null', async () => {
const { updateProduct } = await adminClient.query(gql`
mutation {
updateProduct(input: { id: "T_1", customFields: { nullableStringWithOptions: null } }) {
id
customFields {
nullableStringWithOptions
}
}
}
`);
expect(updateProduct.customFields.nullableStringWithOptions).toBeNull();
});

it(
'invalid localeString',
assertThrowsWithMessage(async () => {
Expand Down
3 changes: 3 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 @@ -91,6 +91,9 @@ function validateStringField(
const options = (config as StringCustomFieldConfig).options;
if (options) {
const validOptions = options.map(o => o.value);
if (value === null && config.nullable === true) {
return;
}
if (!validOptions.includes(value)) {
throw new UserInputError('error.field-invalid-string-option', {
name: config.name,
Expand Down

0 comments on commit 9afa145

Please sign in to comment.