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

Allow Rule.custom to specify severity when reporting multiple #8294

Open
blended-bram opened this issue Jan 16, 2025 · 1 comment
Open

Allow Rule.custom to specify severity when reporting multiple #8294

blended-bram opened this issue Jan 16, 2025 · 1 comment

Comments

@blended-bram
Copy link

Is your feature request related to a problem? Please describe.

It is annoying that reaching for Rule.custom is not enough to hand-pick the severity. In particular when mixing logic that may produce both errors and warnings. This results in unneeded complexity (as the same logic to inspect the value is split across different rules).
Multiple rules are needed and need to be nested where already using Rule.custom should permit maximal customization.

Creating custom warnings, mixed with errors, currently requires:

Rule.all([
  Rule.custom(/* warnings impl */).warning(),
  Rule.custom(/* errors impl */),
])

I guess one could make a helper function that splits an implementation:

function makeCustomRule(Rule: Rule, fn: (value, context) => ValidationWithSeverity[]): Rule {
  return Rule.all([
    Rule.custom((value, context) => fn(value, context).filter(x => (x.severity ?? 'error') == 'error')).error(),
    Rule.custom((value, context) => fn(value, context).filter(x => (x.severity ?? 'error') == 'warning')).warning(),
  ])
}

Describe the solution you'd like

interface ValidationError {
  // New addition:
  severity?: 'warning' | 'error' /* | ... */

  // Current operational fields:
  message: string;
  path?: Path;
}

Then use it like so:

Rule.custom((value) => {
  const failures: ValidationError[] = [];
  if (/* ... */) {
    failures.push({ message: 'My warning', severity: 'warning' }) // Overrides severity of the rule definition.
  }
  if (/* ... */) {
    failures.push({ message: 'My error' }) // no severity field: use severity from rule definition
  }
})
@cngonzalez
Copy link
Member

Hi @blended-bram ! Yes, I understand the duplicated logic probably feels a bit frustrating. I'm forwarding this feature request to our studio team. Thanks very much for submitting it!

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