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

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

Merged
merged 2 commits into from
Sep 26, 2024

Conversation

kgrigorev
Copy link
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

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