2015-10-31 17:28:25 +00:00
|
|
|
// Copyright 2015 Joyent, Inc.
|
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const assert = require('assert')
|
|
|
|
const util = require('util')
|
2015-10-31 17:28:25 +00:00
|
|
|
|
2020-10-31 21:07:32 +00:00
|
|
|
const isDN = require('./dn').DN.isDN
|
|
|
|
const isAttribute = require('./attribute').isAttribute
|
2015-10-31 17:28:25 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
/// --- Helpers
|
2015-10-31 17:28:25 +00:00
|
|
|
|
|
|
|
// Copied from mcavage/node-assert-plus
|
2019-08-27 21:11:49 +00:00
|
|
|
function _assert (arg, type, name) {
|
|
|
|
name = name || type
|
2015-10-31 17:28:25 +00:00
|
|
|
throw new assert.AssertionError({
|
|
|
|
message: util.format('%s (%s) required', name, type),
|
|
|
|
actual: typeof (arg),
|
|
|
|
expected: type,
|
|
|
|
operator: '===',
|
|
|
|
stackStartFunction: _assert.caller
|
2019-08-27 21:11:49 +00:00
|
|
|
})
|
2015-10-31 17:28:25 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
/// --- API
|
2015-10-31 17:28:25 +00:00
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
function stringDN (input, name) {
|
|
|
|
if (isDN(input) || typeof (input) === 'string') { return }
|
|
|
|
_assert(input, 'DN or string', name)
|
2015-10-31 17:28:25 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
function optionalStringDN (input, name) {
|
|
|
|
if (input === undefined || isDN(input) || typeof (input) === 'string') { return }
|
|
|
|
_assert(input, 'DN or string', name)
|
2015-10-31 17:28:25 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
function optionalDN (input, name) {
|
|
|
|
if (input !== undefined && !isDN(input)) { _assert(input, 'DN', name) }
|
2015-10-31 17:28:25 +00:00
|
|
|
}
|
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
function optionalArrayOfAttribute (input, name) {
|
|
|
|
if (input === undefined) { return }
|
2015-10-31 17:28:25 +00:00
|
|
|
if (!Array.isArray(input) ||
|
2019-08-27 21:11:49 +00:00
|
|
|
input.some(function (v) { return !isAttribute(v) })) {
|
|
|
|
_assert(input, 'array of Attribute', name)
|
2015-10-31 17:28:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-27 21:11:49 +00:00
|
|
|
/// --- Exports
|
2015-10-31 17:28:25 +00:00
|
|
|
|
|
|
|
module.exports = {
|
|
|
|
stringDN: stringDN,
|
|
|
|
optionalStringDN: optionalStringDN,
|
|
|
|
optionalDN: optionalDN,
|
|
|
|
optionalArrayOfAttribute: optionalArrayOfAttribute
|
2019-08-27 21:11:49 +00:00
|
|
|
}
|