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.
Fundamentals
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.
Foundation
📝 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.
Structure of the source object passed to _.conforms():
sourceRequired
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 valueFunction
Validator function accepting one object argument. Suitable for _.filter, Array.prototype.filter, or manual if (validate(data)) guards.
const validate = _.conforms(src)
missing keysImportant
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.
Hands-On
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.
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.
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.
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.
Compare
📋 _.conforms vs related patterns
Topic
_.conforms
_.conformsTo
_.matches
Manual if chain
Returns
Validator function
Boolean
Matcher function
Boolean in place
Rule style
Predicate per key
Predicate per key
Partial equality
Arbitrary statements
Reusable
Yes
No (inline)
Yes
Copy/paste
Range / type checks
Natural fit
Natural fit
Awkward
Natural fit
Filter arrays
_.filter(arr, fn)
Wrap in lambda
_.filter(arr, fn)
Verbose
🧠 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.
Important
📝 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.
_.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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about _.conforms()
Use these points when validating object shapes.
5
Core concepts
✅01
Predicate map
One fn per property.
Basics
🔄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.