8000 Add custom validator to blueprint variables by phobologic · Pull Request #218 · cloudtools/stacker · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add custom validator to blueprint variables #218

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

Merged
merged 4 commits into from
Sep 8, 2016
Merged

Conversation

phobologic
Copy link
Member
  • Now all blueprint variables must provide a type, otherwise an
    exception is thrown.

Before it wasn't required, and the type was ignored if it wasn't
provided. That didn't seem right to me, but I realize I may just not
have understood the implications.

Other changes:

  • variable resolution now done in a function not a class method
  • moved variable type validation to a function
  • instead of passing the whole blueprint just to use it's name, I just
    pass the blueprints name into the exceptions that use them (that was
    all the blueprint was used for it appeared)

I'll add docs in the next checkin.

- Now all blueprint variables must provide a `type`, otherwise an
  exception is thrown.

Before it wasn't required, and the type was ignored if it wasn't
provided. That didn't seem right to me, but I realize I may just not
have understood the implications.

Other changes:

- variable resolution now done in a function not a class method
- moved variable type validation to a function
- instead of passing the whole blueprint just to use it's name, I just
  pass the blueprints name into the exceptions that use them (that was
  all the blueprint was used for it appeared)

I'll add docs in the next checkin.
@phobologic
Copy link
Member Author

@mhahn @jeffpierce

@phobologic
Copy link
Member Author

Fixes #217


# If no validator, return the value as is, otherwise apply validator
validator = var_def.get("validator", lambda v: v)
value = validator(value)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should have a preferred exception that validators raise that gets caught here. Then we can include more details about which variable was invalid etc.

Right now if you raise an exception within the validator, you can't tell which variable was being validated.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or maybe just any exception that is raised by the validator we wrap with some other info?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call - I'll think about it and come up with something that makes it easy to tell which variable failed it's validator, and why.

Makes it a bit more explicit when there is an issue with a validator.
try:
value = validator(value)
except Exception as exc:
raise ValidatorError(var_name, validator.__name__, value, exc)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add this to docstring above

@mwildehahn
Copy link
Contributor

👍

@mwildehahn
Copy link
Contributor

:shipit:

@phobologic phobologic merged commit 6a141b6 into master Sep 8, 2016
@phobologic phobologic deleted the add_validators branch September 8, 2016 20:19
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.

2 participants
0