Răsfoiți Sursa

fix(core): Fix validation for nullable custom string fields with options

Fixes #1083
Michael Bromley 4 ani în urmă
părinte
comite
9afa14595c

+ 23 - 2
packages/core/e2e/custom-fields.e2e-spec.ts

@@ -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',
@@ -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 },
@@ -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 {
@@ -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"
@@ -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 () => {

+ 3 - 0
packages/core/src/api/common/validate-custom-field-value.ts

@@ -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,