Ludwig.Validation

This module provides the means to validate values and data in a flexible way.

Here is a simple stand-alone example:

# This is a simple mock type to illustrate a simple validation
type SecurityGroup:
  | SecurityGroup {
      name: String,
      openPorts: List<Int>
    }

# Define the validation
fun dontAllowOpenPort22(sg: SecurityGroup) -> Validation:
  if List.any(fun(port): port == 22, sg.SecurityGroup.openPorts) then
    Validation.error {message: "Don't open port 22 to the world."}
  else
    Validation.success

# Register the validation
validate dontAllowOpenPort22

# Create a resource.  This will throw a validation error.
sg: SecurityGroup {
  name: "My example security group",
  openPorts: [8000, 80, 22, 443]
}

Module Members

addPrefix

(Function)

Add a prefix to the error message. In the case that this is a successful validation, we don’t do anything.

Type Signature

 fun (String, Validation) -> Validation
Argument: prefix
Type: String
Argument: val
Type: Validation
Returns:
Type: Validation

and

(Function)

Combine two validations, ensuring that they both pass. If both are failing validations, we will end up with a validation that contains more than one error.

Type Signature

 fun (Validation, Validation) -> Validation
Argument: x
Type: Validation
Argument: y
Type: Validation
Returns:
Type: Validation

assert

(Function)

Create a ‘Validation’ from a ‘Bool’ value. True results in a successful validation and False results in a failing validation with the given message.

Type Signature

 fun (String, Bool) -> Validation
Argument: message
Type: String
Argument: test
Type: Bool
Returns:
Type: Validation

error

(Function)

Raise a validation failure.

Type Signature

 fun { message: String,
       when: Optional<Bool>,
       title: Optional<String>,
       problems: Optional<List<a>> } -> Validation
Argument: message

Description of the problem.

Type: String

Argument: when

Only raise the error when this condition is true. Defaults to True.

Type: Optional<Bool>

Argument: title

Title for the error. Defaults to “Validation failed”.

Type: Optional<String>

Argument: problems

The offending node values. This should only be used for NodeStream validations. In these cases, the compiler will be able to extract a stack and point to the right source code location from these values.

Type: Optional<List<a>>

Returns:

A failing Validation.

Type: Validation

failure

(Function)

Raise a validation failure.

DEPRECATED: Please use error instead.

Type Signature

 fun (String) -> Validation
Argument: err
Type: String
Returns:
Type: Validation

getFailureMessage

(Function)

Get the failure message. If the validation is successful, this will return the empty string.

Type Signature

 fun (Validation) -> String
Argument: val
Type: Validation
Returns:
Type: String

isFailure

(Function)

Check if a validation failed. Warnings are not considered failures.

Type Signature

 fun (Validation) -> Bool
Argument: val
Type: Validation
Returns:
Type: Bool

isSuccess

(Function)

Check if a validation was successful.

Type Signature

 fun (Validation) -> Bool
Argument: val
Type: Validation
Returns:
Type: Bool

join

(Function)

Reduce a list of Validations into a single value using and. This is equivalent (but sometimes more convenient) than registering all validations separately.

Type Signature

 fun (List<Validation>) -> Validation
Argument: validations

The list of validations to combine.

Type: List<Validation>

Returns:

A single validation that is the result of anding all of the supplied validations together.

Type: Validation

optional

(Function)

Apply a validation on an optional. If there is no value, the ‘Validation’ is successful.

Type Signature

 fun (fun (a) -> Validation, Optional<a>) -> Validation
Argument: f
Type: fun ( a) -> Validation
Argument: x
Type: Optional<a>
Returns:
Type: Validation

toErrorAt

(Function)

Convert the validation to an error using errorAt (if there is any). If the validation is successful, just return the unit ().

This function is mostly meant to be used internally and should be avoided if possible.

Type Signature

 fun (Int, Validation) -> 0-tuple
Argument: n
Type: Int
Argument: val
Type: Validation
Returns:
Type: 0-tuple

warning

(Function)

Raise a validation warning.

Type Signature

 fun { message: String,
       when: Optional<Bool>,
       title: Optional<String>,
       problems: Optional<List<a>> } -> Validation
Argument: message

Description of the problem.

Type: String

Argument: when

Only raise the warning if this condition is true. Defaults to True.

Type: Optional<Bool>

Argument: title

Title for the warning. Defaults to “Validation warning”.

Type: Optional<String>

Argument: problems

The offending node values. This should only be used for NodeStream validations. In these cases, the compiler will be able to extract a stack and point to the right source code location from these values.

Type: Optional<List<a>>

Returns:

A warning Validation.

Type: Validation

Validation

(Type)

type Validation:
  | Success
  | Failure String
  | V2 List<Problem>

Failure

(Constructor)

Argument:
Ludwig.String

Success

(Constructor)

V2

(Constructor)

Argument:
Ludwig.List<type_Ludwig_Validation_Problem>

The validation type. This models the result of a validation.

DEPRECATED: The constructors of this type should not be used directly. Use success, error or warning instead.

success

(Value)

Validation success:
  V2([])

Return a Success value.

Type Signature

 Validation