import { inspect } from 'util' ; import { ArrayType } from './ArrayType' ; import type { Platform } from '../platforms' ; import { ValidationError } from '../errors' ; function mapHydrator < T > ( items , hydrate ) { if ( items && items . length > 0 && typeof items [ 0 ] === 'number' ) { return ( i ) => + i as unknown as T ; } return hydrate ; } export class EnumArrayType < T extends string | number = string > extends ArrayType < T > { constructor ( private readonly owner , private readonly items ? , hydrate = i => i as T ) { super ( mapHydrator ( items , hydrate ) ) ; } convertToDatabaseValue ( value , platform , fromQuery ? ) { if ( Array . isArray ( value ) && Array . isArray ( this . items ) ) { const invalid = value . filter ( v => ! this . items ! . includes ( v ) ) ; if ( invalid . length > 0 ) { throw new ValidationError ( `Invalid enum array items provided in ${ this . owner }: ${ inspect ( invalid ) }` ) ; } } return super . convertToDatabaseValue ( value , platform , fromQuery ) ; } }