You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Similar to the existing properties minLength and minLength add support for properties uniqueItems and sorted.
Sorted should accept a selector function to specify the value to sort on (perhaps also sort order).
uniqueItems boolean true (or perhaps an optional selector function to only enforce uniqueness of a specific value)?
With this feature we would no longer need to define custom types like:
/** * An array with length > 0 and distinct values. * Allows custom comparator function to specify 'distinct' values, defaults to 'isEqual', which performs a deep comparison between two values for equality comparison. * Note that although array length is validated, the array element(s) itself may still be falsy (undefined etc.) depending on the elementType. * WARNING: Be sure to document the distinct behavior in de OpenApiMetadata of the type you are using this with so API consumers are aware * of this restriction * @param elementType the type of the array elements. */typeNonEmptyDistinctArray<T>=Branded<T[],'NonEmptyDistinctArray'>;functionNonEmptyDistinctArray<T>(elementType: Type<T>,comparatorFn: Comparator<T>=isEqual): Type<NonEmptyDistinctArray<T>>{consttype=NonEmptyArray<T>(elementType)// Set the brand to plain `NonEmptyDistinctArray`:.withConstraint('NonEmptyDistinctArray',value=>{returnuniqWith(value,comparatorFn).length===value.length||'no duplicate values allowed';});// But set the runtime name to be more specific:Object.defineProperty(type,'name',{value: `NonEmptyDistinctArray<${elementType.name}>`});returntype;}typeNonEmptyDistinctSortedDates<T>=Branded<T[],'NonEmptyDistinctSortedDates'|'NonEmptyDistinctArray'>;functionNonEmptyDistinctSortedDates<TextendsISODate>(elementType: Type<T>){consttype=NonEmptyDistinctArray<T>(elementType)// Set the brand to plain `NonEmptyDistinctSortedDates`:.withConstraint('NonEmptyDistinctSortedDates',value=>{returnvalue.every(pairwise((next,prev)=>!prev||next>prev))||'should be sorted by date';});// But set the runtime name to be more specific:Object.defineProperty(type,'name',{value: `SortedByDate<${elementType.name}>`});returntype;}
with:
/** * An array with length > 0. * Note that although array length is validated, the array element(s) itself may still be falsy (undefined etc.) depending on the elementType. * @param elementType the type of the array elements. */exportfunctionNonEmptyArray<T>(elementType: Type<T>): Type<T[]>{returnarray(elementType).withConfig(`NonEmptyArray<${elementType.name}>`,{minLength: 1,customMessage: `expected at least one ${elementType.name}`,});}
The text was updated successfully, but these errors were encountered:
Similar to the existing properties
minLength
andminLength
add support for propertiesuniqueItems
andsorted
.uniqueItems
booleantrue
(or perhaps an optional selector function to only enforce uniqueness of a specific value)?With this feature we would no longer need to define custom types like:
with:
The text was updated successfully, but these errors were encountered: