summaryrefslogtreecommitdiffstats
path: root/lib/config/validator.js
blob: 0ea278f28e80269a86b6f673a6aaab50abfab854 (plain)
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
var _ = require('lodash');
var jsonschema = require('jsonschema');
var jsonSchemaDefaults = require('json-schema-defaults');
var mergeDefaults = require('merge-defaults');

var schema = require('./schema');
var error = require('../utils/error');

// Validate a book.json content
// And return a mix with the default value
function validate(bookJson) {
    bookJson = _.cloneDeep(bookJson);

    var v = new jsonschema.Validator();
    var result = v.validate(bookJson, schema, {
        propertyName: 'config'
    });

    // Throw error
    if (result.errors.length > 0) {
        throw new error.ConfigurationError(new Error(result.errors[0].stack));
    }

    // Insert default values
    var defaults = jsonSchemaDefaults(schema);
    return mergeDefaults(bookJson, defaults);
}

module.exports = {
    validate: validate
};