You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

range.js 985B

123456789101112131415161718192021222324252627282930313233343536
  1. 'use strict';
  2. module.exports = function defFunc(ajv) {
  3. defFunc.definition = {
  4. type: 'number',
  5. macro: function (schema, parentSchema) {
  6. var min = schema[0]
  7. , max = schema[1]
  8. , exclusive = parentSchema.exclusiveRange;
  9. validateRangeSchema(min, max, exclusive);
  10. return exclusive === true
  11. ? {exclusiveMinimum: min, exclusiveMaximum: max}
  12. : {minimum: min, maximum: max};
  13. },
  14. metaSchema: {
  15. type: 'array',
  16. minItems: 2,
  17. maxItems: 2,
  18. items: { type: 'number' }
  19. }
  20. };
  21. ajv.addKeyword('range', defFunc.definition);
  22. ajv.addKeyword('exclusiveRange');
  23. return ajv;
  24. function validateRangeSchema(min, max, exclusive) {
  25. if (exclusive !== undefined && typeof exclusive != 'boolean')
  26. throw new Error('Invalid schema for exclusiveRange keyword, should be boolean');
  27. if (min > max || (exclusive && min == max))
  28. throw new Error('There are no numbers in range');
  29. }
  30. };