(schema, payload)
| 21 | * @returns {Promise} |
| 22 | */ |
| 23 | const validator = (schema, payload) => { |
| 24 | return new Promise((resolve, reject) => { |
| 25 | if (!payload) { |
| 26 | reject(new errs.InternalValidationError("Payload is falsy")); |
| 27 | } else { |
| 28 | try { |
| 29 | const validate = ajv.compile(schema); |
| 30 | const valid = validate(payload); |
| 31 | |
| 32 | if (valid && !validate.errors) { |
| 33 | resolve(_.cloneDeep(payload)); |
| 34 | } else { |
| 35 | const message = ajv.errorsText(validate.errors); |
| 36 | reject(new errs.InternalValidationError(message)); |
| 37 | } |
| 38 | } catch (err) { |
| 39 | reject(err); |
| 40 | } |
| 41 | } |
| 42 | }); |
| 43 | }; |
| 44 | |
| 45 | export default validator; |
no test coverage detected