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
.
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.
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.