So, I want to validate a Mongoose field which should be a string containing only digits(because the first digit can be 0), and I setup a custom validator like such:
id: {
type: String,
required: [true, REQUIRED_VALIDATOR_ERROR_MESSAGE("ID")],
validator: {
validate: (value: string) => validator.isNumeric(value),
message: (props) => `${props.value} is invalid.`,
},
}
but when I pass an ID which contains letters, the validation passes.
You've got
validate
andvalidator
mixed up. It should be:Here's a minimal working example:
Reference: https://mongoosejs.com/docs/4.x/docs/validation.html, Custom Validators