Skip to content

Commit

Permalink
Add missing warning to rewriteFields about ignored files
Browse files Browse the repository at this point in the history
  • Loading branch information
sandrina-p committed Jul 24, 2024
1 parent 3fff064 commit 42b31ca
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 8 deletions.
29 changes: 21 additions & 8 deletions src/modify.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import get from 'lodash/get';
import mergeWith from 'lodash/mergeWith';

const WARNING_TYPES = {
CHANGE_FIELD_NOT_FOUND: 'CHANGE_FIELD_NOT_FOUND',
};
/**
*
* @param {*} path
Expand All @@ -26,14 +29,21 @@ function standardizeAttrs(attrs) {

function rewriteFields(schema, fieldsConfig) {
if (!fieldsConfig) return null;
const warnings = [];

const fieldsToModify = Object.entries(fieldsConfig);

fieldsToModify.forEach(([shortPath, mutation]) => {
const fieldPath = shortToFullPath(shortPath);

if (!get(schema.properties, fieldPath)) {
return; // Ignore field non existent in original schema.
// Do not override/edit a field that already exists.
// That's the job of config.fields method.
warnings.push({
type: WARNING_TYPES.CHANGE_FIELD_NOT_FOUND,
message: `Changing field "${shortPath}" was ignored because it does not exist.`,
});
return;
}

const fieldAttrs = get(schema.properties, fieldPath);
Expand All @@ -49,13 +59,17 @@ function rewriteFields(schema, fieldsConfig) {
);

if (fieldChanges.properties) {
rewriteFields(get(schema.properties, fieldPath), fieldChanges.properties);
const result = rewriteFields(get(schema.properties, fieldPath), fieldChanges.properties);
warnings.push(result.warnings);
}
});

return { warnings: warnings.flat() };
}

function rewriteAllFields(schema, configCallback, context) {
if (!configCallback) return null;

const parentName = context?.parent;

Object.entries(schema.properties).forEach(([fieldName, fieldAttrs]) => {
Expand All @@ -80,12 +94,9 @@ function rewriteAllFields(schema, configCallback, context) {

export function modify(originalSchema, config) {
const schema = JSON.parse(JSON.stringify(originalSchema));
let warnings = []; // To be implemented in next PRs.

// All these functions mutate "schema",
// that's why we create a copy above
rewriteFields(schema, config.fields);
// All these functions mutate "schema" that's why we create a copy above

const resultRewrite = rewriteFields(schema, config.fields);
rewriteAllFields(schema, config.allFields);

if (!config.muteWarningTip) {
Expand All @@ -94,8 +105,10 @@ export function modify(originalSchema, config) {
);
}

const allWarnings = [resultRewrite?.warnings].filter(Boolean).flat();

return {
schema,
warnings: warnings.length > 0 ? warnings : null,
warnings: allWarnings.length > 0 ? allWarnings : null,
};
}
38 changes: 38 additions & 0 deletions src/tests/modify.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,44 @@ describe('modify() - basic mutations', () => {
});
});

it('replace fields that dont exist gets ignored', () => {
// IMPORTANT NOTE on this behavior:
// Context: At Remote we have a lot of global customization that run equally across multiple different JSON Schemas.
// With this, we avoid applying customizations to non-existing fields. (aka create fields)
// That's why we have the "create" config, specific to create new fields.
const result = modify(schemaPet, {
fields: {
unknown_field: {
title: 'This field does not exist in the original schema',
},
'nested.field': {
title: 'Nop',
},
pet_name: {
title: 'New pet title',
},
},
});

expect(result.schema.properties.unknown_field).toBeUndefined();
expect(result.schema.properties.nested).toBeUndefined();
expect(result.schema.properties.pet_name).toEqual({
...schemaPet.properties.pet_name,
title: 'New pet title',
});

expect(result.warnings).toEqual([
{
type: 'CHANGE_FIELD_NOT_FOUND',
message: 'Changing field "unknown_field" was ignored because it does not exist.',
},
{
type: 'CHANGE_FIELD_NOT_FOUND',
message: 'Changing field "nested.field" was ignored because it does not exist.',
},
]);
});

it('replace all fields', () => {
const result = modify(schemaPet, {
allFields: (fieldName, fieldAttrs) => {
Expand Down

0 comments on commit 42b31ca

Please sign in to comment.