Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom validators using schema #473

Closed
aaleksandrov opened this issue Nov 22, 2019 · 3 comments
Closed

Custom validators using schema #473

aaleksandrov opened this issue Nov 22, 2019 · 3 comments
Labels
flag: schema-based-validation type: question Questions about the usage of the library.

Comments

@aaleksandrov
Copy link
Contributor

Hi,
is it possible to create a custom validator and use it with validation schema? I don't want to use decorators because I don't control model (it comes from some sdk).
Please consider the following TS example. How should customValidationSchema look to make the custom validator work?

import {ValidatorConstraint, ValidatorConstraintInterface, ValidationArguments, ValidationSchema, registerSchema, validate} from 'class-validator';

@ValidatorConstraint({ name: 'customText', async: false })
export class CustomTextLength implements ValidatorConstraintInterface {

    validate(text: string, _args: ValidationArguments) {
        return text.length > 1 && text.length < 5;
    }

    defaultMessage(_args: ValidationArguments) {
        return 'Text ($value) is too short or too long!';
    }
}

class Model {
    name: string;
}

const instance = new Model();
instance.name = 'too-long-name-';

// This schema doesn't work
const customValidationSchema: ValidationSchema = {
    name: 'myCustomSchema',
    properties: {
        name: [{
            type: 'customValidation',
            constraints: [CustomTextLength]
        }],
    }
};

registerSchema(customValidationSchema);

validate('myCustomSchema', instance).then(errors => {
    if (errors.length > 0) {
        console.log('Validation failed: ', errors);
    } else {
        console.log('Validation succeed.');
    }
});

@vlapo
Copy link
Contributor

vlapo commented Jan 10, 2020

I dont think so class-validator support custtom validators. Also this feature is listed in 1.0 roadmap #178.

Close as duplicate of #186

@NoNameProvided NoNameProvided added type: question Questions about the usage of the library. flag: schema-based-validation labels Dec 9, 2022
@NoNameProvided
Copy link
Member

Schema validation is deprecated in the library. Closing this as wontfix.

@github-actions
Copy link

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jan 15, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
flag: schema-based-validation type: question Questions about the usage of the library.
Development

No branches or pull requests

3 participants