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

JSON Schema #55

Open
NathanHazout opened this issue Mar 22, 2018 · 17 comments · Fixed by axa-ch/json-logic-js#4 · May be fixed by #136
Open

JSON Schema #55

NathanHazout opened this issue Mar 22, 2018 · 17 comments · Fixed by axa-ch/json-logic-js#4 · May be fixed by #136

Comments

@NathanHazout
Copy link

Does an official JSON Schema (http://json-schema.org/) currently exists for json-logic syntax?

If not, do you think it would be possible?

@jwadhams
Copy link
Owner

Not to my knowledge.
I think you'd end up writing a schema for every supported operation, right? And then any operation can take an operation in almost any argument, but in a few minutes Googling I couldn't find something inheritance-like to say:
{"+":[]} is a type of JsonLogic statement. It takes an array of numbers or "JsonLogic statements"

Maybe you could write one big schema that takes every supported operation as an optional property, but I'm not sure the format lets you say "you have to have exactly one of this huge property list"... and it doesn't play nice with adding operations.

What benefit do you expect from having a schema?

@iddan
Copy link

iddan commented May 18, 2018

  1. You can assert input to operations
  2. You can inspect input for operations: I'm developing a UI for editing json-logic and a schema would be extremely helpful

@AndyOGo
Copy link

AndyOGo commented Feb 19, 2019

Yes, indeed a JSON Schema would be awesome 🎉

@AndyOGo
Copy link

AndyOGo commented Mar 18, 2019

@nasht00
@jwadhams
@iddan

This package is really great, unfortunately it seems that it isn't maintained anymore 😞

Therefore we forked it, rewrote it to ESnext and added JSON Schemas.
I just released v2.0.1 at on npm @axa-ch/json-logic-js

@jwadhams
I hope we can return the ball to your original repo

@yelworc
Copy link

yelworc commented Mar 20, 2019

Whoa, @AndyOGo – I was just evaluating json-logic for a project yesterday, and wondered if there was a schema to validate the resulting logic data – and here it is, added just a day before 😆

Now if only the schema files were really hosted at the given URLs, my IDE (VS Code) could actually use them for validation 😏

@AndyOGo
Copy link

AndyOGo commented Mar 20, 2019

@yelworc
Yeah, you are absolutely right, thats why I want to play the ball back to @jwadhams
So that all is nicely grouped together and publicly at jsonlogic.com

@yelworc
Copy link

yelworc commented Mar 20, 2019

Hmm. After playing with it a little more, I'm starting to wonder if this is as helpful as I had hoped. The schema validates anything, due to the inclusion of common/all-types in common/any – which is fair, I guess, since the apply function does accept any value as its first parameter, but that makes the whole thing a bit pointless. Am I missing something here?

@AndyOGo
Copy link

AndyOGo commented Mar 20, 2019

@yelworc
Yeah, you are absolutely right, actually ideally this would be oneOf instead of anyOf:
https://github.com/axa-ch/json-logic-js/blob/d56799465c6ce011c23f09d45267bba55ae55ec7/schemas/common/any.json#L8

and maybe it works if object is removed from all-types here:
https://github.com/axa-ch/json-logic-js/blob/d56799465c6ce011c23f09d45267bba55ae55ec7/schemas/common/all-types.json#L7

ACTUALLY IF I CHECK THE ORIGINALL CODE, IT SEEMS WOULD BE OKAY TO REMOVE object:

json-logic-js/logic.js

Lines 209 to 219 in a15f528

jsonLogic.apply = function(logic, data) {
// Does this array contain logic? Only one way to find out.
if(Array.isArray(logic)) {
return logic.map(function(l) {
return jsonLogic.apply(l, data);
});
}
// You've recursed to a primitive, stop!
if( ! jsonLogic.is_logic(logic) ) {
return logic;
}

Indeed jsonLogic.apply and all it's operands are very forgiving, so I decided to start with a forgiving schema.
The first arg could also just be a boolean, as documented here:
https://github.com/jwadhams/json-logic-js#always-and-never

But I agree with you it's rather paradox and makes these schemas kinda pointless.
Though you can still replace all-types and any with your own.

Also it is possibly to nest any operator within each other, but semantically this is pointless too.
An ideal schema would also test, that a given operator makes sense.

I would also really like to get the inventors feedback @jwadhams
What do you think?

Edit:
Tracked here axa-ch#15

@AndyOGo
Copy link

AndyOGo commented Mar 21, 2019

@yelworc
I just started to refactor the schemas to get rid of anyOf and replace it with oneOf.
axa-ch#16

All official tests pass now, so seems to be good.

@AndyOGo
Copy link

AndyOGo commented Mar 21, 2019

@yelworc
I just released a fix version 2.0.1.
Works now with oneOf only and type object is only allowed if it is not a logic object (any existing operator)

@yelworc
Copy link

yelworc commented Mar 21, 2019

@AndyOGo cool! This did fix one false negative I saw before ({ if: [true, 'x', 'y'] }, for what it's worth).

Agreed, I'd appreciate some feedback by the maintainer, too; e.g. thoughts regarding a potential future major version with less permissive input handling.

(I'm a bit concerned about the general maintenance status of the project, so I'm glad to see the activity in your fork!)

@AndyOGo
Copy link

AndyOGo commented Mar 21, 2019

@yelworc
The last thing which is not ideal I see is, that currently all operations can be combined with any other operator. Tracked here: axa-ch#17

If you see any other issues, please feel free to create a ticket.

@jwadhams
Copy link
Owner

Thank you all for your hard work, this is spectacular. I haven't been able to dedicate as much time to this library as I'd like.

@AndyOGo I'll take a look at your repo and see if we're in a position to merge up. It looks like really great, thoughtful work.

@AndyOGo
Copy link

AndyOGo commented May 29, 2020

@jwadhams
We just migrated from draft-04 to draft-07 with v2.2.2.

@tajnymag
Copy link

A year and a half later, any news?

@toddbaert
Copy link

@jwadhams Thanks for all your help efforts with this library, and with the Json-Logic project in general.

Is schema-support something you'd still consider? It would be great to have these schemas actually hosted at jsonlogic.com. I imagine this project is a labor of love, so I understand having limited time to devote to it, but I think this is a killer feature.

@Tiglat-Pileser
Copy link

Tiglat-Pileser commented Jun 20, 2024

Has any progress been made on this issue? I have recently been working on a schema for validating json logics components myself but I would prefer to check that against any other attempts out there if there are any.

EDIT: Actually, I decided to upload my attempt at a json schema as pull request #136 . Tests so far seem fine, but I do not have the time or means to test every possible combination of expressions and arguments.

@Tiglat-Pileser Tiglat-Pileser linked a pull request Jun 20, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
8 participants