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

Support 'function' data type #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

bitmage
Copy link

@bitmage bitmage commented Mar 16, 2013

Now I know what you're gonna say. Functions aren't valid json. Bear with me.

JSON schemas are really useful, and shouldn't be limited to just strict JSON cases. If you add in the capability to detect functions, you've now got support for all javascript types and therefore have broadened the usefulness.

This doesn't interfere with any of the existing capabilities, as you can see. The tests all still pass, and I've added a few more. There a couple possible cases to consider:

  1. The user of the library doesn't have functions in their target JSON, or their schema. Great, everything will work as according to spec.
  2. The user of the library has functions in their target JSON, but not in the schema. Type checks still work as normal, so if you're expecting an object and you get a function, the schema validation will fail. This is actually an increase in accuracy, since with the previous implementation this 'function' would have been detected as an 'object' and the test would pass.
  3. The user has functions in their schema, but not in the JSON. This should be the same as the previous case. Type checking still works as normal.

What do you say? Can we have our cake and eat it too?

@toverly
Copy link

toverly commented Mar 16, 2013

Maybe pitch your idea to the json schema people?

@bitmage
Copy link
Author

bitmage commented Mar 16, 2013

Well, I imagine they would reject it, and rightly so. It doesn't have anything to do with JSON in the universal sense. For instance it wouldn't make any sense for a .NET or Python implementation to support JavaScript functions. However, it seems handy in the case of a JavaScript implementation. It allows you to do extra things with no perceivable disadvantage.

Can you think of any cases where this would be a hindrance, or lead to problems down the road?

On Mar 16, 2013, at 12:41 PM, toverly notifications@github.com wrote:

Maybe pitch your idea to the json schema people?


Reply to this email directly or view it on GitHub.

@pvorb
Copy link
Contributor

pvorb commented Mar 26, 2013

I would say this feature doesn't really belong to JaySchema. It does JSON Schema validation according to the spec and that's all. Keep it simple.

You might want to consider to plug this functionality into JaySchema. But therefore one would need some kind ofplug-in interface.

@bitmage
Copy link
Author

bitmage commented Mar 26, 2013

If you wanted to support this sort of functionality as plugins, you would essentially be building an extensible type system. The way I would suggest going about this is by letting the consumer of the library inject their own 'apparentType' function.

However, I'm still not seeing why this wouldn't be supported natively. If you really want to be true to the JSON spec, JaySchema should throw an exception or fail to parse a document with functions. Those aren't valid JSON. But I think it's wise to strike a balance between being academically correct and building a tool that's useful to people.

Either way, I have what I need. I'm just trying to make this work accessible to other people so they don't have to re-implement it.

@redben
Copy link

redben commented Dec 20, 2014

+1 though not part of JSON, this would be useful for example to enforce "javascript interfaces", or to document these.

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

4 participants