diff options
author | Samy Pessé <samypesse@gmail.com> | 2016-02-25 10:12:46 +0100 |
---|---|---|
committer | Samy Pessé <samypesse@gmail.com> | 2016-02-25 10:12:46 +0100 |
commit | 82a4f80cd23ecb0a734c21668bc8561734d96e36 (patch) | |
tree | f50f2cbbc79b0b05ed9d58f66f63e26f35a1eaba /lib/config/validator.js | |
parent | f47a3968b7a78af8091906fe8936cd00581ae05f (diff) | |
download | gitbook-82a4f80cd23ecb0a734c21668bc8561734d96e36.zip gitbook-82a4f80cd23ecb0a734c21668bc8561734d96e36.tar.gz gitbook-82a4f80cd23ecb0a734c21668bc8561734d96e36.tar.bz2 |
Use jsonschema to validate configuration
Diffstat (limited to 'lib/config/validator.js')
-rw-r--r-- | lib/config/validator.js | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/lib/config/validator.js b/lib/config/validator.js new file mode 100644 index 0000000..0ea278f --- /dev/null +++ b/lib/config/validator.js @@ -0,0 +1,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 +}; |