Releases: adonisjs/validator
Updating underlying dependencies
- chore: update dependencies 7621e42
request.validate can also accept the class constructor
- feat: accept Validator as a class constructor in request.validate method da1f5f6
Making validator API consistent and less verbose
This release removes the compile
and compileAndCache
methods from the validator object. This is done in the favor of removing too many ways of doing the same thing.
Why these APIs were added in the first place
The validator schema created using schema.create
needs to be compiled to re-usable function and then use the compiled output for validating runtime data. The compilation step ensures, we are not paying the performance penalty every time, when even the underlying schema is same. Because of this, we added the validator.compile
method. So you will write the code as follows:
validator.compile
validator.validate({
schema: validator.compile(schema.create({
username: schema.string(),
}))
})
But, there is a problem with the above approach, what if, you want to use dynamic data inside your schema. For example:
validator.validate({
schema: validator.compile(schema.create({
username: schema.string({}, [
rules.unique(ctx.auth.user.id)
]),
}))
})
The above schema uses a dynamic value ctx.auth.user.id
, but the compiler will compile the schema with the first value and re-uses it afterwards. To overcome this situation, we added another method called compileAndCache
validator.compileAndCache
validator.validate({
schema: validator.compileAndCache(schema.create({
username: schema.string({}, [
rules.unique(ctx.auth.user.id)
]),
}), `cache-key-${ctx.auth.user.id}`)
})
The above method will cache the compiled schema against the cached key, so if the key changes, it will re-compile the schema.
Conclusion
Both APIs have subtle differences and this subtlety demands too much attention in order to use the correct API. We have removed both the methods and now, you always have to define the cacheKey
in order to avoid re-compiling.
This is how it looks
validate.validate({
schema: schema.create({
username: schema.string({}, [
rules.unique(ctx.auth.user.id)
]),
}),
cacheKey: `cache-key-${ctx.auth.user.id}`
})
Most of the times, the cache key can be URL of the HTTP request, unless, you are using dynamic values inside the schema, which are not part of the URL.
I hope, all this justifies the breaking change
Commits
Updating underlying dependencies
Fixing `request.validate` method to return validated body
Install @types/validator as prod dependency
Adding bunch of new rules
- feat: add min length rules f669bcd
- feat: add mobile validation rule 531391d
- feat: add ip validation rule f6190fd
- feat: add trim option property to string rule 71c5e02
- feat: add option to escape string values inside validator 312f739
- feat: add email validation rule 0d39c64
- style: keep code consitent c130080
- feat: profile request validation action 238e454
- style: remove console.log statement from one of the tests bb5bb58
- chore(package): update dependencies 53e46ad
Handful of bug fixes and adding support for decide the non existing fields behavior
It is always tricky to decide when to consider a field value is non-existing. Some may want to treat only undefined
as non-existing, some may not undefined
and null
both and some may want empty strings as well.
Infact, treating empty string as non-existing becomes mandatory when dealing with traditional HTML forms, since if a field value is not filled out the server will receive it as an empty string.
With this release we allow you to control the behavior of what should be treated as non-existing value.
Handful of bug fixes
Fix by installing endent as production dependency
- chore(package): add endent as prod dependency 46aa026