From 74bf63a288fc4f90d4409ed02083902dbba567e7 Mon Sep 17 00:00:00 2001 From: Charly POLY Date: Wed, 8 Aug 2018 12:37:32 +0200 Subject: [PATCH] fix(getSchemaFromConfig): ignoreFields now works for required fields --- __tests__/forms/utils.spec.ts | 48 +++++++++++++++++++++++++++++++++++ lib/forms/utils.ts | 9 ++++++- 2 files changed, 56 insertions(+), 1 deletion(-) diff --git a/__tests__/forms/utils.spec.ts b/__tests__/forms/utils.spec.ts index 9ae937c..39e4c00 100644 --- a/__tests__/forms/utils.spec.ts +++ b/__tests__/forms/utils.spec.ts @@ -202,6 +202,54 @@ describe('forms/utils', () => { }); }); + describe('for ApolloFormConfigMutation / ignoring optional field', () => { + const todoSchema: JSONSchema6 = require('../mocks/todo-json-schema.json'); + const config: ApolloFormConfigMutation = { + mutation: { + name: 'create_todo', + document: null + }, + ignoreFields: ['todo.completed'] + }; + + expect( + getSchemaFromConfig(todoSchema, config) + ).toEqual({ + type: 'object', + definitions: todoSchema.definitions, + properties: schema({ + todo: { + name: types.type('string', { required: true }) + } + }).properties, + required: [] + }); + }); + + describe('for ApolloFormConfigMutation / ignoring optional field', () => { + const todoSchema: JSONSchema6 = require('../mocks/todo-json-schema.json'); + const config: ApolloFormConfigMutation = { + mutation: { + name: 'create_todo', + document: null + }, + ignoreFields: ['todo.name'] + }; + + expect( + getSchemaFromConfig(todoSchema, config) + ).toEqual({ + type: 'object', + definitions: todoSchema.definitions, + properties: schema({ + todo: { + completed: types.type('boolean') + } + }).properties, + required: [] + }); + }); + }); }); diff --git a/lib/forms/utils.ts b/lib/forms/utils.ts index 0cf13ef..abe2bb0 100644 --- a/lib/forms/utils.ts +++ b/lib/forms/utils.ts @@ -63,7 +63,7 @@ export const flattenSchemaProperties = (entrySchema: any): any => { schema.properties, (result, value, key) => { if (get(value, '$ref')) { - result[key] = retrieveSchema(value, definitions); + result[key] = cloneDeep(retrieveSchema(value, definitions)); } else { result[key] = has(value, 'properties') ? { ...value, properties: reducer(value, definitions) } @@ -156,6 +156,13 @@ export const getSchemaFromConfig = (jsonSchema: JSONSchema6, config: ApolloFormC if (config.ignoreFields) { config.ignoreFields.map(f => { unset(flattenSchema.properties, f.replace(/\./g, '.properties.')); + const pathParts = f.split('.'); + const prop = pathParts.pop(); // remove prop + const parentPath = pathParts.join('.properties.'); + const parentRequired = get(flattenSchema.properties, `${parentPath}.required`); + if (parentRequired.includes(prop)) { + set(flattenSchema.properties, `${parentPath}.required`, filter(parentRequired, v => v !== prop)); + } }); }