Skip to content
Permalink
Browse files
adding documentation for code with redoc
  • Loading branch information
skondram committed Nov 26, 2021
1 parent 9088655 commit a2d8fed6f30a2c0ea56f3c1a82d095475640b4b8
Show file tree
Hide file tree
Showing 36 changed files with 13,717 additions and 12 deletions.
@@ -1,15 +1,36 @@
/**
* A module to run JSON Schema based validation on request/response data.
* @module controllers/validation
* @author Miltos Skondras
* @see schemas/* for JSON Schema definition files
*/

const {Validator, ValidationError} = require('jsonschema');

const userSchema = require('../schemas/user.json').definitions.user;
const appSchema = require('../schemas/application.schema.js');

/**
* Wrapper that returns a Koa middleware validator for a given schema.
* @param {object} schema - The JSON schema definition of the resource
* @param {string} resource - The name of the resource e.g. 'article'
* @returns {function} - A Koa middleware handler taking (ctx, next) params
*/

const makeKoaValidator = (schema, resource) => {

const v = new Validator();
const validationOptions = {
throwError: true,
propertyName: resource
};

/**
* Koa middleware handler function to do validation
* @param {object} ctx - The Koa request/response context object
* @param {function} next - The Koa next callback
* @throws {ValidationError} a jsonschema library exception
*/

const handler = async (ctx, next) => {

@@ -31,5 +52,7 @@ const makeKoaValidator = (schema, resource) => {
return handler;
}

/** Validate data against user schema for creating new users */
exports.validateUser = makeKoaValidator(userSchema, 'user');
/** Validate data against application schema */
exports.validateApplication = makeKoaValidator(appSchema, 'application');
@@ -0,0 +1,108 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>JSDoc: Source: controllers/validation.js</title>

<script src="scripts/prettify/prettify.js"> </script>
<script src="scripts/prettify/lang-css.js"> </script>
<!--[if lt IE 9]>
<script src="//html5shiv.googlecode.com/svn/trunk/html5.js"></script>
<![endif]-->
<link type="text/css" rel="stylesheet" href="styles/prettify-tomorrow.css">
<link type="text/css" rel="stylesheet" href="styles/jsdoc-default.css">
</head>

<body>

<div id="main">

<h1 class="page-title">Source: controllers/validation.js</h1>






<section>
<article>
<pre class="prettyprint source linenums"><code>/**
* A module to run JSON Schema based validation on request/response data.
* @module controllers/validation
* @author Miltos Skondras
* @see schemas/* for JSON Schema definition files
*/

const {Validator, ValidationError} = require('jsonschema');

const userSchema = require('../schemas/user.json').definitions.user;
const appSchema = require('../schemas/application.schema.js');

/**
* Wrapper that returns a Koa middleware validator for a given schema.
* @param {object} schema - The JSON schema definition of the resource
* @param {string} resource - The name of the resource e.g. 'article'
* @returns {function} - A Koa middleware handler taking (ctx, next) params
*/

const makeKoaValidator = (schema, resource) => {

const v = new Validator();
const validationOptions = {
throwError: true,
propertyName: resource
};

/**
* Koa middleware handler function to do validation
* @param {object} ctx - The Koa request/response context object
* @param {function} next - The Koa next callback
* @throws {ValidationError} a jsonschema library exception
*/

const handler = async (ctx, next) => {

const body = ctx.request.body;

try {
v.validate(body, schema, validationOptions);
await next();
} catch (error) {
if (error instanceof ValidationError) {
console.error(error);
ctx.body = {message: error.stack};
ctx.status = 400;
} else {
throw error;
}
}
}
return handler;
}

/** Validate data against user schema for creating new users */
exports.validateUser = makeKoaValidator(userSchema, 'user');
/** Validate data against application schema */
exports.validateApplication = makeKoaValidator(appSchema, 'application');</code></pre>
</article>
</section>




</div>

<nav>
<h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-controllers_validation.html">controllers/validation</a></li><li><a href="module-helpers_database.html">helpers/database</a></li></ul><h3>Classes</h3><ul><li><a href="module-helpers_database-DatabaseException.html">DatabaseException</a></li></ul>
</nav>

<br class="clear">

<footer>
Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.7</a> on Fri Nov 26 2021 03:00:19 GMT+0000 (GMT)
</footer>

<script> prettyPrint(); </script>
<script src="scripts/linenumber.js"> </script>
</body>
</html>
Binary file not shown.

0 comments on commit a2d8fed

Please sign in to comment.