1.2.6 • Published 7 years ago
meta-ngin v1.2.6
meta-ngin
Generates metadata for BYU APIs
Formatting Basic Metadata (validation_response)
The metadata function takes in three parameters:
code
: An http status code.message
(optional): A message associated with the http status code.errors
(optional): An array of errors found while validating path and query parameters. This is usually used when returning a 409.
The currently supported codes and message defaults are:
- 200 - OK
- 201 - Created
- 202 - Accepted
- 204 - No Content
- 400 - Bad Request
- 401 - Unauthorized
- 403 - Forbidden
- 404 - Not Found
- 405 - Method Not Allowed
- 409 - Conflict
- 500 - Internal Server Error
- 503 - Service Unavailable
- 504 - Gateway Timeout
Example using Express:
The most common use case for the validation_information property (supplied by the errors parameter) is when validating client input and returning validation errors.
const express = require('express');
const path = require('path');
const bodyParser = require('body-parser');
const status = require('meta-ngin');
app.use(bodyParser.json());
app.post(function(req, res){
let errors = [];
if (!req.body.id) errors.push('Missing id in body');
if (!req.body.name) errors.push('Missing name in body');
if (errors.length !== 0) res.send(status(409, 'Conflict in query parameters,', ['Missing id in body']);
else res.send(status(202));
})
let port = process.env.PORT || 8081;
app.listen(port, function () {
console.log("Beginning server");
console.log(" [INFO] Server running on port: " + port);
});
If errors are found in the request body, returns:
{
"validation_response": {
"code": 409,
"message": "Conflict in query parameters"
},
"validation_information": [
"Missing id in body"
]
}
If no errors are found in the request body, returns:
{
"validation_response": {
"code": 202,
"message": "Accepted"
}
}