UNPKG

@cowwoc/requirements

Version:

A fluent API for enforcing design contracts with automatic message generation.

43 lines 2.05 kB
import { AbstractCollectionValidator, ValidationTarget, Pluralizer, collectionIsSorted, ObjectSizeValidatorImpl } from "../internal.mjs"; import isEqual from "lodash.isequal"; /** * Default implementation of `ArrayValidator`. */ class ArrayValidatorImpl extends AbstractCollectionValidator { /** * @param scope - the application configuration * @param configuration - the validator configuration * @param name - the name of the value * @param value - the value * @param pluralizer - the type of items in the array * @param context - the contextual information set by a parent validator or the user * @param failures - the list of validation failures * @throws TypeError if `name` is `undefined` or `null` * @throws RangeError if `name` contains whitespace, or is empty * @throws AssertionError if `scope`, `configuration`, `value`, `context` or `failures` are null */ constructor(scope, configuration, name, value, pluralizer, context, failures) { super(scope, configuration, name, value, pluralizer, context, failures); } isSorted(comparator) { const sorted = this.value.undefinedOrNullToInvalid().map(v => { const valueAsList = this.collectionAsArray(v); const sortedList = [...valueAsList]; sortedList.sort(comparator); if (isEqual(valueAsList, sortedList)) return null; return sortedList; }).or(null); if (sorted !== null) { this.failOnUndefinedOrNull(); this.addRangeError(collectionIsSorted(this, sorted).toString()); } return this; } size() { this.failOnUndefinedOrNull(); return new ObjectSizeValidatorImpl(this.scope, this._configuration, this, this.name + ".size()", this.value.undefinedOrNullToInvalid().map(v => v.length), this.pluralizer, this.context, this.failures); } } export { ArrayValidatorImpl }; //# sourceMappingURL=ArrayValidatorImpl.mjs.map