import { EntityAliasOrString , InvalidAttributeAliasSchemaError , NoSuchAttributeExistsError , } from '@typedorm/common' ; import { regexInterpolatedWord } from './constants' ; import { isKeyOfTypeAliasSchema } from './is-key-of-type-alias-schema' ; param key param dict export function validateKey ( key , dict , entityName ? ) { if ( isKeyOfTypeAliasSchema ( key ) ) { if ( typeof key . alias !== 'string' ) { throw new InvalidAttributeAliasSchemaError ( key . alias as any ) ; } const aliasType = dict [ key . alias ] ; if ( ! aliasType ) { throw new NoSuchAttributeExistsError ( key . alias , entityName ) ; } return ; } const matchIterator = key . matchAll ( regexInterpolatedWord ) ; validateMatch ( key , matchIterator , dict ) ; return ; } function validateMatch ( key , iterator , dict ) { const next = iterator . next ( ) ; if ( next . done ) { return ; } const variable = next . value [ 1 ] ; const variableType = dict [ variable ] ; if ( ! variableType ) { throw new Error ( `key "${ key }" references variable "${ variable }" but it could not be resolved` ) ; } if ( variableType !== 'String' && variableType !== 'Number' && variableType !== 'Boolean' ) { throw new Error ( `"${ variable }" is used in key ${ key }, thus it's type must be or scalar type, if attribute type is Enum, please set "isEnum" to true in attribute decorator.` ) ; } validateMatch ( key , iterator , dict ) ; }