UNPKG

node-red-contrib-request-validator

Version:

a node-red custom node to create schemas to validate incoming requests data.

177 lines (145 loc) 5.9 kB
<script type="text/javascript"> const nodeTypeOptions = { color: '#5C9EDB', category: 'function', defaults: { name: { value: "" }, group_name: { value: "" }, query: { value: "", required: false }, reqbody: { value: "", required: false }, method: { value: "GET", required: true }, includeHeader: { value: false, required: false }, headerSchema: { value: "", required: false } }, inputs: 1, outputs: 2, icon: "font-awesome/fa-check-square-o", label: function () { let name = "Request Validator"; if (!!this.group_name.value) { this.group_name.value += " - " } if (!!this.name.value) { name = this.group_name.value + this.name.value; } return name }, oneditprepare: function () { $("#node-input-method").typedInput({ types: [ { value: "method", options: [ { value: "GET", label: "GET" }, { value: "POST", label: "POST" }, { value: "PUT", label: "PUT" }, { value: "DELETE", label: "DELETE" } ] } ] }) $("#node-input-query").typedInput({ type: "json", types: ["json"] }) $("#node-input-reqbody").typedInput({ type: "json", types: ["json"] }) } } RED.nodes.registerType('RequestValidator', nodeTypeOptions); </script> <script type="text/html" data-template-name="RequestValidator"> <div class="form-row"> <label for="node-input-group_name"><i class="fa fa-tag"></i>Request Group Name</label> <input type="text" id="node-input-group_name" placeholder="User"> </div> <div class="form-row"> <label for="node-input-name"><i class="fa fa-tag"></i>Request Name</label> <input type="text" id="node-input-name" placeholder="Change Email"> </div> <div class="form-row"> <label for="node-input-method"><i></i>Method</label> <input type="text" id="node-input-method"> </div> <div class="form-row"> <label for="node-input-query"><i></i> Query Params</label> <input type="json" id="node-input-query" placeholder='{"id":{"type":"number","required":true}}'> </div> <div class="form-row"> <label for="node-input-reqbody"><i></i> Body Params</label> <input type="json" id="node-input-reqbody" placeholder='{"email":{"type":"string"}}'> </div> </script> <script type="text/markdown" data-help-name="RequestValidator"> Create Schemas to validate request method, body and query parameters. Schemas are configured in this node Properties. ## docs [![image](https://img.shields.io/badge/npm-CB3837?style=for-the-badge&logo=npm&logoColor=white)](https://www.npmjs.com/package/node-red-contrib-request-validator) [![image](https://img.shields.io/badge/GitHub-100000?style=for-the-badge&logo=github&logoColor=white)](https://github.com/JordanBispo/node-red-request-validator) --- ### Properties - Method (GET, POST, PUT, DELETE) - the expected method of the request to be validated. - Query Params (json) - the Schema of the expected query params. - Body Params (json) - the Schema of the expected body params. ### Outputs 1. Valid Request (top output) * `msg (any)` - the original msg of request. 2. Invalid Request (bottom output) * `payload (json)` - the error message. * `statusCode (400 | 500)` - the status code of error. ## Schema ### Schema Params - `type (string | number | boolean | array | date | any)` - the type of the param. - `required (true | false)` - whether the param is required or not. - `subtype (string | number | boolean | date | any)` - if the type is array, the subtype of the array elements. - specifications (json) - the specifications of the param. ### Specifications a specifications is a json object, used to validate specifics aspects of the param. Has the following properties: - `min (number)` - the minimum value of the param. If the param is an String, the min length of the param. - `max (number)` - the maximum value of the param. If the param is an String, the max length - the param. - `float (true | false)` - whether the param is a float number - `format ( "email" | "phone" | "date" | "cnpj" | "cpf" )` - the format of the string param. --- ## Examples ### Body schema example ```json { "name": { "type": "string", "required": true }, "phoneList": { "type": "array", "subtype": "string" } } ``` ##### Verification steps 1. Verify that the request body exist. 2. Verify if exist property `name` and its type is `string`. 3. If exist property `addressList` verify if its type is `array` and its subtype is `string`. 4. If all the validations are passed, send the original msg to the node connected to the top output. 5. If any validation fails, send the error message to the node connected to the bottom output. ---- ### Query specific schema example ```json { "id": { "type": "number", "required": true, "specifications":{ "min": 1, "float": false, } } } ``` ##### Verification steps 1. Verify that the request query exist. 2. Verify if exist property `id`. 3. Verify if the query string can be converted to a number. 4. Verify if a `id` is Integer number. 5. Verify if a `id` is greater or equal than 1. </script>