oneRequired.js 531 B

123456789101112131415161718192021222324
  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. };