-
Notifications
You must be signed in to change notification settings - Fork 1
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 Error #5
Comments
True. The proposed API is: const { validate } = require('@expresso/validator')
function getCustomError (ajvErrors) {
return new MyCustomError(`Errors: ${ajvErrors.join('\n')}`)
}
module.exports = [
validate({
type: 'object',
properties: {
email: {
type: 'string',
format: 'email'
},
password: {
type: 'string',
minLength: 6
},
firstName: {
type: 'string',
minLength: 3,
// pattern: '/^[a-z]+$/i'
},
phone: {
type: 'string',
// pattern: '/^\\+\\d{2}\\s\\(\\d{2}\\)\\s\\d{4,5}-?\\d{4}$/g'
}
},
required: ['email', 'password', 'firstName', 'phone']
}, { getCustomError }),
function (req, res) {
// regular route handling
}
] |
@khaosdoctor what do you think about this API? |
I think it should be simpler: // ErrorFile.ts
export class MyError {
constructor (message) { super(message) }
}
// RouteFile.ts
import { MyError } from './ErrorFile'
module.exports = [
validate({
type: 'object',
properties: {
email: {
type: 'string',
format: 'email'
},
password: {
type: 'string',
minLength: 6
},
firstName: {
type: 'string',
minLength: 3,
// pattern: '/^[a-z]+$/i'
},
phone: {
type: 'string',
// pattern: '/^\\+\\d{2}\\s\\(\\d{2}\\)\\s\\d{4,5}-?\\d{4}$/g'
}
},
required: ['email', 'password', 'firstName', 'phone']
}, MyError),
function (req, res) {
// regular route handling
}
] Then inside the validator we wrap the error and create a joined message but returning user error |
What if the user doesn't want to use a class as an error? Or what if users want to use a different class based on, say, environment? Using a function seems more flexible, and doesn't add that much complexity on our side. |
But a function can be used as a constructor just like a class. You can pass either a function or a class |
Well, then it doesn't need to be specified as a class 🤔 |
@khaosdoctor can I go with the function? Thinking about implementing it tonight |
go on |
It would be interesting if there was how to customize the error returned
The text was updated successfully, but these errors were encountered: