Syntax, Parameter and Return Value
Syntax:
_.conforms(source)
Parameters:
source (Object): An object mapping properties to functions for validation.
Return Value:
(Function) - Returns a function that takes an object to check if it conforms to the source rules.
Examples
Creating a Validator Function for an Object:
JavaScript
var _ = require('lodash');
var isAdultMale = _.conforms({
age: n => n >= 18,
gender: gender => gender === 'male'
});
console.log(isAdultMale({ age: 20, gender: 'male' }));
console.log(isAdultMale({ age: 17, gender: 'male' }));

You can also try this code with Online Javascript Compiler
Run Code
Output:
true
false
Demonstrates using _.conforms() to create a function that validates if an object represents an adult male.
Filtering an Array of Objects:
JavaScript
var users = [
{ 'name': 'John', 'age': 25, 'active': true },
{ 'name': 'Jane', 'age': 20, 'active': false },
{ 'name': 'Jim', 'age': 30, 'active': true }
];
var activeUsers = _.filter(users, _.conforms({ 'active': Boolean }));
console.log(activeUsers);

You can also try this code with Online Javascript Compiler
Run Code
Output:
[{ 'name': 'John', 'age': 25, 'active': true }, { 'name': 'Jim', 'age': 30, 'active': true }]
Shows how to use a function created by _.conforms() to filter an array of objects.
Complex Object Validation:
JavaScript
var hasValidData = _.conforms({
id: _.isNumber,
name: name => name.length > 0,
tags: _.isArray
});
var data = { id: 1, name: 'Sample Item', tags: ['tag1', 'tag2'] };
console.log(hasValidData(data));

You can also try this code with Online Javascript Compiler
Run Code
Output:
true
An example of validating complex objects with multiple properties and types.
Schema Checking in Data Processing:
JavaScript
var isValidProduct = _.conforms({
productId: _.isString,
price: price => price > 0,
inStock: _.isBoolean
});
var products = [{ productId: 'A123', price: 99.99, inStock: true }, { productId: 'B456', price: -5.00, inStock: false }];
var validProducts = products.filter(isValidProduct);
console.log(validProducts.length);

You can also try this code with Online Javascript Compiler
Run Code
Output:
1
Demonstrates using _.conforms() for schema checking in a list of products.
Frequently Asked Questions
How does _.conforms() differ from simple property checking?
_.conforms() allows for more complex validations by providing predicate functions for each property, rather than just checking for property existence or simple equality.
Can _.conforms() be used with nested objects?
While _.conforms() works directly with top-level properties, handling nested objects might require composing the predicates or using other methods for deep property checking.
Is _.conforms() suitable for all data validation scenarios?
_.conforms() is well-suited for many validation scenarios, but for very complex or specialized validation requirements, custom logic or specialized validation libraries might be more appropriate.
Conclusion
Lodash's _.conforms() method is a versatile tool for creating functions that validate objects against specific criteria defined for their properties. It's useful for a range of scenarios from simple object filtering to complex data validation and schema enforcement.
You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSA, DBMS, Competitive Programming, Python, Java, JavaScript, etc.
Also, check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.