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

Fix schema validation with internal fields (fixes #1244) #1259

Merged
merged 1 commit into from
Jun 13, 2017

Conversation

leplatrem
Copy link
Contributor

Fixes #1244

@leplatrem leplatrem requested a review from Natim June 8, 2017 09:11
@leplatrem leplatrem changed the title Fix schema validation when internal fields (fixes #1244) Fix schema validation with internal fields (fixes #1244) Jun 9, 2017
if required_fields:
schema = {**schema, 'required': required_fields}
else:
schema = {f: v for f, v in new.items() if f != 'required'}
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't think we need to go to this lengths to avoid putting an empty required field. The JSON Schema Spec says that omitting it is the same as an empty array. Unless you're trying to do this so that we don't confuse users by secretly changing their schema, but I don't think that's valuable, since we're already changing the schema.

Should we be doing these manipulations to the schema at schema save time?

Should we be removing definitions of internal fields from schema['properties']?

Copy link
Member

Choose a reason for hiding this comment

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

I agree with @glasserc that we should do all this at save time, rejecting the schema or changing it silently if it has internal fields in it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This approach has some advantages:

  • it avoids breaking with existing collection metadata
  • we strip record and schema at the same place (stripping record fields will still be necessary if the users don't allow additional props on their schemas)
  • we are in a resource class, where self.model.id_field etc. are available. It's not the case within the colander validator for collection schema
  • it is fairly similar to what there were before this PR, so I'm not chocked by those changes

Please expose your whys :)

Copy link
Contributor Author

@leplatrem leplatrem Jun 12, 2017

Choose a reason for hiding this comment

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

I don't think we need to go to this lengths to avoid putting an empty required field

The empty array gives me an error in jsonschema library that we use:

Failed validating 'minItems' in schema['properties']['required']:
    {'items': {'type': 'string'},
     'minItems': 1,
     'type': 'array',
     'uniqueItems': True}

On instance['required']:
    []

Opened python-jsonschema/jsonschema#339

schema = {**schema, 'required': required_fields}
else:
schema = {f: v for f, v in new.items() if f != 'required'}
data = {f: v for f, v in new.items() if f not in internal_fields}
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

@leplatrem leplatrem force-pushed the 1244-fix-id-schema-validation branch from 2065990 to c6bf967 Compare June 13, 2017 07:58
@leplatrem leplatrem merged commit 9df4fea into master Jun 13, 2017
@leplatrem leplatrem deleted the 1244-fix-id-schema-validation branch June 13, 2017 08:11
@leplatrem
Copy link
Contributor Author

Please open an issue if you think that we should refactor this.

@glasserc glasserc mentioned this pull request Jun 20, 2017
3 tasks
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

Successfully merging this pull request may close these issues.

3 participants