Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 48 additions & 0 deletions __tests__/forms/utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: []
});
});

});

});
9 changes: 8 additions & 1 deletion lib/forms/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) }
Expand Down Expand Up @@ -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));
}
});
}

Expand Down