Files
nginx-proxy-manager/backend/lib/validator/api.js
T

44 lines
1009 B
JavaScript
Raw Normal View History

2024-10-10 15:53:11 +10:00
const Ajv = require('ajv/dist/2020');
2024-10-09 18:05:15 +10:00
const error = require('../error');
2020-02-19 15:55:06 +11:00
2024-10-10 15:53:11 +10:00
const ajv = new Ajv({
verbose: true,
allErrors: true,
allowUnionTypes: true,
strict: false,
coerceTypes: true,
2020-02-19 15:55:06 +11:00
});
/**
* @param {Object} schema
* @param {Object} payload
* @returns {Promise}
*/
function apiValidator (schema, payload/*, description*/) {
return new Promise(function Promise_apiValidator (resolve, reject) {
2024-10-09 18:05:15 +10:00
if (schema === null) {
reject(new error.ValidationError('Schema is undefined'));
return;
}
2020-02-19 15:55:06 +11:00
if (typeof payload === 'undefined') {
reject(new error.ValidationError('Payload is undefined'));
2024-10-09 18:05:15 +10:00
return;
2020-02-19 15:55:06 +11:00
}
2024-10-10 15:53:11 +10:00
const validate = ajv.compile(schema);
const valid = validate(payload);
2020-02-19 15:55:06 +11:00
if (valid && !validate.errors) {
resolve(payload);
} else {
let message = ajv.errorsText(validate.errors);
let err = new error.ValidationError(message);
err.debug = [validate.errors, payload];
reject(err);
}
});
}
module.exports = apiValidator;