Skip to content

JSON Schema #55

@NathanHazout

Description

@NathanHazout

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

If not, do you think it would be possible?

Activity

jwadhams

jwadhams commented on Mar 26, 2018

@jwadhams
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

iddan commented on May 18, 2018

@iddan
  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

AndyOGo commented on Feb 19, 2019

@AndyOGo

Yes, indeed a JSON Schema would be awesome 🎉

AndyOGo

AndyOGo commented on Mar 18, 2019

@AndyOGo

@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

yelworc commented on Mar 20, 2019

@yelworc

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

AndyOGo commented on Mar 20, 2019

@AndyOGo

@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

yelworc commented on Mar 20, 2019

@yelworc

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

AndyOGo commented on Mar 20, 2019

@AndyOGo

@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

AndyOGo commented on Mar 21, 2019

@AndyOGo

@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

AndyOGo commented on Mar 21, 2019

@AndyOGo

@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

yelworc commented on Mar 21, 2019

@yelworc

@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

AndyOGo commented on Mar 21, 2019

@AndyOGo

@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

jwadhams commented on Mar 21, 2019

@jwadhams
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

AndyOGo commented on May 29, 2020

@AndyOGo

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

5 remaining items

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Participants

      @NathanHazout@yelworc@AndyOGo@jwadhams@tajnymag

      Issue actions

        JSON Schema · Issue #55 · jwadhams/json-logic-js