-
Notifications
You must be signed in to change notification settings - Fork 57
/
index.js
52 lines (43 loc) · 1.9 KB
/
index.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
41
42
43
44
45
46
47
48
49
50
51
52
const Joi = require('joi');
const Hoek = require('@hapi/hoek');
const Util = require('util');
const SchemaResolver = require('./lib/resolver');
const schemaSchema = Joi.alternatives(Joi.object().unknown(true), Joi.string()).required();
const extensionSchema = Joi.alternatives().try(Joi.object().unknown(true), Joi.function())
const optionsSchema = Joi.object({
subSchemas: Joi.object().unknown(true).allow(null),
extensions: Joi.array().items(extensionSchema).allow(null),
refineType: Joi.func().allow(null),
refineSchema: Joi.func().allow(null),
strictMode: Joi.boolean().default(false),
useDefaults: Joi.boolean(). default(false)
});
const validate = function (schema, options = {}) {
const validateOptions = optionsSchema.validate(options);
Hoek.assert(!validateOptions.error, validateOptions.error);
const validateSchema = schemaSchema.validate(schema);
Hoek.assert(!validateSchema.error, validateSchema.error);
return validateOptions.value;
};
exports.defaults = function (defaults = {}) {
return {
schema(schema, options = {}) {
const merged = {
subSchemas: Object.assign({}, defaults.subSchemas, options.subSchemas),
extensions: defaults.extensions || [],
refineType: options.refineType || defaults.refineType,
refineSchema: options.refineSchema || defaults.refineSchema,
strictMode: options.strictMode || defaults.strictMode
};
if (Util.isArray(options.extensions)) {
merged.extensions = merged.extensions.concat(options.extensions);
}
options = validate(schema, merged);
return new SchemaResolver(schema, options).resolve();
}
};
};
exports.schema = function (schema, options = {}) {
options = validate(schema, options);
return new SchemaResolver(schema, options).resolve();
};