Lodash _.conforms() Method

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 6 Examples + 3 Try It
Util utilities

What You’ll Learn

By the end of this tutorial, you’ll build reusable object validators with Lodash’s _.conforms() and predicate functions per property.

01

Core Syntax

Map keys to predicates: _.conforms({ key: fn }).

02

Validator Fn

Returns a function you call on any object.

03

All Must Pass

Every listed predicate must return truthy.

04

Filter Arrays

Pair with _.filter(arr, validator).

05

vs matches

Predicates vs deep partial equality.

06

Limitations

Predicates see values only—not full object.

What Is _.conforms()?

_.conforms() is a Lodash util helper that turns a template of predicate functions into a reusable validator. You define which properties matter and how each value should look—string type, minimum length, numeric range—and Lodash returns a function that returns true only when every predicate passes on the matching property.

💡
Beginner tip

Think of the source object as a checklist: { age: (v) => v >= 18, name: _.isString }. The returned function walks that checklist against any candidate object. Extra fields on the candidate are ignored unless you add them to the checklist.

Use it for form guards, filtering user lists, or gatekeeping API payloads—anywhere you want declarative rules without rewriting if chains for every field.

📝 Syntax

Pass a source object whose values are predicate functions (one per property to validate):

javascript
_.conforms(source)

Syntax Rules

  • source — plain object mapping property names to predicate functions.
  • Predicate args — each function receives object[propertyName] only.
  • Return value — a validator function returning true or false.
  • All keys — every key in source must pass; one failure fails the whole check.
  • Extra keys — properties on the target not listed in source are not validated.
javascript
import conforms from "lodash/conforms";
import isString from "lodash/isString";

const validatePerson = conforms({
  name: isString,
  age: (value) => typeof value === "number" && value >= 18
});

validatePerson({ name: "John", age: 25 });   // true
validatePerson({ name: "Alice", age: "30" }); // false

⚡ Quick Reference

TaskCode patternResult
Create validatorconst ok = _.conforms(source)Returns predicate fn
Test objectok({ name: "Ada" })true / false
One-off check_.conformsTo(obj, source)Boolean directly
Filter array_.filter(items, _.conforms(criteria))Matching objects
Use lodash predicate{ age: _.isNumber }Reuse lang helpers
Equality patterns_.matches({ role: "admin" })Different helper
Returns
Function

Reusable validator

Logic
AND

All predicates pass

Sibling
conformsTo

Immediate boolean

Category
Util

Object predicates

🧰 Parameters

Structure of the source object passed to _.conforms():

source Required

Object whose keys are property names to validate and whose values are predicate functions. Non-function values are not valid—always pass functions like (v) => ... or _.isString.

_.conforms({ email: isEmail })
predicate(value) Per key

Called with the target object’s property value. Return truthy to pass, falsy to fail. Does not receive the full object or the key name.

age: (v) => v >= 18
return value Function

Validator function accepting one object argument. Suitable for _.filter, Array.prototype.filter, or manual if (validate(data)) guards.

const validate = _.conforms(src)
missing keys Important

If the target lacks a key from source, validation fails. Require optional fields by making predicates accept undefined explicitly when needed.

nick: (v) => v === undefined || isString(v)

For cross-field rules (password === confirmPassword), combine _.conforms() with a separate check—the built-in predicates cannot see sibling properties.

Examples Gallery

Practical _.conforms() patterns with copy-ready code, sample output, and interactive Try It Yourself labs.

📚 Getting Started

Validate object shape with per-property predicate functions.

Example 1 — Validate a person object

Require a string name and numeric age of at least 18.

javascript
const validatePerson = _.conforms({
  name: (value) => typeof value === "string" && value.length > 0,
  age:  (value) => typeof value === "number" && value >= 18
});

console.log(validatePerson({ name: "John", age: 25 }));  // true
console.log(validatePerson({ name: "John", age: 16 }));  // false
Try It Yourself

How It Works

Lodash runs name and age predicates on the candidate object. Both must pass; age 16 fails the numeric >= 18 rule.

Example 2 — Type mismatch fails validation

Wrong types fail even when the value “looks” correct as a string.

javascript
const validatePerson = _.conforms({
  name: _.isString,
  age:  (value) => typeof value === "number" && value >= 18
});

console.log(validatePerson({ name: "Alice", age: "30" })); // false
console.log(validatePerson({ name: "Alice", age: 30 }));   // true

How It Works

Form inputs often arrive as strings. A strict typeof value === "number" check catches "30" before it silently breaks math elsewhere.

📈 Practical Patterns

Filter collections, guard forms, and reuse validator modules.

Example 3 — Filter an array of users

Pass the conforms validator directly to _.filter—every predicate in the criteria must pass.

javascript
const users = [
  { name: "John", age: 25, role: "admin" },
  { name: "Alice", age: 30, role: "user" },
  { name: "Bob", age: 20, role: "user" }
];

const isAdultUser = _.conforms({
  age:  (value) => value >= 25,
  role: (value) => value === "user"
});

const filtered = _.filter(users, isAdultUser);
// -> [{ name: "Alice", age: 30, role: "user" }]
Try It Yourself

How It Works

Each predicate must be a function—use (value) => value === "user", not a bare string like role: "user". John fails the role check; Bob fails the age check.

Example 4 — Login form guard

Validate username length and password minimum before accepting submission.

javascript
const validateLoginForm = _.conforms({
  username: (value) => typeof value === "string" && value.length >= 5 && value.length <= 20,
  password: (value) => typeof value === "string" && value.length >= 8
});

