Skip to content
This repository has been archived by the owner on Jan 20, 2024. It is now read-only.

Rework using webapi-parser #61

Merged
merged 67 commits into from
Jul 28, 2020
Merged

Rework using webapi-parser #61

merged 67 commits into from
Jul 28, 2020

Conversation

postatum
Copy link
Contributor

@postatum postatum commented Feb 27, 2020

Major changes:

  • Reworked to work with webapi-parser;
  • New dependency: webapi-parser;
  • Dropped dependencies: json-schema-compatibility, is-stream, object-values, raml-validate;
  • Dropped option: RAMLVersion (now inferred automatically);
  • Default exported function API Changed: It now accepts webapi-parser.Operation object as first argument, path string as second argument, method name as third and options object as final argument;
  • Changed errors format;

Minor changes:

  • Updated dependencies' versions;
  • Switched to const/let instead of var;

Making a release:

  • Release dependencies PRs;
  • Bump major version and make a release;

PR depends on:

To test the PR locally with all the reworked Osprey sub-dependencies, use this Makefile.


Error format change

The new version changes the errors response format for RAML and JSON Schema validation errors. The new errors responses are more uniformal and descriptive while at the same time being less verbose. The change is caused by the fact that a different parser is used to parse RAML specs, which in turn produces a different parsed document model.

Let's explore the changes on body validation examples.

JSON Schema validation example responses

Number value is greater than the "maximum":

Old:

{
  "errors":[
    {
      "type":"json",
      "keyword":"schema",
      "schema":"{\n  \"$schema\": \"http://json-schema.org/draft-07/schema#\",\n  \"type\": \"object\",\n  \"minProperties\": 2,\n  \"properties\": {\n    \"firstName\": {\n      \"type\": \"string\"\n    },\n    \"lastName\": {\n      \"type\": \"string\"\n    },\n    \"age\": {\n      \"type\": \"number\",\n      \"minimum\": 5,\n      \"maximum\": 99\n    }\n  }\n}",
      "data":{
        "firstName":"John",
        "age":200
      },
      "message":"invalid json (schema, {\n  \"$schema\": \"http://json-schema.org/draft-07/schema#\",\n  \"type\": \"object\",\n  \"minProperties\": 2,\n  \"properties\": {\n    \"firstName\": {\n      \"type\": \"string\"\n    },\n    \"lastName\": {\n      \"type\": \"string\"\n    },\n    \"age\": {\n      \"type\": \"number\",\n      \"minimum\": 5,\n      \"maximum\": 99\n    }\n  }\n})"
    }
  ],
  "stack":"..."
}

New:

{
  "errors":[
    {
      "type":"json",
      "keyword":"maximum",
      "dataPath":"/age",
      "message":"should be <= 99",
      "data":200,
      "schema":99
    }
  ],
  "stack":"..."
}

Less properties than "minProperties":

Old:

{
  "errors":[
    {
      "type":"json",
      "keyword":"schema",
      "schema":"{\n  \"$schema\": \"http://json-schema.org/draft-07/schema#\",\n  \"type\": \"object\",\n  \"minProperties\": 2,\n  \"properties\": {\n    \"firstName\": {\n      \"type\": \"string\"\n    },\n    \"lastName\": {\n      \"type\": \"string\"\n    },\n    \"age\": {\n      \"type\": \"number\",\n      \"minimum\": 5,\n      \"maximum\": 99\n    }\n  }\n}",
      "data":{
        "firstName":"John"
      },
      "message":"invalid json (schema, {\n  \"$schema\": \"http://json-schema.org/draft-07/schema#\",\n  \"type\": \"object\",\n  \"minProperties\": 2,\n  \"properties\": {\n    \"firstName\": {\n      \"type\": \"string\"\n    },\n    \"lastName\": {\n      \"type\": \"string\"\n    },\n    \"age\": {\n      \"type\": \"number\",\n      \"minimum\": 5,\n      \"maximum\": 99\n    }\n  }\n})"
    }
  ],
  "stack":"..."
}

New:

{
  "errors":[
    {
      "type":"json",
      "keyword":"minProperties",
      "dataPath":"",
      "message":"should NOT have fewer than 2 properties",
      "data":{
        "firstName":"John"
      },
      "schema":2
    }
  ],
  "stack":"..."
}

RAML validation responses examples

Number value is greater than the "maximum":

Old:

{
  "errors":[
    {
      "type":"json",
      "dataPath":"age",
      "keyword":"maximum",
      "schema":99,
      "data":200,
      "message":"Value 200 is greater than maximum 99"
    }
  ],
  "stack":"..."
}

New:

{
  "errors":[
    {
      "type":"json",
      "keyword":"maximum",
      "dataPath":"/age",
      "message":"should be <= 99",
      "data":200,
      "schema":99
    }
  ],
  "stack":"..."
}

Less properties than "minProperties":

Old:

{
  "errors":[
    {
      "type":"json",
      "keyword":"minProperties",
      "schema":2,
      "message":"Too few properties defined, minimum 2"
    }
  ],
  "stack":"..."
}

New:

{
  "errors":[
    {
      "type":"json",
      "keyword":"minProperties",
      "dataPath":"",
      "message":"should NOT have fewer than 2 properties",
      "data":{
        "firstName":"John"
      },
      "schema":2
    }
  ],
  "stack":"..."
}

Copy link
Contributor

@jstoiko jstoiko left a comment

Choose a reason for hiding this comment

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

Major changes:
(...)

  • Changed errors format;

it would be nice to explain the rational behind this change (i.e. why it was needed) and what this change implies for the end-users.

README.md Outdated Show resolved Hide resolved
README.md Show resolved Hide resolved
@postatum
Copy link
Contributor Author

postatum commented Jul 14, 2020

it would be nice to explain the rational behind this change (i.e. why it was needed) and what this change implies for the end-users.

Updated the issue description to describe error format change in detail. Please let me know if the update looks good.

This behaviour was ported from the old version of
osprey-method-handler. The check limited body schema
types which were validated in jsonBodyHandler to
Scalar, Node, Array only.

The issue was discovered when trying to validate a
body of type Union. The validation just did not happen
because the body schema was ignored as it didn't pass
the check.

Now the check is completely removed as it doesn't seem
to make sense. As jsonBodyHandler is only called for
application/json requests, all the request bodies
defined in RAML for application/json should work well
with validation.
README.md Show resolved Hide resolved
README.md Show resolved Hide resolved
README.md Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
@postatum postatum merged commit ed03b79 into develop Jul 28, 2020
@postatum postatum deleted the rework_webapi_parser branch July 28, 2020 08:05
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants