Skip to content

Commit 9a59179

Browse files
author
Charly POLY
authored
Merge pull request #15 from wittydeveloper/fix/ignore-required-fields
fix(getSchemaFromConfig): ignoreFields now works for required fields
2 parents 88f7ca3 + 74bf63a commit 9a59179

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed

__tests__/forms/utils.spec.ts

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,54 @@ describe('forms/utils', () => {
202202
});
203203
});
204204

205+
describe('for ApolloFormConfigMutation / ignoring optional field', () => {
206+
const todoSchema: JSONSchema6 = require('../mocks/todo-json-schema.json');
207+
const config: ApolloFormConfigMutation = {
208+
mutation: {
209+
name: 'create_todo',
210+
document: null
211+
},
212+
ignoreFields: ['todo.completed']
213+
};
214+
215+
expect(
216+
getSchemaFromConfig(todoSchema, config)
217+
).toEqual({
218+
type: 'object',
219+
definitions: todoSchema.definitions,
220+
properties: schema({
221+
todo: {
222+
name: types.type('string', { required: true })
223+
}
224+
}).properties,
225+
required: []
226+
});
227+
});
228+
229+
describe('for ApolloFormConfigMutation / ignoring optional field', () => {
230+
const todoSchema: JSONSchema6 = require('../mocks/todo-json-schema.json');
231+
const config: ApolloFormConfigMutation = {
232+
mutation: {
233+
name: 'create_todo',
234+
document: null
235+
},
236+
ignoreFields: ['todo.name']
237+
};
238+
239+
expect(
240+
getSchemaFromConfig(todoSchema, config)
241+
).toEqual({
242+
type: 'object',
243+
definitions: todoSchema.definitions,
244+
properties: schema({
245+
todo: {
246+
completed: types.type('boolean')
247+
}
248+
}).properties,
249+
required: []
250+
});
251+
});
252+
205253
});
206254

207255
});

lib/forms/utils.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ export const flattenSchemaProperties = (entrySchema: any): any => {
6363
schema.properties,
6464
(result, value, key) => {
6565
if (get(value, '$ref')) {
66-
result[key] = retrieveSchema(value, definitions);
66+
result[key] = cloneDeep(retrieveSchema(value, definitions));
6767
} else {
6868
result[key] = has(value, 'properties') ?
6969
{ ...value, properties: reducer(value, definitions) }
@@ -156,6 +156,13 @@ export const getSchemaFromConfig = (jsonSchema: JSONSchema6, config: ApolloFormC
156156
if (config.ignoreFields) {
157157
config.ignoreFields.map(f => {
158158
unset(flattenSchema.properties, f.replace(/\./g, '.properties.'));
159+
const pathParts = f.split('.');
160+
const prop = pathParts.pop(); // remove prop
161+
const parentPath = pathParts.join('.properties.');
162+
const parentRequired = get(flattenSchema.properties, `${parentPath}.required`);
163+
if (parentRequired.includes(prop)) {
164+
set(flattenSchema.properties, `${parentPath}.required`, filter(parentRequired, v => v !== prop));
165+
}
159166
});
160167
}
161168

0 commit comments

Comments
 (0)