import { ValidateBy , ValidationArguments , ValidationOptions , } from 'class-validator' ; export const ARRAY_AT_LEAST_ONE = 'ArrayAtLeastOne' ; export function arrayAtLeastOne ( allowed , values ) { return allowed . some ( ( elem ) => values . includes ( elem ) ) ; } export class ArrayAtLeastOneConstraint { getAllowedList ( args ) { return args . constraints [ 0 ] ; } validate ( values , args ) { const allowed = this . getAllowedList ( args ) ; return Array . isArray ( values ) && arrayAtLeastOne ( allowed , values ) ; } defaultMessage ( args ) { const values = args . value ; const allowed = this . getAllowedList ( args ) ; const output = Array . isArray ( values ) ? values . join ( ', ' ) : `${ values }` ; return `${ args . property } allows only theses values: "${ allowed . join ( ', ' ) }", got: "${ output }"` ; } } export function ArrayAtLeastOne ( allowed , validationOptions ? ) { return ValidateBy ( { name : ARRAY_AT_LEAST_ONE , constraints : [ allowed ] , validator : ArrayAtLeastOneConstraint , } , validationOptions ) ; }