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

keep properties of invalid schemas #553

Closed

Conversation

sberan
Copy link

@sberan sberan commented Aug 31, 2017

What issue does this pull request resolve?

#129 (comment)

What changes did you make?

Only remove additional properties when the schema is valid.

Copy link
Member

@epoberezkin epoberezkin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for the PR!

  1. The change of behaviour is only possible for a specific value of removeAdditional option, as you proposed in removeAdditional doesn't work well with anyOf, oneOf or allOf #129 (comment), it cannot be done for all option values. Not sure if "afterValidation" is clear enough, I will think about it, but the main point is that it should be something specific. Current values should keep the current behaviour.
  2. In addition to the test you added (where the option value should be changed), there should be two more tests
    • for the option removeAdditional: true the property is removed even though the validation failed (the current behaviour)
    • for the new option value the property is removed when validation passed

An open question is how would be handled the situation when additionalProperties is a schema? There three possibilities

  1. just preserve old behaviour
  2. we need two new option values (new behaviour for current options false and "fail")
  3. create a completely new option, e.g. removeAdditionalOnSuccess: true

I need to sleep on it, but the last choice seems better.
For choices 2 or 3 there will be needed additional 3 tests (same as above) for cases when additionalProperties is a schema.

What do you think?

object.should.have.property('baz');
object.should.not.have.property('fizz');
console.log(ajv.getSchema('//test/fooBar2').toString());
ajv.validate('//test/fooBar2', failingObject).should.equal(false);
Copy link
Author

@sberan sberan Aug 31, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@epoberezkin I think I found a bug in master. When additionalProperties is a schema, properties are not validated (and this test fails at this location 😬 .

Copy link
Author

@sberan sberan Aug 31, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(it's only a problem if you supply an invalid additional property with an additional properties schema, and removeAdditional: 'failing' )

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@epoberezkin I figured out the issue. $nextValid was not being unset when the additional property was removed, which caused the function to exit with no errors.

@sberan sberan force-pushed the remove-additional-when-valid branch from 0609054 to 755cd79 Compare September 1, 2017 15:36
@sberan
Copy link
Author

sberan commented Sep 1, 2017

@epoberezkin I can go with removeAdditionalOnSuccess - I've implemented it in this PR and added tests for all the cases noted. What do you think?

@sberan
Copy link
Author

sberan commented Sep 1, 2017

@epoberezkin note that the diff is much easier to read if you ignore whitespace (?w=1) 😄

@epoberezkin
Copy link
Member

@sberan I will review, thank you

@@ -121,7 +131,8 @@ var {{=$nextValid}} = true;
if (errors) validate.errors.length = errors;
else validate.errors = null;
}
delete {{=$data}}[{{=$key}}];
{{=$nextValid}} = true;
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

setting this field to true fixed the bug I mentioned.

@epoberezkin
Copy link
Member

@sberan Thank you!

I don't think it is going to work as expected (that is to only remove additional properties when otherwise current schema succeeds). In your code additional properties are removed when all other properties succeed, but it doesn't check that other keywords succeed, e.g. required or minProperties etc.

Also the changes in tests are difficult to follow. I suggest not touching the existing tests and instead add other tests that validate that without this new option even if schema fails additional props are removed anyway and with this option they don't.

Regarding the fix for 'failing' option - I still don't understand the issue. Maybe it's better to make it a separate PR or at least make a separate test case that demonstrates the issue.

@epoberezkin
Copy link
Member

Closing, it is old.

@epoberezkin epoberezkin closed this Aug 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

Successfully merging this pull request may close these issues.

3 participants