testRules.js 1001 B

12345678910111213141516171819202122
  1. var util = require('util');
  2. var _ = require('@sailshq/lodash');
  3. var anchor = require('../../index.js');
  4. // Test a rule given a deliberate example and nonexample
  5. // Test WITH and WITHOUT callback
  6. module.exports = function testRules (rules, example, nonexample) {
  7. // Throw an error if there's any trouble
  8. // (not a good production usage pattern-- just here for testing)
  9. var exampleOutcome = anchor(example, rules);
  10. if (exampleOutcome.length > 0) {
  11. throw new Error('Valid input marked with error: '+util.inspect(exampleOutcome,{depth:null})+ '\nExample: '+util.inspect(example,{depth:null})+'\nDetails: '+util.inspect(exampleOutcome));
  12. }
  13. var nonexampleOutcome = anchor(nonexample, rules);
  14. if (!_.isArray(nonexampleOutcome) || nonexampleOutcome.length === 0) {
  15. throw new Error('Invalid input (' + nonexample + ') allowed through. '+util.inspect(rules,{depth:null})+ '\nNon-example: '+util.inspect(nonexample,{depth:null})+'\nDetails: '+util.inspect(nonexampleOutcome));
  16. }
  17. };