vudash/vudash

View on GitHub
packages/core/src/config-validator/index.js

Summary

Maintainability
A
0 mins
Test Coverage
'use strict'

const Joi = require('joi')
const { ConfigurationError } = require('../errors')

exports.validate = function (name, rules = {}, json = {}, options = {}) {
  if (!rules || (typeof rules === 'object' && !Object.keys(rules).length)) {
    return json
  }

  const { error, value } = Joi.validate(json, rules, options)
  if (error) {
    throw new ConfigurationError(
      `Could not register ${name} due to invalid configuration: ${error}`
    )
  }

  return value
}