Skip to content

Commit

Permalink
🐛 Fix config being scrubbed from passing schemas (#994)
Browse files Browse the repository at this point in the history
* 🐛 Do not scrub properties that fail with passing schemas

Specifically when a oneOf schema fails by having more than a single passing schema. The property is
still scrubbed when there are zero passing oneOf schemas.

* ✅ Add core assertion for additional snapshot multiple name options
  • Loading branch information
wwilsman authored Jul 20, 2022
1 parent 85b131e commit d3f9f50
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/config/src/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ export function validate(data, key = '/config') {
set(data, path, Math.min(error.data, error.schema));
} else if (keyword === 'required') {
del(data, path.slice(0, -1));
} else {
} else if (!params.passingSchemas) {
del(data, path);
}

Expand Down
32 changes: 31 additions & 1 deletion packages/config/test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ describe('PercyConfig', () => {
},
errors: {
oneOf: ({ params }) => (
`must pass a single schema, passed ${params.passingSchemas ?? 0}`
`must pass a single schema, passed ${params.passingSchemas?.length ?? 0}`
)
}
}
Expand Down Expand Up @@ -300,6 +300,36 @@ describe('PercyConfig', () => {
expect(conf).toEqual({});
});

it('does not scrub properties that match at least one schema', () => {
PercyConfig.addSchema({
foo: {
type: 'object',
properties: {
bar: { const: 'baz' },
qux: { const: 'xyzzy' }
},
oneOf: [
{ required: ['bar'] },
{ required: ['qux'] }
],
errors: {
oneOf: ({ params }) => (
`only 1 schema should match (matched ${params.passingSchemas?.length})`
)
}
}
});

let conf = { foo: { bar: 'baz', qux: 'xyzzy' } };

expect(PercyConfig.validate(conf)).toEqual([{
path: 'foo',
message: 'only 1 schema should match (matched 2)'
}]);

expect(conf).toEqual({ foo: { bar: 'baz', qux: 'xyzzy' } });
});

it('can validate functions and regular expressions', () => {
PercyConfig.addSchema({
func: { instanceof: 'Function' },
Expand Down
4 changes: 4 additions & 0 deletions packages/core/test/snapshot.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ describe('Snapshot', () => {
'[percy] - additionalSnapshots[0]: missing required name, prefix, or suffix',
'[percy] - additionalSnapshots[1]: prefix & suffix are ignored when a name is provided'
]);
expect(logger.stdout).toEqual([
'[percy] Snapshot taken: /',
'[percy] Snapshot taken: nombre'
]);
});

it('warns when providing conflicting options', async () => {
Expand Down

0 comments on commit d3f9f50

Please sign in to comment.