BVB Source Codes

waterline Show validate.js Source code

Return Download waterline: download validate.js Source code - Download waterline Source code - Type:.js
  1. /**
  2.  * Module dependencies
  3.  */
  4.  
  5. var _ = require('@sailshq/lodash');
  6. var flaverr = require('flaverr');
  7. var normalizeValueToSet = require('../utils/query/private/normalize-value-to-set');
  8.  
  9.  
  10. /**
  11.  * validate()
  12.  *
  13.  * Verify that a value would be valid for a given attribute, then return it, loosely coerced.
  14.  *
  15.  * > Note that this validates the value in the same way it would be checked
  16.  * > if it was passed in to an `.update()` query-- NOT a `.create()`!!
  17.  *
  18.  * ```
  19.  * // Check the given string and return a normalized version.
  20.  * var normalizedBalance = BankAccount.validate('balance', '349.86');
  21.  * //=> 349.86
  22.  *
  23.  * // Note that if normalization is not possible, this throws:
  24.  * var normalizedBalance;
  25.  * try {
  26.  *   normalizedBalance = BankAccount.validate('balance', '$349.86');
  27.  * } catch (e) {
  28.  *   switch (e.code) {
  29.  *     case 'E_':
  30.  *       console.log(e);
  31.  *       // => '[Error: Invalid `bankAccount`]'
  32.  *       throw e;
  33.  *     default: throw e;
  34.  *   }
  35.  * }
  36.  *
  37.  * // IWMIH, then it was valid...although it may have been normalized a bit (potentially in-place).
  38.  *
  39.  * ```
  40.  * - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  41.  *
  42.  * @param {String} attrName
  43.  *        The name of the attribute to validate against.
  44.  *
  45.  * @param {Ref} value
  46.  *        The value to validate/normalize.
  47.  *
  48.  * --
  49.  *
  50.  * @returns {Ref}
  51.  *          The successfully-normalized value.  (MAY or MAY NOT be the same as the original reference.)
  52.  *
  53.  * --
  54.  *
  55.  * @throws {Error} If it encounters incompatible usage in the provided `value`,
  56.  *                 including e.g. the case where an invalid value is specified for
  57.  *                 an association.
  58.  *         @property {String} code
  59.  *                   - E_HIGHLY_IRREGULAR
  60.  *
  61.  *
  62.  * @throws {Error} If the provided `value` has an incompatible data type.
  63.  *   |     @property {String} code
  64.  *   |               - E_TYPE
  65.  *   |     @property {String} expectedType
  66.  *   |               - string
  67.  *   |               - number
  68.  *   |               - boolean
  69.  *   |               - json
  70.  *   |
  71.  *   | This is only versus the attribute's declared "type", or other similar type safety issues  --
  72.  *   | certain failed checks for associations result in a different error code (see above).
  73.  *   |
  74.  *   | Remember:
  75.  *   | This is the case where a _completely incorrect type of data_ was passed in.
  76.  *   | This is NOT a high-level "anchor" validation failure! (see below for that)
  77.  *   | > Unlike anchor validation errors, this exception should never be negotiated/parsed/used
  78.  *   | > for delivering error messages to end users of an application-- it is carved out
  79.  *   | > separately purely to make things easier to follow for the developer.
  80.  *
  81.  *
  82.  * @throws {Error} If the provided `value` fails the requiredness guarantee of the corresponding attribute.
  83.  *   |     @property {String} code
  84.  *   |               - E_REQUIRED
  85.  *
  86.  *
  87.  * @throws {Error} If the provided `value` violates one or more of the high-level validation rules
  88.  *   |             configured for the corresponding attribute.
  89.  *   |     @property {String} code
  90.  *   |               - E_VIOLATES_RULES
  91.  *   |     @property {Array} ruleViolations
  92.  *   |               e.g.
  93.  *   |               ```
  94.  *   |               [
  95.  *   |                 {
  96.  *   |                   rule: 'minLength',    //(isEmail/isNotEmptyString/max/isNumber/etc)
  97.  *   |                   message: 'Too few characters (max 30)'
  98.  *   |                 }
  99.  *   |               ]
  100.  *   |               ```
  101.  *
  102.  *
  103.  * @throws {Error} If anything else unexpected occurs.
  104.  *
  105.  */
  106.  
  107. module.exports = function validate(attrName, value) {
  108.  
  109.   // Set up a few, common local vars for convenience / familiarity.
  110.   var orm = this.waterline;
  111.   var modelIdentity = this.identity;
  112.  
  113.   if (!_.isString(attrName)) {
  114.     throw flaverr({ name: 'UsageError' }, new Error(
  115.       'Please specify the name of the attribute to validate against (1st argument).'
  116.     ));
  117.   }//-鈥
  118.  
  119.   var normalizedVal;
  120.   try {
  121.     normalizedVal = normalizeValueToSet(value, attrName, modelIdentity, orm, false);
  122.   } catch (e) {
  123.     switch (e.code) {
  124.  
  125.       // If it is determined that this should be ignored, it's either because
  126.       // the attr is outside of the schema or the value is undefined.  In this
  127.       // case, set it to `undefined` and then continue on ahead to the checks
  128.       // below.
  129.       case 'E_SHOULD_BE_IGNORED':
  130.         normalizedVal = undefined;
  131.         break;
  132.  
  133.       // Violated the attribute's validation ruleset
  134.       case 'E_VIOLATES_RULES':
  135.         throw e;
  136.  
  137.       // Failed requireness guarantee
  138.       case 'E_REQUIRED':
  139.         throw e;
  140.  
  141.       // Failed type safety check
  142.       case 'E_TYPE':
  143.         throw e;
  144.  
  145.       // Miscellaneous incompatibility
  146.       case 'E_HIGHLY_IRREGULAR':
  147.         throw e;
  148.  
  149.       // Unexpected error
  150.       default:
  151.         throw e;
  152.     }
  153.   }//>-鈥
  154.  
  155.   // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  156.   // FUTURE: change this logic so that it works like it does for `.create()`
  157.   // (instead of just working like it does for .update())
  158.   //
  159.   // That entails applying required and defaultsTo down here at the bottom,
  160.   // and figuring out what makes sense to do for the auto timestamps.  Note
  161.   // that we'll also need to change the `false` flag above to `true` (the one
  162.   // we pass in to normalizeValueToSet)
  163.   // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  164.  
  165.   // Return normalized value.
  166.   return normalizedVal;
  167.  
  168. };
  169.  
downloadvalidate.js Source code - Download waterline Source code
Related Source Codes/Software:
notepad-plus-plus - Notepad++ official repository h... 2017-01-10
che - Eclipse Che: Next-generation Eclipse IDE. Open sou... 2017-01-10
Gource - oftware version control visualization ... 2017-01-10
FDFullscreenPopGesture - A UINavigationController's category to enable full... 2017-01-10
node-style-guide - A guide for styling your node.js / JavaScript code... 2017-01-09
Workerman - An asynchronous event driven PHP framework for eas... 2017-01-10
structor - An advanced visual editor for React components ... 2017-01-10
golearn - Machine Learning for Go 2017-01-10
poisontap - Exploits locked/password protected computers over ... 2017-01-10
kcptun - A Simple UDP Tunnel Based On KCP 2017-01-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top