@mu-ts/validation
v1.0.4
Published
## Examples FIXME clean this up and explain the examples.
Downloads
1
Readme
Validation
Examples
FIXME clean this up and explain the examples.
/**
* Decorates endpoints ({endpoint}) with validation constraints to perform on body of requests. This is intended
* to be used in conjunction with the mu.ts endpoint module.
*
* These can optionally be placed on methods marked with the @endpoint(...) decorator. Order matters, and the
* validation should be defined before the @endpoint annotation.
*
* The validator can either take a file name for a json descriptor or json descriptor object itself that
* describes the validations to perform against the body. These descriptors are intended to be compatible with
* whatever validation library you intend to use within. For these examples, we are using validate.js
* (https://www.validatejs.org).
*
* <pre><code>
* @validate('new-user-validation.json')
* @validate({"firstName": {"presence": true}})
* @endpoint('/user', HTTPAction.POST)
* public createUser(event: HTTPEvent, context: Context) : Promise<HTTPResponse> {
* ..
* }
* </code></pre>
*
* Multiple validators can be attached to a single endpoint for different expected bodies in different
* scenarios, as necessary. For this you can utilize the conditions parameter. Conditions should return
* a boolean and are provided the body as well as the originating lambda event to determine if the
* validation should be applied.
*
* <pre><code>
* @validate('new-client-user-validation.json', (body: HTTPBody, event: HTTPEvent) => body ? body.type === 'client' : false)
* @validate('new-demo-user-validation.json', (body: HTTPBody, event: HTTPEvent) => body ? body.type === 'demo' : false)
* @validate('new-user-validation.json')
* @endpoint('/user', HTTPAction.POST)
* public createUser(event: HTTPEvent, context: Context) : Promise<HTTPResponse> {
* ..
* }
* </code></pre>
*
* This will execute the validation rules in the new-user-validation.json file (our common validation for
* new User POST calls), and then if the body contains "type: client" it will also process the validation
* rules from new-client-user-validation.json, which would contain validation that doesn't apply to other
* user types in our endpoint. Likewise if the body had a "type: demo", the common validation and the
* new-demo-user-validation.json would run against the body.
*
* As mentioned prior, you can use any validator library you would like to provide to the EndpointRouter in
* the mu.ts endpoint module, as long as it implements a method validate(body: object, schema: object), or
* you will have to create a light wrapper around your desired library to conform.
*/
import {endpoint} from "./src/decorators";
import {Context, HTTPAction, HTTPEvent, HTTPResponse} from "./src/Model";
import {validate} from "./src/decoratorsValidate";
class GetUsersEndpoint {
// @validate('./file2.json', body => body ? body.test === false : false)
@validate('./file2.json')
@validate({"firstName": {"presence": true}}, body => body ? body.test === true : false)
@endpoint('/list',HTTPAction.PATCH)
updateUser(event: HTTPEvent, context: Context):Promise<HTTPResponse> {
let body = HTTPResponse
.setBody('the-body');
return Promise.resolve(body);
}
@endpoint('/list',HTTPAction.GET, ((body, event1) => body ? body.test === true : false))
loadUser(event: HTTPEvent, context: Context):Promise<HTTPResponse> {
let body = HTTPResponse
.setBody('the-body-2');
return Promise.resolve(body);
}
@validate("./file1.json")
@endpoint('/list', HTTPAction.POST)
createUser(event: HTTPEvent, context: Context) : Promise<HTTPResponse> {
let body = HTTPResponse
.setBody('the-body-3');
return Promise.resolve(body);
}
}
export { GetUsersEndpoint }