UNPKG

json-timeline

Version:

Generate Interactive Timelines stored in JSON Advents with Tags.

216 lines (174 loc) 5.12 kB
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JSDoc: Source: schema/validateSchema.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: schema/validateSchema.js</h1> <section> <article> <pre class="prettyprint source linenums"><code>/** * @module validateSchema */ const compareDates = require('../util/compareDates'); const isType = require('../util/isType'); /** * @constant * @type {Map} * @name monthDays * @description Map of Days in a Month based on Year */ const monthDays = new Map([ [1, (_) => 31], [2, (year) => year % 4 === 0 ? 29 : 28], [3, (_) => 31], [4, (_) => 30], [5, (_) => 31], [6, (_) => 30], [7, (_) => 31], [8, (_) => 31], [9, (_) => 30], [10, (_) => 31], [11, (_) => 30], [12, (_) => 31] ]); /** * @function * @name validateSchema * @description Validates Input against AdventSchema * @param {*} input Input Argument * @returns {(Object|false)} Value of Input if Valid, false otherwise */ function validateSchema(input) { if (!isType.isObject(input)) { return false; } const value = {}; // start const start = validateDate(input.start); if (start) { value.start = start; } else { return false; } // end? const end = validateDate(input.end); if (end) { if (compareDates(start, end) &lt;= 0) { value.end = end; } else { return false; } } else { value.end = null; } // title const title = input.title; if (isType.isString(title)) { value.title = title; } else { return false; } // content const content = input.content; if (isType.isString(content)) { value.content = content; } else { return false; } // tags const tags = validateTags(input.tags); if (tags) { value.tags = tags; } else { return false; } return value; } /** * @function * @name validateDate * @description Validates Input against DateSchema * @param {*} input Input Argument * @returns {(Object|false)} Value of Input if Valid, false otherwise */ function validateDate(input) { if (!isType.isObject(input)) { return false; } const value = {}; // Year if (isType.isNumber(input.year) &amp;&amp; (1 &lt;= input.year)) { value.year = input.year; } else { return false; } // Month if (isType.isNumber(input.month) &amp;&amp; (1 &lt;= input.month &amp;&amp; input.month &lt;= 12)) { value.month = input.month; } else { value.month = null; } // Day if (isType.isNumber(input.day)) { if (!isType.isNull(value.month) &amp;&amp; (1 &lt;= input.day &amp;&amp; input.day &lt;= monthDays.get(value.month)(value.year))) { value.day = input.day; } else { return false; } } else { value.day = null; } // Fill out nulls if (value.month === null) { value.month = 1; } if (value.day === null) { value.day = 1; } return value; } /** * @function * @name validateTags * @description Validates Input against AdventSchema.tags * @param {*} input Input Argument * @returns {(Object|false)} Value of Input if Valid, false otherwise */ function validateTags(input) { if (!Array.isArray(input)) { return false; } if (input.some(tag => !isType.isString(tag))) { return false; } return input; } module.exports = validateSchema; </code></pre> </article> </section> </div> <nav> <h2><a href="index.html">Home</a></h2><h3>Modules</h3><ul><li><a href="module-advents.html">advents</a></li><li><a href="module-compareDates.html">compareDates</a></li><li><a href="module-groupAdvents.html">groupAdvents</a></li><li><a href="module-index.html">index</a></li><li><a href="module-isType.html">isType</a></li><li><a href="module-json-timeline.html">json-timeline</a></li><li><a href="module-loadAdvents.html">loadAdvents</a></li><li><a href="module-renderAdvents.html">renderAdvents</a></li><li><a href="module-schemas.html">schemas</a></li><li><a href="module-sortAdvents.html">sortAdvents</a></li><li><a href="module-validateSchema.html">validateSchema</a></li></ul> </nav> <br class="clear"> <footer> Documentation generated by <a href="https://github.com/jsdoc/jsdoc">JSDoc 3.6.6</a> on Mon Dec 14 2020 23:14:56 GMT-0500 (Eastern Standard Time) </footer> <script> prettyPrint(); </script> <script src="scripts/linenumber.js"> </script> </body> </html>