rules.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. 'use strict';
  2. var ruleModules = require('./_rules')
  3. , toHash = require('./util').toHash;
  4. module.exports = function rules() {
  5. var RULES = [
  6. { type: 'number',
  7. rules: [ { 'maximum': ['exclusiveMaximum'] },
  8. { 'minimum': ['exclusiveMinimum'] }, 'multipleOf', 'format'] },
  9. { type: 'string',
  10. rules: [ 'maxLength', 'minLength', 'pattern', 'format' ] },
  11. { type: 'array',
  12. rules: [ 'maxItems', 'minItems', 'uniqueItems', 'contains', 'items' ] },
  13. { type: 'object',
  14. rules: [ 'maxProperties', 'minProperties', 'required', 'dependencies', 'propertyNames',
  15. { 'properties': ['additionalProperties', 'patternProperties'] } ] },
  16. { rules: [ '$ref', 'const', 'enum', 'not', 'anyOf', 'oneOf', 'allOf' ] }
  17. ];
  18. var ALL = [ 'type' ];
  19. var KEYWORDS = [
  20. 'additionalItems', '$schema', '$id', 'id', 'title',
  21. 'description', 'default', 'definitions'
  22. ];
  23. var TYPES = [ 'number', 'integer', 'string', 'array', 'object', 'boolean', 'null' ];
  24. RULES.all = toHash(ALL);
  25. RULES.types = toHash(TYPES);
  26. RULES.forEach(function (group) {
  27. group.rules = group.rules.map(function (keyword) {
  28. var implKeywords;
  29. if (typeof keyword == 'object') {
  30. var key = Object.keys(keyword)[0];
  31. implKeywords = keyword[key];
  32. keyword = key;
  33. implKeywords.forEach(function (k) {
  34. ALL.push(k);
  35. RULES.all[k] = true;
  36. });
  37. }
  38. ALL.push(keyword);
  39. var rule = RULES.all[keyword] = {
  40. keyword: keyword,
  41. code: ruleModules[keyword],
  42. implements: implKeywords
  43. };
  44. return rule;
  45. });
  46. if (group.type) RULES.types[group.type] = group;
  47. });
  48. RULES.keywords = toHash(ALL.concat(KEYWORDS));
  49. RULES.custom = {};
  50. return RULES;
  51. };