-
Notifications
You must be signed in to change notification settings - Fork 0
/
validator.js
40 lines (33 loc) · 1.16 KB
/
validator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
const Validator = require('formio/src/resources/Validator');
const hook = require('formio/src/util/hook')({});
const _Validator = Validator;
const submissionModel = function(doc, field, skipId) {};
/**
* Validates the given data against the provided schema.
*
* @param {Object} schema - The JSON schema to validate the data against.
* @param {Object} data - The data to be validated.
* @return {Promise<Object>} - A promise that resolves with the validated submission, or rejects with an error object.
* @throws {Object} - An error object containing a `system` property for system errors or a `validation` property for validation errors.
*/
async function validate(schema, data) {
const validator = new _Validator(schema, submissionModel, {}, {}, hook);
const body = {data, metadata: {}};
const handler = (body) => new Promise((resolve, reject) => {
return validator.validate(body, (error, submission) => {
if (error) {
return reject(error);
}
return resolve(submission);
});
});
try {
const submission = await handler(body);
return submission;
} catch (err) {
throw err;
}
}
module.exports = {
validate,
};