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

Matching documents in array #14

Closed
Mihailoff opened this issue Jul 12, 2023 · 3 comments
Closed

Matching documents in array #14

Mihailoff opened this issue Jul 12, 2023 · 3 comments

Comments

@Mihailoff
Copy link
Contributor

There is some operator that iterates over an array.

var: ''

Empty var: '' is confusing, a special character like $this could make it more explicit.

test('some true', () => {
      const answer = logic.run({
        some: [
          [1, 2, 3],
          {
            '>': [
              {
                var: '$this'
              },
              2
            ]
          }
        ]
      })

      expect(answer).toBe(true)
    })

Regarding the nested object, I'd expect something like this:

test('some true', () => {
      const answer = logic.run({
        some: [
          [{ a: 5 }, { a: 1 }],
          {
            '>': [
              {
                var: '$this.a'
              },
              2
            ]
          }
        ]
      })

      expect(answer).toBe(true)
    })

What do you think?

@TotalTechGeek
Copy link
Owner

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.

@Mihailoff
Copy link
Contributor Author

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
   ] }
] }

@TotalTechGeek
Copy link
Owner

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

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

No branches or pull requests

2 participants