23andMe / Yamale

A schema and validator for YAML.
MIT License
666 stars 88 forks source link

boolean validator to validate value is always set to either true or false. #222

Closed OmkarG1986 closed 1 year ago

OmkarG1986 commented 1 year ago

Boolean validator should have option to validate if set boolean value is either true or false as per the requirement.

e.g. I have one flag based boolean field "create_iac_project". This field is always required to set to true. Withe below boolean validation in schema it doesnt serve the purpose as validation passes even if the filed value is set to boolean false.

create_iac_project : bool(required=True)

As a WA i tried to use enum validator but enum treats the arguments of type string instead of boolean and i couldnt find anyways to pass type of the argument to enum.

create_iac_project : enum('false', ignore_case=True)

{"ok": false, "errors": {"/rules/example.yaml": ["create_iac_project: 'False' not in ('false',)"

my Data/yaml file that I am validating looks like below:

# Flag
create_project       : false                      # Flag for base project creation. Will stop creation of any resources if set to false
create_iac_project   : false                       # Flag for IaC project creation
create_subnet        : false                      # Flag for subnet creation
create_os_patch      : false                       # Flag for OS Patch Manager 
mechie commented 1 year ago

Have you tried giving enum a Python Boolean? Like so:

create_iac_project : enum(False)
mildebrandt commented 1 year ago

Right, @mechie is correct here. There are two things that were missed when trying to use the enum validator:

  1. The validator doesn't have an ignore_case keyword.
  2. A string was used instead of a boolean.

https://github.com/23andMe/Yamale#enum---enumprimitives

Similarly, the required keyword that was used in the bool validator indicates that this field is required to be present in the yaml. It doesn't indicate what the value of the field is supposed to be. https://github.com/23andMe/Yamale#validators

Try the following schema:

create_iac_project: enum(True)

That should get you what you want.

OmkarG1986 commented 1 year ago

Thank you @mildebrandt @mechie. That was a good catch. I can confirm by using enum(True)/enum(False), Its working as expected. Appreciate your quick response.

I will mark this issue as closed.