Data Conversion

Data Type Conversion

Some data types support data type conversion after a successful validation.

For example, a float value 1.87 can be converted into a string '1.87' or an integer 1.

Empty Value Replacement

Some data types support replacing of empty values to a given value. This can be handsome by pre-processing some weird API return data.

For example, an empty string can be replaced with an empty list.

Code Example

import yaml
import nadap

schema_definition_yaml = """
root:
  type: dict
  keys:
    no_conversion: int
    convert_type:
      type: int
      convert_to: str
    round_float:
      type: float
      round_to_decimals: 2
    replace_empty:
      type: list
      replace_empty_to: ""
"""

n = nadap.Nadap()
schema_def = yaml.load(schema_definition_yaml, Loader=yaml.SafeLoader)
n.schema = schema_def

converted_data = n.validate(
    {
        "no_conversion": 1,
        "convert_type": 2,
        "replace_empty": [],
        "round_float": 11.02263,
    },
    flags=nadap.CONVERT_DATA
  )

print(yaml.dump(converted_data))

... will print this output:

convert_type: '2'
no_conversion: 1
replace_empty: ''
round_float: 11.02