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

No support for remote refs #1

Open
automatthew opened this issue Oct 1, 2013 · 18 comments
Open

No support for remote refs #1

automatthew opened this issue Oct 1, 2013 · 18 comments
Assignees
Milestone

Comments

@automatthew
Copy link
Contributor

JSCK doesn't pass the official refRemote test suite and this one test in ref.js.

@automatthew
Copy link
Contributor Author

I'd like to keep this all synchronous for the time being, but am willing to discuss.

@andrewjanssen
Copy link
Contributor

How about an "async" option on validate()? Defaults to false.

@automatthew
Copy link
Contributor Author

We need to resolve the remote refs at construction time, in case any other schemas refer to schemas within the remotes. Unless I've missed something.

@andrewjanssen
Copy link
Contributor

... in order to limit any schema-resolving recursion? Are there other problems that refs-referring-to-schemas would create?

@automatthew
Copy link
Contributor Author

Yeah, all schema reference resolution has to happen at "compile" time. I guess we can just bite the bullet and make instantiation be async. I'd want both an optional callback and some sort of EventEmitter like interface.

@andrewjanssen
Copy link
Contributor

I might be missing something, but I think we can put all the schema resolution in the validate method. The validate method has access to the schema's schema, so fetching it won't be a problem. And it's only during validation that we can determine what the refs and their schema's even are. Furthermore, I think you'd agree that it's more natural a) to make network requests with... and b) to supply callbacks to... a verb method like "validate" than to a constructor. If the validator caches retrieved schemas, there's no performance problem on validator reuse. Thoughts?

@automatthew
Copy link
Contributor Author

JIT compilation IOW

@automatthew
Copy link
Contributor Author

One consideration is being able to fail at construction time rather than at first use.

@automatthew
Copy link
Contributor Author

Which consideration we can handle by having a method that fetches refs and does callbacks && event emission.

@automatthew
Copy link
Contributor Author

Another possibility is to make JSCK async by default, but provide a method for synchronous usage which throws an error when it encounters a remote ref.

It should be possible to allow users to prefetch a remote schema document and include it in the construction call. If the top level "id" URL is correct, this might even work now?

@andrewjanssen
Copy link
Contributor

One consideration is being able to fail at construction time rather than at first use.

Agree. Failing on construction is a better pattern.

Which consideration we can handle by having a method that fetches refs and does callbacks && event emission.

Excellent. Love it. Would this method be required and cause verify to fail if it wasn't called first, or should it be called by verify as needed?

async by default

Could be good. I could even like an async-only design, as that would force all calling code into the same calling pattern. Depends on your design philosophy; do you favor having many ways to do something, or having only one way to do something?

@automatthew
Copy link
Contributor Author

Many ways, when compelling use cases can be established.

One of my common uses of JSON Schema (and thus JSCK, lately) is for validating service configuration files. These are simple enough schemas that I don't foresee ever needing remote refs. Synchronous construction and validation in this case helps mitigate the complexity of setting up a server in node.js.

@andrewjanssen
Copy link
Contributor

Sounds good. I think I have enough specification to make this issue happen. :) Here I go!

@automatthew
Copy link
Contributor Author

Bump the minor version in package.json for your branch, please.

@andrewjanssen
Copy link
Contributor

Will do.

@automatthew automatthew modified the milestone: 0.3 Nov 10, 2014
@automatthew automatthew self-assigned this Dec 4, 2014
@mafintosh
Copy link
Contributor

What if we created a inlineRemoteRefs module that was async and just inlined remoteRef definitions in your schema? That could be used by all schema engines and would mean that we wouldn't have to support a weird async interface

var inlineRemoteRefs = require('...')
inlineRemoteRefs(schema, function(err, sch) {
  if (err) throw err
  // sch has all remoteRefs definitions inlined
  var validate = validator(sch)
  ...
})

@automatthew
Copy link
Contributor Author

What if we created a inlineRemoteRefs module that was async and just inlined remoteRef definitions

That's probably the best way to add it non-invasively.

@tauren
Copy link

tauren commented Aug 5, 2015

👎 for async loading at construction time. Certainly don't do it at runtime

I suppose it depends on the use case, but it seems that adding support to automatically and recursively make http requests from this library may unnecessarily add complexity. I'm not just talking about requiring an async interface, but also the code in the library will grow and potentially be more fragile.

For my needs, I know in advance all of the schemas needed, so my application can simply prefetch them and provide them to jsck. This allows jsck to stay light and simple with fewer potential bugs and performance problems.

However, if an application doesn't know the schemas it will need and must load them dynamically at run time, perhaps a feature like this would be beneficial. I just hope that it doesn't bloat the library.

👍 for @mafintosh proposal. This would keep the async code in a separate module and wouldn't bloat the core jsck codebase.

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

No branches or pull requests

4 participants