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

Invalid defaultGroupConjunction when using custom conjunctions #1137

Open
oriolcx opened this issue Nov 16, 2024 · 0 comments
Open

Invalid defaultGroupConjunction when using custom conjunctions #1137

oriolcx opened this issue Nov 16, 2024 · 0 comments

Comments

@oriolcx
Copy link

oriolcx commented Nov 16, 2024

Describe the bug
Error happens when the config.conjuntions only contains custom conditions. If a group is created, the jsonLogicFormat() function throws an error.

To Reproduce
create a config:

const config ={
 ...initialConfig,
  conjunctions: {
    add: {
      label: '+',
      formatConj: (children, _conj, not) => (not ? 'NOT ' : '') + '(' + children.join(' + ') + ')',
      mongoConj: '$add',
      sqlFormatConj: (children, _conj, not) => (not ? 'NOT ' : '') + '(' + children.join(' + ') + ')',
      spelFormatConj: (children, _conj, not) => (not ? 'NOT ' : '') + '(' + children.join(' + ') + ')',
    },
  },
});

Then create a group in the query builder and call jsonLogicFormat() function.

Expected behavior
To not throw an error

Additional context
I found different options to fix this.

  • One is defining in the settings the undocumented option to:
    defaultGroupConjunction: "add"

  • An alternative, transparent to the user, would be to add in the function defaultGroupConjuntion in defaultUtils:

var defaultGroupConjunction = exports.defaultGroupConjunction = function defaultGroupConjunction(config) {
  var groupFieldConfig = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : null;
  groupFieldConfig = (0, _configUtils.getFieldConfig)(config, groupFieldConfig); // if `groupFieldConfig` is field name, not config
  var conjs = groupFieldConfig && groupFieldConfig.conjunctions || Object.keys(config.conjunctions);
  if (conjs.length == 1) return conjs[0];
  // Line to add
  if (conjs.length !== 0 && !conjs.includes(config.settings.defaultGroupConjunction)) return conjs[0];
  // End of line to add
  return config.settings.defaultGroupConjunction || config.settings.defaultConjunction || conjs[0];
};

Happy to create a pull request if this is of any help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants