validate products in lib/validate-profile

This commit is contained in:
Jannis R 2018-03-16 17:03:13 +01:00
parent 08357dfa9d
commit bbb68d86d5
No known key found for this signature in database
GPG key ID: 0FE83946296A88A5
3 changed files with 16 additions and 19 deletions

View file

@ -52,6 +52,22 @@ const validateProfile = (profile) => {
throw new Error('profile.products must be an array.')
}
if (profile.products.length === 0) throw new Error('profile.products is empty.')
for (let product of profile.products) {
if ('string' !== typeof product.product) {
throw new Error('profile.products[].product must be a string.')
}
if ('boolean' !== typeof product.default) {
throw new Error('profile.products[].default must be a boolean.')
}
if (!Array.isArray(product.bitmasks)) {
throw new Error(product.product + '.bitmasks must be an array.')
}
for (let bitmask of product.bitmasks) {
if ('number' !== typeof bitmask) {
throw new Error(product.product + '.bitmasks[] must be a number.')
}
}
}
}
module.exports = validateProfile

View file

@ -5,16 +5,7 @@ const slugg = require('slugg')
const createParseLine = (profile, operators) => {
const byBitmask = []
for (let product of profile.products) {
if ('string' !== typeof product.product) {
throw new Error('profile.products[].product must be a string.')
}
if (!Array.isArray(product.bitmasks)) {
throw new Error(product.product + '.bitmasks must be an array.')
}
for (let bitmask of product.bitmasks) {
if ('number' !== typeof bitmask) {
throw new Error(product.product + '.bitmasks[] must be a number.')
}
byBitmask[bitmask] = product
}
}

View file

@ -4,18 +4,8 @@ const createParseBitmask = (profile) => {
const defaultProducts = {}
let withBitmask = []
for (let product of profile.products) {
if ('string' !== typeof product.product) {
throw new Error('profile.products[].product must be a string.')
}
defaultProducts[product.product] = false
if (!Array.isArray(product.bitmasks)) {
throw new Error(product.product + '.bitmasks must be an array.')
}
for (let bitmask of product.bitmasks) {
if ('number' !== typeof bitmask) {
throw new Error(product.product + '.bitmasks[] must be a number.')
}
withBitmask.push([bitmask, product])
}
}