#What primitive value (or in that form) is need to pass to transform it and to validate

14 messages · Page 1 of 1 (latest)

ebon ivy
#

What primitive value (or in that form) is need to pass to transform it and to validate and throw error if I entered 1a and not 1.
It return NaN only.

Here, In nest/packages/common/pipes/validation.pipe.ts, line 113 , it transform and out without validation

 if (!metatype || !this.toValidate(metadata)) { 

Here it check if it is primitive nest/packages/common/pipes/validation.pipe.ts, line 122, like it want to validate it and throw error, but not come to that point if is validating 1 or 1a.

 const isPrimitive = this.isPrimitive(value); 
dawn mauve
#

Okay, so what is it you're trying to figure out here/why? I appreciate the interest, just curious what the root problem is, if there is any

ebon ivy
#

With DTO it transfor validate and throw error.

#

@dawn mauve I don't have permission to look under the hood of the car and understand how it works and how to use one or the other. And to have the knowledge to do or modify something.

dawn mauve
#

You absolutely do, I just wanted to understand if there was a root problem here. Like I said in the GitHub issue, the toValidate checks the metatype against the primitive constructors (String/Number/etc). The isPrimitive checks against the primitive type (string/number/etc)

ebon ivy
#

I want to make my ValidationPipe that will verify primitives and throw error if it is Nan or string is not string, and not understand to remove that lines of code or to leave

#

I don't know what to pass to it to came to that point, other that DTO

dawn mauve
#

Do you plan to use a DTO? If not, the ValidationPipe itself might be more than you need. You could use something like the ParseIntPipe() or just create your own pipe entirely. You said you expect either numbers or strings to come into it, right?

ebon ivy
#

With DTO

dawn mauve
#

If you're using a DTO, then you'll never have a regular string or NaN on those this.isPrimitive(value) line, because the value will be the body object, which eventually gets transformed into the DTO instance

ebon ivy
#

I use @spring silos('id'): number and @Body() body: SomeDTO, @spring silos() params: paramsDTO

dawn mauve
#

So build the @Params('id') id: number into the @Params() params: ParamsDTO

#

If it's just @Params('id') id: number it'll never get to validation as a DTO and the ParseIntPipe should be used instead for that parameter specifically (@Params('id', ParseIntPipe))

ebon ivy
#

I will do something like that with custom pipe validation

    protected transformPrimitive(value: any, metadata: ArgumentMetadata) {
        if (!metadata.data) {
            return value;
        }

        const { type, metatype } = metadata;

        if (type !== 'param' && type !== 'query') {
            return value;
        }

        const place = metadata.data ? `${type} ${metadata.data}` : type;

        switch (metatype) {
            case Boolean: {
                if (typeof value === 'undefined') {
                    return undefined;
                }
    
                if (this.isValidationEnabled && (typeof value !== 'string' || !validator.isBoolean(value))) {
                    throw Error({
                        status: 400,
                    });
                }
    
                return validator.toBoolean(value);
            }

            case Number: {
                if (this.isValidationEnabled && (!validator.isInt(value) && !validator.isNumeric(value))) {
                    throw Error({
                        status: 400,
                        message: `Validation error, ${place} is not numeric value.`,
                    });
                }
    
                return validator.toInt(value);
            }

            default: {
                if (!validator.isAlphanumeric(value)) {
                    throw Error({
                        status: 400,
                    });
                }

                return value;
            }
        }
}

I will play with checks, may be now they are too restrictive (alphanumeric only).