Skip to content

customValidator is not provided via NG_VALIDATORS or NG_ASYNC_VALIDATORS #271

@anteburazer

Description

@anteburazer

I have a problem with custom validators but only when building application for production. The problem is in dynamic-form.service.ts file when code enters in getCustomValidatorFn() function.

I'm using Angular CLI with webpack: v1.0.0-beta.31
Angular v2.4.3

The problem appears when code gets uglified and variable names are renamed. Function getCustomValidatorFn() compares two strings which are not equal because of code uglification and because of that it doesn't find anything in NG_VALIDATORS array.

I hope the code below explains the problem. I put console.log() to output breaking lines of code to understand where the issue is. If it's not clear I will explain more clear.

Right now I'm not able to use the library in production.

getCustomValidatorFn(validatorName: string): ValidatorFn | AsyncValidatorFn | undefined  {

        console.debug(validatorName) // myCustomValidator

        let validatorFn;

        console.log(this.NG_VALIDATORS)
        // Array[1]: 
        //     i(t)
        //       arguments(...)
        //       arguments(caller)
        //       length: 1
        //       name: "i"

        if (this.NG_VALIDATORS) {
            validatorFn = this.NG_VALIDATORS.find(validator => {
                console.log(validatorName); // myCustomValidator
                console.log(validator.name); // i
                console.log( validatorName == validator.name) // false 

                // THIS IS CRITICAL LINE OF CODE WHERE FUNCTION NAME IN "this.NG_VALIDATORS"
                // IS EQUAL TO "i" AND FUNCTION PARAMETER "validatorName" IS EQUAL TO "myCustomValidator"
                return validatorName == validator.name
            });

            console.debug(validatorFn) // undefined
        }

        if (!isDefined(validatorFn) && this.NG_ASYNC_VALIDATORS) {
            validatorFn = this.NG_ASYNC_VALIDATORS.find(asyncValidator => validatorName === asyncValidator.name);
        }

        return validatorFn;
    }

    getValidatorFn(validatorName: string, validatorArgs?: any): ValidatorFn | AsyncValidatorFn | never {

        console.debug( validatorName) // myCustomValidator

        let validatorFn = Validators[validatorName] || this.getCustomValidatorFn(validatorName);

        console.debug(validatorFn) // undefined

        if (!isFunction(validatorFn)) {
            throw new Error(`validator "${validatorName}" is not provided via NG_VALIDATORS or NG_ASYNC_VALIDATORS`);
        }

        return isDefined(validatorArgs) ? validatorFn(validatorArgs) : validatorFn;
    }

Is there any solution to workaround this?

Thanks

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions