question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

.validate(schema) should not dereference the schema

See original GitHub issue

Validator should only check if the schema is correct, but validate() modified the input object.

(Oh my it spent my a whole hour to find validate() mutate the object…)

Workaround: validate(_.cloneDeep(schema))

Issue Analytics

  • State:open
  • Created 6 years ago
  • Reactions:2
  • Comments:7 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Jack-Workscommented, Jan 9, 2018

You’re right, there are always something community will argue about I’ll wait for the isolated validator, thank you

On Tue, Jan 9, 2018, 22:31 James Messinger notifications@github.com wrote:

I agree that mutating the object is bad behavior. But here are the reasons that I chose to do it that way:

  1. I don’t want to bloat the library by adding a deep-clone library
  2. There are many different deep-cloning algorithms, each with their own trade-offs, and regardless of which one I chose, some people would complain
  3. Most people use Swagger Parser to parse/validate files, not in-memory objects. So this mutation behavior only affects a minority of users

That said, there is a solution coming soon. In an upcoming release, I’ll be removing the validate() method altogether and moving it to a separate package. So the swagger-parser will only contain logic for reading, parsing, resolving, and dereferencing. The swagger-validator package (or whatever I end up calling it) will contain the validation logic. There are several benefits to this:

  1. It reduces the size of the swagger-parser package significantly, since its largest dependency is the JSON Schema validator. This is especially valuable for people who don’t need the validate() method.
  2. It decouples the parsing and validation logic
  3. It gets closer to the Node principle of small, single-purpose packages
  4. It allows people to write their own validators on top of swagger-parser. So if want to use a different JSON Schema parser, or bundle-in a deep-cloning algorithm, you can do that.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/BigstickCarpet/swagger-parser/issues/80#issuecomment-356299811, or mute the thread https://github.com/notifications/unsubscribe-auth/AFJBf1mq2aJTezIGDgQoWHUZ3ekLukYFks5tI3hCgaJpZM4RXyTc .

1reaction
JamesMessingercommented, Jan 9, 2018

I agree that mutating the object is bad behavior. But here are the reasons that I chose to do it that way:

  1. I don’t want to bloat the library by adding a deep-clone library
  2. There are many different deep-cloning algorithms, each with their own trade-offs, and regardless of which one I chose, some people would complain
  3. Most people use Swagger Parser to parse/validate files, not in-memory objects. So this mutation behavior only affects a minority of users

That said, there is a solution coming soon. In an upcoming release, I’ll be removing the validate() method altogether and moving it to a separate package. So the swagger-parser will only contain logic for reading, parsing, resolving, and dereferencing. The swagger-validator package (or whatever I end up calling it) will contain the validation logic. There are several benefits to this:

  1. It reduces the size of the swagger-parser package significantly, since its largest dependency is the JSON Schema validator. This is especially valuable for people who don’t need the validate() method.
  2. It decouples the parsing and validation logic
  3. It gets closer to the Node principle of small, single-purpose packages
  4. It allows people to write their own validators on top of swagger-parser. So if want to use a different JSON Schema parser, or bundle-in a deep-cloning algorithm, you can do that.
Read more comments on GitHub >

github_iconTop Results From Across the Web

Everit schema dereferencing - java - Stack Overflow
When I'm trying to validate some object with this schema, using everit I get the following exception: Exception in thread "main" java.io.
Read more >
Validating Documents Against a JSON Schema with the JSON ...
The <json:validate-schema> operation validates that the input content is compliant with a given JSON schema.
Read more >
Implementations - JSON Schema
If you have updates to this list, make a pull request on the GitHub repo. Listing does not signify a recommendation or endorsement...
Read more >
JSON Schema Validation - Cloudant Blog -
It can be used to validate documents prior to being stored in the databases. If it doesn't match the specification, it's not allowed...
Read more >
JSON Schema Validator - Newtonsoft
View source code. An online, interactive JSON Schema validator. Supports JSON Schema Draft 3, Draft 4, Draft 6, Draft 7 and Draft 2019-09....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found