const formData = { username: "john_d", password: "secret123" };

if (validateLoginForm(formData)) {
  console.log("Form OK — submit");
} else {
  console.log("Form invalid — show errors");
}
Try It Yourself

How It Works

Store the validator once at module scope. For confirm-password equality, add formData.password === formData.confirmPassword after the conforms check—predicates cannot compare sibling fields.

Example 5 — Reusable validator modules

Split credentials and profile rules into separate conforms functions for clarity.

javascript
const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;

const validateCredentials = _.conforms({
  username: (v) => typeof v === "string" && v.length >= 5,
  password: (v) => typeof v === "string" && v.length >= 8
});

const validateProfile = _.conforms({
  name:  (v) => typeof v === "string" && v.length > 0,
  email: (v) => typeof v === "string" && emailPattern.test(v)
});

console.log(validateCredentials({ username: "ada", password: "long-enough" })); // true
console.log(validateProfile({ name: "Ada", email: "ada@example.com" }));       // true

How It Works

Small, named validators compose cleanly across routes. Export them from a validation module and import where forms or APIs need the same rules.

🚀 Beyond the Basics

Compare related Lodash helpers and pick the right tool.

Example 6 — conforms vs conformsTo vs matches

Three ways to test object shape—predicates, immediate check, and partial equality.

javascript
const user = { name: "Bob", role: "admin", age: 40 };

// conforms — custom predicate per key (reusable fn)
const isAdmin = _.conforms({
  role: (v) => v === "admin",
  age:  (v) => v >= 18
});
console.log(isAdmin(user)); // true

// conformsTo — same logic, one-shot boolean
console.log(_.conformsTo(user, {
  role: (v) => v === "admin"
})); // true

// matches — partial deep equality (not predicate-based)
console.log(_.matches({ role: "admin" })(user)); // true
console.log(_.matches({ role: "user" })(user));  // false

When to prefer conforms

Use _.conforms() when rules are functional (types, ranges, regex). Use _.matches() when you compare to fixed literal shapes. Use _.conformsTo() for a single inline test without storing the validator.

🧠 How _.conforms() Works

1

Store source predicates

Lodash closes over your { key: predicateFn } template.

Setup
2

Call validator on object

You pass a candidate object when validating or filtering.

Invoke
3

Run each predicate

For every key in source, Lodash calls predicate(object[key]).

Check
=

Return boolean

true only if every predicate passed; any failure returns false.

📝 Notes

  • Every value in source must be a function—not a literal like role: "user".
  • Predicates receive the property value only, not the whole object.
  • Keys listed in source must exist on the target (with valid values) unless your predicate accepts missing data.
  • Extra properties on the target object do not affect the result.
  • For production validation, consider dedicated schema libraries; conforms suits light guards and filters.
  • Sibling helpers: _.conformsTo() (immediate), _.matches() (equality patterns).

Conclusion

_.conforms() turns a table of property rules into a reusable validator you can call anywhere or pass to _.filter. It keeps validation declarative and composable while staying lightweight for tutorials and small apps.

Remember: predicates see single values, not sibling fields. Pair conforms with _.conformsTo() for one-offs and _.matches() when literal shape comparison is enough.

💡 Best Practices

✅ Do

  • Write one predicate function per property with clear type/range checks
  • Reuse lodash lang helpers like _.isString and _.isNumber
  • Export validators from a shared module for forms and APIs
  • Combine with a separate check for cross-field rules
  • Use _.conformsTo when you only need a single boolean test

❌ Don’t

  • Pass non-function values in the source object
  • Assume predicates receive the full object or key name
  • Rely on conforms alone for confirm-password matching
  • Validate dozens of nested paths—use a schema library instead
  • Forget that missing keys fail unless predicates allow undefined

Key Takeaways

Knowledge Unlocked

Five things to remember about _.conforms()

Use these points when validating object shapes.

5
Core concepts
🔄 02

Reusable

Returns validator fn.

Pattern
🔎 03

Filter

Works with _.filter.

Practical
⚠️ 04

Value only

Not full object.

Limit
🔀 05

matches

Equality alternative.

Compare

❓ Frequently Asked Questions

_.conforms() takes a source object whose values are predicate functions—one per property name. It returns a new function that checks a target object: for every key in source, it runs the predicate on object[key]. If all predicates return truthy, the result is true; otherwise false.
Each predicate receives only the property value—object[key]—not the whole object and not the key name. Cross-field rules (like matching two passwords) need a separate check or a custom wrapper, not a single conforms predicate.
Yes. _.conforms() only validates keys listed in your source template. Additional properties on the target object do not fail the check unless you add predicates for them.
If a key from source is absent on the target object (or the value is undefined and the key is not present), conforms returns false for that predicate path and the overall result is false.
_.matches() compares property values with partial deep equality (object patterns). _.conforms() runs custom predicate functions per key, so you can test types, ranges, regexes, and arbitrary logic on each value.
_.conforms(source) returns a reusable validator function. _.conformsTo(object, source) runs the same check once and returns a boolean immediately—useful for one-off tests without storing the predicate.
Did you know?

_.conforms() and _.matches() both return functions suitable for _.filter, but matches compares values with partial deep equality while conforms runs arbitrary predicate logic—so age: (v) => v >= 18 is expresses a range matches cannot.

Practice _.conforms() in the Live Editor

Open the Try It editor, run the examples, and experiment with your own validation rules.

Open Try It editor →

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