Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

oneOf filter #174

Open
juneidy opened this issue Nov 14, 2018 · 5 comments
Open

oneOf filter #174

juneidy opened this issue Nov 14, 2018 · 5 comments

Comments

@juneidy
Copy link

juneidy commented Nov 14, 2018

A question regarding oneOf in filter operation.
Given the example

const validator = require('is-my-json-valid');

const schema = {
	type : 'object',
	additionalProperties : false,
	properties : {
		bar : {
			type : 'object'
		},
		baz : {
			type : 'object'
		}
	},
	oneOf : [{
		properties : {
			bar : {
				type : 'object',
				additionalProperties : false,
				properties : {
					a : {
						type : 'array',
						items : {
							type : 'number'
						}
					}
				}
			},
			baz : {
				type : 'object',
				additionalProperties : false,
				properties : {
					z : {
						type : 'object'
					}
				}
			}
		}
	}, {
		properties : {
			bar : {
				type : 'object',
				additionalProperties : false,
				properties : {
					a : {
						type : 'array',
						items : {
							type : 'number'
						}
					},
					b : {
						type : 'array',
						items : {
							type : 'number'
						}
					}
				}
			},
			baz : {
				type : 'object',
				additionalProperties : false,
				properties : {
					y : {
						type : 'object'
					}
				}
			}
		}
	}]
};

const obj = {
	bar : {
		a : [1],
		b : [1]
	},
	baz : {
		z : {}
	}
};

const filter = validator.filter(schema);
const validate = validator(schema);

console.log(filter(obj));
// { bar: { a: [ 1 ], b: [ 1 ] }, baz: { z: {} } }

validate(obj);

console.log(validate.errors);
//[ { field: 'data',
//  message: 'no (or more than one) schemas match' } ]

It seems that filter ignores the oneOf completely.
Is this an expected behaviour?

@sid-360
Copy link

sid-360 commented Jul 11, 2019

I see no one responded to the above query. By any chance were you able to figure out what was the issue? I am currently working on a conditional validation of sub-schemas and would like to be aware of any issues/observations around oneOf, as I would certainly be using it for my use case. Thanks

@thetumper
Copy link

I don't think any matches. Your object has a bar that matches the bar in the first schema in the oneOf, but has a baz that matches the baz sub-schema in the second schema in the oneOf.

E.g., either of these should work: { bar: { a: [1] }, baz: { z: {} } } or { bar: { a: [1], b: [1]}, baz: { y: {} }}

@juneidy
Copy link
Author

juneidy commented Nov 12, 2019

I see no one responded to the above query. By any chance were you able to figure out what was the issue? I am currently working on a conditional validation of sub-schemas and would like to be aware of any issues/observations around oneOf, as I would certainly be using it for my use case. Thanks

If all you care is validating with oneOf, I think you're fine. The problem I stipulated is filter operation.

I don't think any matches. Your object has a bar that matches the bar in the first schema in the oneOf, but has a baz that matches the baz sub-schema in the second schema in the oneOf.

E.g., either of these should work: { bar: { a: [1] }, baz: { z: {} } } or { bar: { a: [1], b: [1]}, baz: { y: {} }}

I know the object doesn't match the schema, in fact I purposefully made it so to test the filter. Had the filter perform the operation taking into account of the oneOf, the validation that follows (in the example) would pass without error.

@thetumper
Copy link

thetumper commented Nov 13, 2019

If I'm understanding how filter works, you'd have to first match a schema. Because you're not, there's nothing to filter. So, the filter does seem to be taking into account the oneOf -- but the result is "filter based on (no schema)" --> i.e., filter nothing. Make sense?

@juneidy
Copy link
Author

juneidy commented Nov 14, 2019

If I'm understanding how filter works, you'd have to first match a schema. Because you're not, there's nothing to filter. So, the filter does seem to be taking into account the oneOf -- but the result is "filter based on (no schema)" --> i.e., filter nothing. Make sense?

Not quite sure. But if what you say is you need to match the schema to filter, then what is the purpose of filter? If I understand correctly, filter is to strip out properties that does not match the schema. Under normal circumstances that is the case. But why not oneOf?

If oneOf filter requires your object to match one of the schema, then there is nothing to filter too, because the resulting object would be the same anyway since it already match one of the schema.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants