1 |
|
2 |
|
3 | const assert = require('assert')
|
4 | const util = require('util')
|
5 |
|
6 | const isDN = require('./dn').DN.isDN
|
7 | const isAttribute = require('./attribute').isAttribute
|
8 |
|
9 |
|
10 |
|
11 |
|
12 | function _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 |
|
24 |
|
25 | function stringDN (input, name) {
|
26 | if (isDN(input) || typeof (input) === 'string') { return }
|
27 | _assert(input, 'DN or string', name)
|
28 | }
|
29 |
|
30 | function optionalStringDN (input, name) {
|
31 | if (input === undefined || isDN(input) || typeof (input) === 'string') { return }
|
32 | _assert(input, 'DN or string', name)
|
33 | }
|
34 |
|
35 | function optionalDN (input, name) {
|
36 | if (input !== undefined && !isDN(input)) { _assert(input, 'DN', name) }
|
37 | }
|
38 |
|
39 | function 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 |
|
48 |
|
49 | module.exports = {
|
50 | stringDN: stringDN,
|
51 | optionalStringDN: optionalStringDN,
|
52 | optionalDN: optionalDN,
|
53 | optionalArrayOfAttribute: optionalArrayOfAttribute
|
54 | }
|