Skip to content

Latest commit

 

History

History
40 lines (34 loc) · 946 Bytes

constraints.md

File metadata and controls

40 lines (34 loc) · 946 Bytes

Constraints

A constraint limits the value of a certain type.
The implementation of the constraint is done using a javascript function, which is why constraints can only be loaded using javascript descriptors.
The constraint descriptor defines on which types the constraint can be applied, if it has parameters and the function to invoke for validating the value.

Examples:

{
	// ...
	palindrome: {
		types: ["number", "string"],
		validator: () => {
			return function (value: number | string): boolean {
				if (typeof value === "number") {
					if (value % 1 !== 0) {
						return false;
					}
			
					value = value.toString();
				}
			
				if (value.length % 2 === 1) {
					return false;
				}
			
				for (let i = 0; i < value.length / 2; i++) {
					if (value[i] !== value[value.length - i - 1]) {
						return false;
					}
				}
			
				return true;
			}
		}
	}
}