Skip to content

How to use the framework

The constraints framework implementation is located in the utils/constraints-utils.ts module

import * from 'utils/constraints-utils';

What can you use with the framework

The constraints framework provides a set of generic classes and interfaces as well as the invocation framework to validate constraints on arbitrary objects.

Validations in constraints-utils.ts

This file holds the supported constraints and function(s) to validate constraints defined below in the rest of this document.

StringConstraint

Constructor:

new StringConstraint(minValue, maxValue);

API reference 'here'

If given string length falls outside of these inclusive bounds throws detailed Zod error

UrlStringConstraint

Constructor:

new UrlStringConstraint(minValue, maxValue);

API reference 'here'

If given string length falls outside of these inclusive bounds, or does not follow a proper URL format it throws detailed Zod error

NumberConstraint

Constructor:

new NumberConstraint(minValue, maxValue);

API reference 'here'

If given number falls outside of these inclusive bounds throws detailed Zod error.

ArrayConstraint

Constructor:

new utils.ArrayConstraint(minValue, maxValue);

API reference 'here'

If given array length falls outside of these inclusive bounds throws detailed Zod error.

GenericRegexStringContraint

Constructor:

new GenericRegexStringConstraint(new RegExp(regexString));

If given string does not match the regular expression throws detailed Zod error.

CompositeConstraint

Constructor:

new CompositeConstraint(...constraints);

If given value does not comply with each of the constraints in the list throws detailed Zod error for first failure.

InternetHostStringConstraint

Constructor:

new InternetHostStringConstraint();

If given string does not match RFC 1123 standards throws detailed Zod error.

validateConstraints Function

validateConstraints<T>(constraints: ConstraintsType<T>, context: string, ...object: any)
This is the entry point to use the framework. This function can validate either a single object or an array of objects against the provided constraints.

How to use the constraints-utils.ts

You need two things when utilizing constraints-utils.ts and the following examples are from 'here'

First you need a class with specified keys assigned to given constraints.

Example with two keys:

export class BlueprintPropsConstraints implements ConstraintsType<EksBlueprintProps> {
    id = new StringConstraint(1, 63);
    name = new StringConstraint(1, 63);

Second you need to call the validateConstraints function:

Example (note: punctuation, formatting):

validateConstraints(new BlueprintPropsConstraints, EksBlueprintProps.name, blueprintProps);

Limitations

Currently, constraints can be defined for flat objects, and nested structures will require individual validations.