UNPKG

854 BJavaScriptView Raw
1'use strict'
2
3const bsplit = require('buffer-split')
4const errcode = require('err-code')
5
6/**
7 * Checks a record and ensures it is still valid.
8 * It runs the needed validators.
9 *
10 * @param {Object} validators
11 * @param {Record} record
12 * @param {function(Error)} callback
13 * @returns {undefined}
14 */
15const verifyRecord = (validators, record, callback) => {
16 const key = record.key
17 const parts = bsplit(key, Buffer.from('/'))
18
19 if (parts.length < 3) {
20 // No validator available
21 return callback()
22 }
23
24 const validator = validators[parts[1].toString()]
25
26 if (!validator) {
27 const errMsg = `Invalid record keytype`
28
29 return callback(errcode(new Error(errMsg), 'ERR_INVALID_RECORD_KEY_TYPE'))
30 }
31
32 validator.func(key, record.value, callback)
33}
34
35module.exports = {
36 verifyRecord: verifyRecord,
37 validators: require('./validators')
38}