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

Tasks/schema validation #125

Open
wants to merge 3 commits into
base: staging
Choose a base branch
from
Open

Tasks/schema validation #125

wants to merge 3 commits into from

Conversation

npauzenga
Copy link
Owner

Hey @enriikke! Almost there, just a few more additions I want to squeeze in.

I do have one issue I'm hoping you can help with though. It seems that my JSON responses are not conforming to my schema, yet I can't get Committee to complain.

ActiveModel::Serializer is setup to use the JsonApi adapter which seems to be doing it's job. My actual responses look nice:

{
  "data": {
    "id": "1",
    "type": "users",
    "attributes": {
      "first_name": "Nate",
      "last_name": "Pauzenga",
      "email": "plabass@gmail.com",
      "city": "Annapolis",
      "state": "MD",
      "country": "USA"
    }
  }
}

My schema, however, wasn't built with this nesting in mind and the same response looks like this (as defined in the schema):

{
  "id": "1294",
  "first_name": "Nate",
  "last_name": "Pauzenga",
  "email": "test@admin.com",
  "city": "Annapolis",
  "state": "Maryland",
  "country": "USA"
}

I'm using the test helper assert_schema_conform and the middleware options Committee provides. No one seems to want to complain about the difference between the actual response and the schema definitions. I can even add/remove attributes in the schema and everything still passes.

Thoughts? It seems that, although the tests all pass, it may not be validating appropriately. Or perhaps I'm misunderstanding what is actually being validated, there are a couple examples but not a ton of documentation on the actual checks.

@npauzenga
Copy link
Owner Author

Also, this PR is really just documentation and a minor change to the Gemfile.

https://github.com/npauzenga/meet_meeples-server/blob/staging/spec/controllers/groups_controller_spec.rb

That link shows a spec that's currently using assert_schema_conform (though I do think it's implemented and pointing to the correct schema).

It could be the schema itself --

https://github.com/npauzenga/meet_meeples-server/blob/staging/config/schema/api.json

@npauzenga npauzenga mentioned this pull request Feb 1, 2016
@enriikke
Copy link
Collaborator

enriikke commented Feb 2, 2016

@npauzenga there are a couple of things that could be preventing your schema from actually being invalid. One is, make sure that you define both the schema as well as the targetSchema in your link definitions. schema is used to describe the input that the user should submit and what each endpoint accepts. targetSchema is used to describe your response and this is what Committee will use to validate. If it's not present I believe it will simply skip validation for that endpoint.

The other thing you might want to look at is making sure that your targetSchema specifies what is required using the required keyword. If you define your targetSchema but nothing is required then it wouldn't be invalid if none of those things are present. I think it's a good idea to always specify required attributes for any object definition, not just targetSchema. This is pretty helpful for schema as well since it would validate that user requests are actually valid before even hitting your API 💥

And the last thing you could also check is to not allow additional properties by setting additionalProperties to false at the beginning of your schema definition. By default is set to true so a schema returning a bunch of random stuff would be valid if everything else is good.

😃

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.

None yet

2 participants