Closed Mihailoff closed 1 year ago
Hi @Mihailoff! I appreciate your contributions to the repo!
I actually do agree that being more explicit improves readability in this case, but I'd like to remain as spec-compliant as possible with JSON-Logic.
Thinking through options:
One approach could be to erase "$this" as if it weren't there if it's at the beginning, allowing for both the explicit and implicit syntax. However, this would cause a divergence from the spec without extending behavior
Another approach is to override the var method with a different implementation, to customize it for the use case. However, there are some optimizations baked into var though that make me feel uncomfortable recommending this to a general user of the engine. Perhaps I could improve this somewhat.
If "$this" was added, would the traversal syntax remain the same?
For example,
If you wanted to access data from an above section,
../../a
inside of the some block would let you access the data from what was passed into the function.
Where is the spec? :) https://jsonlogic.com doesn't mention empty var
...
../../a
Following the "path" style, should it be then a dot .
?
This is my use case, a document with subdocuments. Will this work?
// doc = { items: [ { a: 1 }, { a: 2 } ] }
{ 'some': [
{ 'var': 'items' },
{ '<': [
{ 'var': 'a' }
10
] }
] }
Hm, the website fails to mention it there:
But it is implemented in the mainline repo here: https://github.com/jwadhams/json-logic-js/blob/9c805e9ac6a3787e8508e982a079888d3cc295b5/logic.js#L126
And shows up on the website under this section: https://jsonlogic.com/operations.html#all-none-and-some
And is part of the test suite: https://jsonlogic.com/tests.json
[ {"var":""}, 1, 1 ],
[ {"var":null}, 1, 1 ],
[ {"var":[]}, 1, 1 ],
Also yes, your rule there will work perfectly!
import LogicEngine from './logic.js'
const engine = new LogicEngine()
const validate = engine.build({
some: [
{
var: 'items'
},
{
'<': [
{
var: 'a'
},
10
]
}
]
})
console.log(validate({
items: [
{
a: 1
},
{
a: 2
}
]
})) // true
console.log(validate({
items: [
{
a: 11
},
{
a: 12
}
]
})) // false
There is
some
operator that iterates over an array.https://github.com/TotalTechGeek/json-logic-engine/blob/5f1d2b5b039e9add4a505f8a23713ebd0bb6d9c4/test.js#L466
Empty
var: ''
is confusing, a special character like$this
could make it more explicit.Regarding the nested object, I'd expect something like this:
What do you think?