Skip to content

validator: make rules exported and allow custom set of rules for Validate#320

Merged
StevenACoffman merged 2 commits intovektah:masterfrom
kgrigorev:export_validator_rules
Sep 26, 2024
Merged

validator: make rules exported and allow custom set of rules for Validate#320
StevenACoffman merged 2 commits intovektah:masterfrom
kgrigorev:export_validator_rules

Conversation

@kgrigorev
Copy link
Copy Markdown
Contributor

@kgrigorev kgrigorev commented Sep 23, 2024

validator.Validate function uses predefined set of rules, which are populated during package initialisation. While it is possible to add custom rules via AddRule, it is not possible to have completely customised set of rules (e.g. if someone wants to delete or replace some predefined rule). The javascript implementation allows to pass custom set of rules and falls back to the default set only if the parameter is null (https://github.com/graphql/graphql-js/blob/bd2fe71d2a7ca8619ec71bae919692ce61e10958/src/validation/validate.ts#L40). This PR converts every rule to the package-level variable and allows to pass custom set of rules to the Validate function as varargs. It also falls back to predefined set of rules for the backward compatibility, so that none of the existing code is broken. With that change validation can be customized:

errList := validator.Validate(s, q, []validator.Rule{rules.FieldsOnCorrectTypeRule, rules.VariablesInAllowedPositionRule}...)

Describe your PR and link to any relevant issues.

I have:

  • Added tests covering the bug / feature
  • Updated any relevant documentation

@coveralls
Copy link
Copy Markdown

coveralls commented Sep 23, 2024

Coverage Status

coverage: 88.168% (+0.1%) from 88.038%
when pulling a4cc52c on kgrigorev:export_validator_rules
into ed10d5c on vektah:master.

@StevenACoffman
Copy link
Copy Markdown
Collaborator

Thanks! One thing to note is that changing the package variable will not be safe for tests that run in parallel, as well as in production code.

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

Successfully merging this pull request may close these issues.

3 participants