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

Schema validation chain doesn't break on validation failure. #1114

Open
jooohny opened this issue Dec 6, 2021 · 1 comment
Open

Schema validation chain doesn't break on validation failure. #1114

jooohny opened this issue Dec 6, 2021 · 1 comment

Comments

@jooohny
Copy link

jooohny commented Dec 6, 2021

Describe the bug

Schema validation chain doesn't break on matches validation failure.

To Reproduce

const version = {
    in: ['body', 'query'],
    errorMessage: 'Field "version" is invalid.',
    trim: true,
    matches: {
        options: [versionRe],
    },
    customSanitizer: {
        options: (value) => {
            // ...change value to sanitizedValue
            return sanitizedValue;
        },
    },
};

Expected behavior

If my validation fails on matches , customSanitizer still fires and changes value.

Current behavior

Validation must break, so I later can get original value from query or body and send it back to client. But it turns out that field doesn't match my regular expression, customSanitizer is still ok with it though and I send back to client good sanitized value which is not correct obviously. And I'm forced to do extra check inside sanitizer to avoid this behaviour:

    customSanitizer: {
        options: (value) => {
            if (!versionRe.test(value)) return value; // avoid returning sanitized value
            // ...change value to sanitizedValue
            return sanitizedValue;
        },
    },

Environment:

  • Express-validator version: 6.12.0
  • Express version: 4.17.1
@gustavohenke gustavohenke changed the title Schema validation chain doesn't break on matches validation failure. Schema validation chain doesn't break on validation failure. Dec 20, 2021
@tommmn
Copy link
Contributor

tommmn commented Apr 3, 2022

@jooohny did you try to bail before the sanitizer? I ran some tests and it works fine with bail.
matches: { options: [versionRe], bail: true, },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants