11import equal from 'fast-deep-equal' ;
22import { registerDecorator , ValidationArguments , ValidationOptions } from 'class-validator' ;
33
4+ import { TIdentifierNamesCache } from '../../types/TIdentifierNamesCache' ;
5+ import { TIdentifierNamesCacheDictionary } from '../../types/TIdentifierNamesCacheDictionary' ;
6+
47import { IOptions } from '../../interfaces/options/IOptions' ;
58
69import { DEFAULT_PRESET } from '../presets/Default' ;
710
811/**
9- * @param {"string" | "number" } valuesType
12+ * @param value
13+ * @returns {boolean }
14+ */
15+ const validateDictionary = ( value : unknown | TIdentifierNamesCacheDictionary ) : boolean => {
16+ if ( typeof value !== 'object' ) {
17+ return false ;
18+ }
19+
20+ if ( value === null ) {
21+ return false ;
22+ }
23+
24+ const objectValues : unknown [ ] = Object . values ( value ) ;
25+
26+ if ( ! objectValues . length ) {
27+ return true ;
28+ }
29+
30+ for ( const objectValue of objectValues ) {
31+ if ( typeof objectValue !== 'string' ) {
32+ return false ;
33+ }
34+ }
35+
36+ return true ;
37+ } ;
38+
39+ /**
1040 * @param {ValidationOptions } validationOptions
1141 * @returns {(options: IOptions, propertyName: keyof IOptions) => void }
1242 */
13- export function IsPrimitiveDictionary (
14- valuesType : 'string' | 'number' ,
43+ export function IsIdentifierNamesCache (
1544 validationOptions ?: ValidationOptions
1645) : ( options : IOptions , propertyName : keyof IOptions ) => void {
1746 return ( optionsObject : IOptions , propertyName : keyof IOptions ) : void => {
1847 registerDecorator ( {
1948 propertyName,
20- constraints : [ valuesType ] ,
21- name : 'IsPrimitiveDictionary ' ,
49+ constraints : [ ] ,
50+ name : 'IsIdentifierNamesCache ' ,
2251 options : validationOptions ,
2352 target : optionsObject . constructor ,
2453 validator : {
@@ -27,7 +56,7 @@ export function IsPrimitiveDictionary (
2756 * @param {ValidationArguments } validationArguments
2857 * @returns {boolean }
2958 */
30- validate ( value : IOptions [ keyof IOptions ] , validationArguments : ValidationArguments ) : boolean {
59+ validate ( value : unknown , validationArguments : ValidationArguments ) : boolean {
3160 const defaultValue : IOptions [ keyof IOptions ] | undefined = DEFAULT_PRESET [ propertyName ] ;
3261 const isDefaultValue : boolean = equal ( value , defaultValue ) ;
3362
@@ -39,26 +68,22 @@ export function IsPrimitiveDictionary (
3968 return false ;
4069 }
4170
42- const objectValues : unknown [ ] = Object . values < unknown > ( value ) ;
43-
44- if ( ! objectValues . length ) {
45- return true ;
71+ if ( value === null ) {
72+ return false ;
4673 }
4774
48- for ( const objectValue of objectValues ) {
49- if ( typeof objectValue !== 'string' ) {
50- return false ;
51- }
75+ if ( ! validateDictionary ( ( < TIdentifierNamesCache > value ) ?. globalIdentifiers ) ) {
76+ return false ;
5277 }
5378
54- return true ;
79+ return validateDictionary ( ( < TIdentifierNamesCache > value ) ?. propertyIdentifiers ) ;
5580 } ,
5681
5782 /**
5883 * @returns {string }
5984 */
6085 defaultMessage ( ) : string {
61- return ` Passed value must be a dictionary with \` ${ valuesType } \` values or \ `null\ ` value` ;
86+ return ' Passed value must be an identifier names cache object or `null` value' ;
6287 }
6388 }
6489 } ) ;
0 commit comments