logo

Validation

LCAx's validation engine enables users to define and run custom rules to on a LCAx project.

The validate function takes in a LCAx project and a list of validation rules (ValidationSchema) to check the project against.

A validation schema should contain the following three properties:

ValidationSchema

level: 'project' | 'assembly' | 'product' | 'impactData'
field: string
rule: ValidationRule

The fields are:

  • level: The four levels corresponds to LCAx's data structure levels and the value determines where the rule should be applied.
  • field: Which field the rule should be applied to. If the field is optional (meaning it can be null), then a ? should be added to the name.
  • rule: The validation rule to apply.

ValidationRule

range: [number, number] | null
includes: string | null
required: boolean | null
equal: string | null
greater: number | null
less: number | null
oneOf: string[] | null

The fields are:

  • range: Check that a field has a value between two numbers.
  • includes: Check that a field (of a string or a list) includes a certain value.
  • required: Check that a field is not null or empty.
  • equal: Check that a field is equal to a value.
  • greater: Check that a field is greater than a value.
  • less: Check that a field is less than a value.
  • oneOf: Check that a field has one of the values.

Simple Project Validation

In the example below we make a simple validation that the project's name is equal to Test eksempel.

When all validation rules pass the validate function will return true.

from lcax import ValidationSchema, validate, Level, ValidationRule, Project
from pathlib import Path

project = Project.loads(Path("project.json").read_text())

schemas = [
    ValidationSchema(**{
        "level": Level.Project,
        "field": "name",
        "rule": ValidationRule(**{"equal": "Test eksempel"})
    })
]

result = validate(project, schemas) # result = []

Validation Errors

If the validation is not successful then the validate function will throw an error, containing information on the failed fields.

In this example we will validate that the project's name is equal to Te eksempel, which it is not.

from lcax import ValidationSchema, validate, Level, ValidationRule, Project
from pathlib import Path

project = Project.loads(Path("project.json").read_text())

schemas = [
    ValidationSchema(**{
        "level": Level.Project,
        "field": "name",
        "rule": ValidationRule(**{"equal": "Te eksempel"})
    })
]

result = validate(project, schemas)
# result = [{ field: "name", message: "Field is not equal to: Te eksempel" }]

Nested and Optional Fields

In this example we want to validate that the location of the project is in Denmark. That information is in location.country and we can create a validation rule that checks that the field is equal to dnk (LCAx uses ISO3166-3 to represent countries).

We also want to validate that the gross floor area of the project is greater than 50, so we create a rule for that. Since projectInfo is an optional field (meaning it can be null), we have to include ? in the field name.

from lcax import ValidationSchema, validate, Level, ValidationRule, Project
from pathlib import Path

project = Project.loads(Path("project.json").read_text())

schemas = [
    ValidationSchema(**{
        "level": Level.Project,
        "field": "location.country",
        "rule": ValidationRule(**{"equal": "dnk"})
    }),
    ValidationSchema(**{
        "level": Level.Project,
        "field": "projectInfo?.grossFloorArea?.value",
        "rule": ValidationRule(**{"greater": 50.0})
    })
]

result = validate(project, schemas) # result = []