apicco-lib
Advanced tools
Comparing version 1.1.2 to 1.2.0
{ | ||
"name": "apicco-lib", | ||
"version": "1.1.2", | ||
"version": "1.2.0", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -26,2 +26,3 @@ # Apicco-lib | ||
- [Discovery endpoint](#discovery) | ||
- [Request body formatting](#formatting) | ||
- [Minimal SDK](https://github.com/SokratisVidros/apicco/blob/master/sdk/README.md) | ||
@@ -85,2 +86,10 @@ - [Versionless clients](https://github.com/SokratisVidros/apicco/blob/master/sdk/README.md) | ||
<a name="formatting"></a> | ||
#### Formatting | ||
Apicco can be also used for request body conversions and formatting. Apicco uses [Joi](https://github.com/hapijs/joi). That is, if the validation convert option is on (enabled by default), a string will be converted using the specified modifiers for string.lowercase(), string.uppercase(), string.trim(), and each replacement specified with string.replace(). | ||
The converted request body can be accessed at `ctx.request.validatedBody`; | ||
### Action files | ||
@@ -87,0 +96,0 @@ |
@@ -29,4 +29,6 @@ const Boom = require('boom'); | ||
if (ctx.request.body) { | ||
const { error } = joi.validate(ctx.request.body, schema, opts); | ||
const { error, value } = joi.validate(ctx.request.body, schema, opts); | ||
ctx.request.validatedBody = value; | ||
if (!isEmpty(error)) { | ||
@@ -44,2 +46,4 @@ switch (errorCode) { | ||
} | ||
} else { | ||
console.warn('Missing ctx.request.body...'); | ||
} | ||
@@ -46,0 +50,0 @@ |
@@ -7,6 +7,8 @@ /* eslint-env jest */ | ||
describe('validate({ schema, joi, joiOptions }) middleware', () => { | ||
function mockCtx() { | ||
function mockCtx(values = {}) { | ||
return { | ||
request: { | ||
body: {} | ||
body: { | ||
...values | ||
} | ||
} | ||
@@ -16,2 +18,21 @@ }; | ||
test('sets request.validatedBody on ctx', async () => { | ||
const ctx = mockCtx({ foo: ' BAR ' }); | ||
const next = jest.fn(); | ||
const schema = { | ||
foo: Joi.string() | ||
.lowercase() | ||
.trim() | ||
}; | ||
await validate({ | ||
schema, | ||
joi: Joi | ||
})(ctx, next); | ||
expect(ctx.request.validatedBody).toEqual({ | ||
foo: 'bar' | ||
}); | ||
}); | ||
describe('when validation error exists', () => { | ||
@@ -18,0 +39,0 @@ test('throws a 422 boom error including detailed error data', async () => { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
18664
303
229