strictYAML is a type of safety parser used to parse and write as per YAML specifications.

It provides Strict type safety validation based on YAML Subset specification. There are a lot of yaml libraries in Python

  • pyyaml standard package: It is not secure and does not type safety
  • strictyaml : Type safety, order, No duplicate keys

strictyaml python features:

  • Keys are ordered and stored
  • Scalar types such as string numbers are allowed strictly
  • Duplicate keys are not allowed
  • Strict key validation
  • No Empty scalars: Empty strings or numbers are not allowed. It ensures the key has nonempty values.
  • Follows Schema contract: Allows only properties defined in the schema, Throws a YAML Error if any nonfields are added.
  • Consistent Indendation: Enforces consistent indentation across yaml file content, No space allowed for scalar types. Tabs are not allowed.
  • Not Empty, Allow Null scalar values: Empty values are not allowed, Allows null values instead of null values.
  • Inheritance properties are not allowed.

StrictYAML Python Example

This explains reading yaml content, doing validation, and throwing an error if there is an error.

First, Install the package using the pip

pip install strictyaml

Import load and YAMLValidationError in the code from the package.

It provides a load function. It is similar to standard load functions.

This load function does validation while parsing the content.

Here is an example of Python strictyml read content.

from strictyaml import load, YAMLValidationError

content = """
host: localhost
port: 80
username: root

    # Load YAML content using strictyaml
    results = load(content)
except YAMLValidationError as e:
    print(f"YAML Validation Error: {e}")