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.

24 lines
531 B

4 years ago
  1. 'use strict';
  2. module.exports = function defFunc(ajv) {
  3. defFunc.definition = {
  4. type: 'object',
  5. macro: function (schema) {
  6. if (schema.length == 0) return true;
  7. if (schema.length == 1) return {required: schema};
  8. var schemas = schema.map(function (prop) {
  9. return {required: [prop]};
  10. });
  11. return {oneOf: schemas};
  12. },
  13. metaSchema: {
  14. type: 'array',
  15. items: {
  16. type: 'string'
  17. }
  18. }
  19. };
  20. ajv.addKeyword('oneRequired', defFunc.definition);
  21. return ajv;
  22. };