Browse project docs

Filter API

Parse and construct LDAP search filters safely with the ldapjs-community filter API.

On this page

Search filters can be supplied as strings or constructed filter objects. Construct filter objects when values come from users or another untrusted source so the library performs the wire encoding instead of interpolating LDAP filter syntax.

For filter grammar and escaping, use LDAP filter syntax and LDAP filter escaping rules.

Link to Parse a filterParse a filter

javascript
const ldap = require('ldapjs')

const filter = ldap.parseFilter('(&(objectClass=person)(mail=*@example.com))')

console.log(filter.type)
console.log(filter.toString())

parseFilter(value) throws when the string is not valid filter syntax. Compound filters expose their child filters as a tree.

Link to Construct a filterConstruct a filter

javascript
const filter = new ldap.AndFilter({
  filters: [
    new ldap.EqualityFilter({
      attribute: 'objectClass',
      value: 'person'
    }),
    new ldap.EqualityFilter({
      attribute: 'uid',
      value: process.env.LDAP_USERNAME
    })
  ]
})

client.search(process.env.LDAP_BASE_DN, {
  scope: 'sub',
  filter
}, callback)

Link to Exported filter classesExported filter classes

ClassPurpose
EqualityFilterMatch one exact attribute value.
PresenceFilterRequire an attribute to be present.
SubstringFilterMatch initial, intermediate, or final substrings.
GreaterThanEqualsFilterApply greater-than-or-equal matching.
LessThanEqualsFilterApply less-than-or-equal matching.
ApproximateFilterRepresent approximate matching.
AndFilterRequire every child filter to match.
OrFilterRequire at least one child filter to match.
NotFilterNegate one child filter.
ExtensibleFilterEncode or parse an LDAPv3 extensible-match filter.

Standard filter objects expose matches(entry) for in-memory server routing. Attribute values are strings unless the application adds its own schema-aware conversion.

Extensible matching

The current implementation can parse and encode ExtensibleFilter values for clients, but it does not implement server-side extensible matching. Do not rely on matches() for that filter type.

Link to Complete referenceComplete reference