ldapjs-community exports parseDN, DN, and RDN. Client methods accept DN strings, while server request objects expose parsed DN instances through req.dn.
For DN concepts and escaping rules, read Distinguished names and relative distinguished names.
Link to Parse a DNParse a DN
const ldap = require('ldapjs')
const dn = ldap.parseDN('uid=ada,ou=people,dc=example,dc=com')
console.log(dn.toString())
parseDN(value) requires a string and throws when the value is not valid DN syntax. Use ldap.DN.isDN(value) when code needs to distinguish an existing DN object from another value.
Link to Compare and navigateCompare and navigate
| Method | Result |
|---|---|
dn.equals(other) | Whether both DNs contain the same RDNs. |
dn.childOf(other) | Whether dn is below other. |
dn.parentOf(other) | Whether dn is above other. |
dn.parent() | A new DN containing the direct parent. |
dn.isEmpty() | Whether the DN contains no RDNs. |
dn.clone() | A new DN with the same RDNs and formatting options. |
childOf and parentOf are strict: a DN is not its own child or parent. Calling parent() on a one-RDN name produces the empty/root DN; calling it on an already empty DN returns null.
const base = ldap.parseDN('ou=people,dc=example,dc=com')
const user = ldap.parseDN('uid=ada,ou=people,dc=example,dc=com')
console.log(user.childOf(base)) // true
console.log(base.parentOf(user)) // true
console.log(user.parent().equals(base)) // true
Link to Format a DNFormat a DN
toString() uses the DN's current formatting options. format(options) formats one call, while setFormat(options) changes the defaults used by later toString() calls.
Preservation options are keepOrder, keepQuote, keepSpace, and keepCase. Modification options are upperName and skipSpace. Preservation options take precedence when both affect the same parsed value.
const dn = ldap.parseDN('CN=Ada, OU=People, DC=example, DC=com')
console.log(dn.format({ keepCase: true, keepSpace: true }))
console.log(dn.format({ upperName: true, skipSpace: true }))