Lodash _.conformsTo() method

Beginner
⏱️ 6 min read
📚 Updated: May 2026
🎯 3 Code examples
🚀 3 Try-it labs
Lodash

What you’ll learn

  • How _.conformsTo(object, source) runs predicate functions from source against values from object.
  • When this pattern beats hand-written if chains for validation-style checks.
  • How it differs from partial deep matching with _.isMatch.
  • Edge cases such as missing keys and extra properties on object.

Prerequisites

Comfort with plain objects and first-class functions (callbacks). Optional: skim _.isMatch in the Lodash docs for contrast.

  • You can read small predicate functions that take one argument and return a boolean.
  • You can run snippets in Node or use the Try-it labs in the browser.

Overview

_.conformsTo turns a “rules object” into a reusable validator: each rule is a predicate receiving the corresponding value from the candidate object. All listed rules must pass.

Composable checks

Express constraints as small unary predicates instead of nested conditionals.

Source-driven

Only keys defined on source participate in the test.

Boolean result

Returns true only when every predicate passes.

Syntax

javascript
_.conformsTo(object, source)
  • object: value under test (typically a plain object).
  • source: object whose own enumerable properties are predicate functions.
  • Returns: true if every predicate returns truthy for object[key].
1

Single predicate

One rule: numeric property must satisfy a threshold.

javascript
import conformsTo from "lodash/conformsTo";

var user = { age: 21, country: "US" };
var rules = {
  age: function (n) {
    return n >= 18;
  }
};

conformsTo(user, rules); // true
Try it Yourself
2

Multiple predicates

All predicates must pass; one failing rule yields false.

javascript
import conformsTo from "lodash/conformsTo";

var row = { score: 72, passes: true };
var rules = {
  score: function (n) {
    return n >= 80;
  },
  passes: function (b) {
    return b === true;
  }
};

conformsTo(row, rules); // false
Try it Yourself
3

Extra keys on object

Properties on object that are not listed in source do not affect the result.

javascript
import conformsTo from "lodash/conformsTo";

var payload = { id: 9, noise: "ignored", ok: true };
var rules = {
  id: function (n) {
    return n > 0;
  },
  ok: function (v) {
    return v === true;
  }
};

conformsTo(payload, rules); // true
Try it Yourself

📋 conformsTo vs isMatch

APISource shapeBest for
_.conformsTo(object, source)Predicate functions per keyThresholds, type checks, dynamic rules
_.isMatch(object, source)Partial object or nested literalsStructural equality against fixed sample data

Pitfalls to avoid

Missing

Undefined properties

Predicates still run with undefined when a key is absent—make rules defensive.

Scope

Assuming deep validation

Rules apply to immediate values only unless your predicates recurse manually.

Truthiness

Non-boolean returns

Lodash uses truthiness—accidentally returning 0 can fail the check.

❓ FAQ

No. It only reads values and runs predicates.
Lodash reads object[key], which is undefined when the key is missing—your predicate must handle undefined if that case is possible.
isMatch compares against plain values or partial objects. conformsTo expects predicate functions in source for dynamic checks.
Extra keys are ignored unless you add predicates for them in source.

Summary

  • Purpose: check an object against a map of predicate rules.
  • Contract: every predicate listed on source must pass.
  • Next: explore more helpers on Lodash _.eq().
Did you know?

_.conformsTo only checks keys present on the source object—extra keys on object are ignored unless you list predicates for them.

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful