Skip to content

Commit

Permalink
Improve validation
Browse files Browse the repository at this point in the history
  • Loading branch information
ehmicky committed May 29, 2022
1 parent 650c003 commit 6ccfd67
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
7 changes: 2 additions & 5 deletions src/config/normalize/lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { cleanObject } from '../../../utils/clean.js'
import { getInfo } from './info.js'
import { applyKeywords } from './keywords/main.js'
import { normalizeRules } from './normalize.js'
import { normalizeOpts } from './options.js'
import { logWarnings } from './warn.js'

// Validate and normalize a library's inputs.
Expand All @@ -20,7 +21,7 @@ import { logWarnings } from './warn.js'
// - Makes it clear to users what the order is
// TODO: abstract this function to its own library
export const normalizeInputs = async function (inputs, rules, opts) {
const { soft, all } = normalizeOpts(opts)
const { soft, all } = normalizeOpts(rules, opts)
const rulesA = normalizeRules(rules, all)

try {
Expand All @@ -38,10 +39,6 @@ export const normalizeInputs = async function (inputs, rules, opts) {
}
}

const normalizeOpts = function ({ soft = false, all } = {}) {
return { soft, all }
}

const applyRuleDeep = async function ({ inputs, moves, warnings }, rule) {
const entries = list(inputs, rule.name, {
childFirst: true,
Expand Down
20 changes: 20 additions & 0 deletions src/config/normalize/lib/options.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import isPlainObj from 'is-plain-obj'

export const normalizeOpts = function (rules, { soft = false, all } = {}) {
validateRules(rules)
return { soft, all }
}

const validateRules = function (rules) {
if (!Array.isArray(rules)) {
throw new TypeError(`Rules must be an array: ${rules}`)
}

rules.forEach(validateRule)
}

const validateRule = function (rule) {
if (!isPlainObj(rule)) {
throw new TypeError(`Rules must be plain objects: ${rule}`)
}
}

0 comments on commit 6ccfd67

Please sign in to comment.