logo

Advanced Validation Example

In the previous section we saw how simple rules can be built using code.

Sometimes when you want to create more elaborate validation schemas it might an advantage to so in text. For this you can i.e. use YAML, which has a more readable syntax than fx. JSON.

Using YAML to Define Rules

In the example below we create an elaborate set of validation schemas in YAML. In the code examples when then read the YAML file and pass the validation schema objects to the validate function.

- level: project
  field: name
  rule:
    required: true
- level: project
  field: referenceStudyPeriod
  rule:
    equal: '50'
- level: project
  field: lifeCycleModules
  rule:
    includes: a1a3
- level: project
  field: location.address
  rule:
    includes: Testvej
- level: project
  field: projectInfo?.grossFloorArea?.value
  rule:
    greater: 50
- level: project
  field: projectInfo?.floorsAboveGround
  rule:
    less: 2
- level: project
  field: projectInfo?.buildingType
  rule:
    equal: new_construction_works
- level: assembly
  field: id
  rule:
    required: true
- level: assembly
  field: quantity
  rule:
    range:
      - 1
      - 100
- level: product
  field: name
  rule:
    required: true
- level: product
  field: unit
  rule:
    equal: kg
- level: impactData
  field: name
  rule:
    required: true
- level: impactData
  field: source?.name
  rule:
    equal: 'Ökobau.dat'
- level: impactData
  field: subtype
  rule:
    oneOf: ['generic', 'specific']

Validation JSON Schema

If you wish to write your validation rules in YAML, you can then use the JSON Schema for the validation to provide help and feedback on the available fields.

If you use an editor like VS Code or PyCharm to write your YAML you can plug the validation JSON schema in there.

You can find the validation JSON schema here.