The constraints framework implementation is located in the utils/constraints-utils.ts module
import * from 'utils/constraints-utils';
The constraints framework provides a set of generic classes and interfaces as well as the invocation framework to validate constraints on arbitrary objects.
This file holds the supported constraints and function(s) to validate constraints defined below in the rest of this document.
Constructor:
new StringConstraint(minValue, maxValue);
API reference 'here'
If given string length falls outside of these inclusive bounds throws detailed Zod error
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
Constructor:
new NumberConstraint(minValue, maxValue);
API reference 'here'
If given number falls outside of these inclusive bounds throws detailed Zod error.
Constructor:
new utils.ArrayConstraint(minValue, maxValue);
API reference 'here'
If given array length falls outside of these inclusive bounds throws detailed Zod error.
Constructor:
new GenericRegexStringConstraint(new RegExp(regexString));
If given string does not match the regular expression throws detailed Zod error.
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.
Constructor:
new InternetHostStringConstraint();
If given string does not match RFC 1123 standards throws detailed Zod error.
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.
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);
Currently, constraints can be defined for flat objects, and nested structures will require individual validations.