UNPKG

1.35 kBJavaScriptView Raw
1// Copyright 2015 Joyent, Inc.
2
3const assert = require('assert')
4const util = require('util')
5
6const isDN = require('./dn').DN.isDN
7const isAttribute = require('./attribute').isAttribute
8
9/// --- Helpers
10
11// Copied from mcavage/node-assert-plus
12function _assert (arg, type, name) {
13 name = name || type
14 throw new assert.AssertionError({
15 message: util.format('%s (%s) required', name, type),
16 actual: typeof (arg),
17 expected: type,
18 operator: '===',
19 stackStartFunction: _assert.caller
20 })
21}
22
23/// --- API
24
25function stringDN (input, name) {
26 if (isDN(input) || typeof (input) === 'string') { return }
27 _assert(input, 'DN or string', name)
28}
29
30function optionalStringDN (input, name) {
31 if (input === undefined || isDN(input) || typeof (input) === 'string') { return }
32 _assert(input, 'DN or string', name)
33}
34
35function optionalDN (input, name) {
36 if (input !== undefined && !isDN(input)) { _assert(input, 'DN', name) }
37}
38
39function optionalArrayOfAttribute (input, name) {
40 if (input === undefined) { return }
41 if (!Array.isArray(input) ||
42 input.some(function (v) { return !isAttribute(v) })) {
43 _assert(input, 'array of Attribute', name)
44 }
45}
46
47/// --- Exports
48
49module.exports = {
50 stringDN: stringDN,
51 optionalStringDN: optionalStringDN,
52 optionalDN: optionalDN,
53 optionalArrayOfAttribute: optionalArrayOfAttribute
54}