Skip to content

DRY validators? #1754

Answered by sisp
JohnLyman asked this question in Q&A
Aug 26, 2024 · 1 comments · 3 replies
Discussion options

You must be logged in to vote

In the case for validators, it's still a lot of boilerplate because I think you'd still have to pass the variable name to the macro?

That's right. But it makes validator reuse more flexible, e.g. when you have a parametrized validator or one that needs more than just the answer to the question for which you're setting the validator. For instance:

age:
  validator: >-
    {% from 'validators.jinja' import validate_int %}
    {% if validate_int(age, min=0) %}
    Must be at least 0
    {% endif %}"

If you find the long line too verbose, you may use a multiline YAML string like this (at least it's more readable IMO):

int1:
  validator: >-
    {% from 'positive_int.jinja' import positive_in…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@JohnLyman
Comment options

@sisp
Comment options

sisp Aug 27, 2024
Collaborator

Answer selected by JohnLyman
@JohnLyman
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants