jwadhams / json-logic-js

Build complex rules, serialize them as JSON, and execute them in JavaScript
MIT License
1.26k stars 140 forks source link

JSON Schema #55

Open NathanHazout opened 6 years ago

NathanHazout commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago
  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 commented 5 years ago

Yes, indeed a JSON Schema would be awesome 🎉

AndyOGo commented 5 years ago

@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 commented 5 years ago

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 commented 5 years ago

@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 commented 5 years ago

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 commented 5 years ago

@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: https://github.com/jwadhams/json-logic-js/blob/a15f528919346f2ec7d82bd4fc91c41481546c01/logic.js#L209-L219

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 https://github.com/axa-ch/json-logic-js/issues/15

AndyOGo commented 5 years ago

@yelworc I just started to refactor the schemas to get rid of anyOf and replace it with oneOf. https://github.com/axa-ch/json-logic-js/pull/16

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

AndyOGo commented 5 years ago

@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 commented 5 years ago

@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 commented 5 years ago

@yelworc The last thing which is not ideal I see is, that currently all operations can be combined with any other operator. Tracked here: https://github.com/axa-ch/json-logic-js/issues/17

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

jwadhams commented 5 years ago

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 commented 4 years ago

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

tajnymag commented 3 years ago

A year and a half later, any news?

toddbaert commented 11 months ago

@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 commented 2 months ago

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.