Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Why This Function is Used
3.
Syntax, Parameter and Return Value
3.1.
Syntax: 
3.2.
Parameters:
3.3.
Return Value: 
4.
Examples 
4.1.
Creating a Validator Function for an Object:
4.2.
JavaScript
4.3.
Filtering an Array of Objects:
4.4.
JavaScript
4.5.
Complex Object Validation:
4.6.
JavaScript
4.7.
Schema Checking in Data Processing:
4.8.
JavaScript
5.
Frequently Asked Questions
5.1.
How does _.conforms() differ from simple property checking?
5.2.
Can _.conforms() be used with nested objects?
5.3.
Is _.conforms() suitable for all data validation scenarios?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Lodash _.conforms() Method

Author Riya Singh
0 upvote
Create a resume that lands you SDE interviews at MAANG
Speaker
Anubhav Sinha
SDE-2 @
12 Jun, 2024 @ 01:30 PM

Introduction

In JavaScript, validating objects against specific criteria or rules is a common task, especially in data processing and manipulation. Lodash's _.conforms() method offers a powerful and concise way to create a function that performs such validations. It checks if an object conforms to the source by invoking the predicates provided for corresponding properties. 

Lodash _.conforms() Method

This method is especially useful in scenarios where you need to filter data, validate object structures, or enforce data integrity based on specific rules.

Why This Function is Used

The _.conforms() function is used to create a function that checks whether an object meets a set of predicate functions mapped to its properties. This is essential for validating objects where each property must satisfy specific criteria, such as in data validation, schema checking, or complex filtering operations.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

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

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' }));

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

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);

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

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));

Output: 

true


An example of validating complex objects with multiple properties and types.

Schema Checking in Data Processing:

  • JavaScript

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);

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 DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. 

Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Previous article
Lodash _.defaultsDeep() Method
Next article
Lodash _.floor() Method
Live masterclass