Lodash _.conformsTo() method
What you’ll learn
- How
_.conformsTo(object, source)runs predicate functions fromsourceagainst values fromobject. - When this pattern beats hand-written
ifchains 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
_.conformsTo(object, source) - object: value under test (typically a plain object).
- source: object whose own enumerable properties are predicate functions.
- Returns:
trueif every predicate returns truthy forobject[key].
Single predicate
One rule: numeric property must satisfy a threshold.
import conformsTo from "lodash/conformsTo";
var user = { age: 21, country: "US" };
var rules = {
age: function (n) {
return n >= 18;
}
};
conformsTo(user, rules); // true Multiple predicates
All predicates must pass; one failing rule yields false.
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 Extra keys on object
Properties on object that are not listed in source do not affect the result.
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 📋 conformsTo vs isMatch
| API | Source shape | Best for |
|---|---|---|
_.conformsTo(object, source) | Predicate functions per key | Thresholds, type checks, dynamic rules |
_.isMatch(object, source) | Partial object or nested literals | Structural equality against fixed sample data |
Pitfalls to avoid
Undefined properties
Predicates still run with undefined when a key is absent—make rules defensive.
Assuming deep validation
Rules apply to immediate values only unless your predicates recurse manually.
Non-boolean returns
Lodash uses truthiness—accidentally returning 0 can fail the check.
❓ FAQ
Summary
- Purpose: check an object against a map of predicate rules.
- Contract: every predicate listed on
sourcemust pass. - Next: explore more helpers on Lodash _.eq().
_.conformsTo only checks keys present on the source object—extra keys on object are ignored unless you list predicates for them.
6 people found this page helpful
