Validation in Solidity
Learn how to validate method inputs and the contract state in Solidity.
We’ve made a glaring omission in all of our smart contracts so far. We assume that all the inputs that users provide are valid and they won't accidentally or intentionally try to break our smart contracts.
We’ve already covered access modifiers, which is one way to restrict what users can do with our smart contracts. In this lesson, we'll look into validation functions in Solidity that allow us to enforce valid input arguments, verify the internal state of our smart contracts, and perform other related tasks.
Validation in Solidity
Solidity has quite a buffet of validation methods to choose from. So, we'll start this lesson by going over the available options, and after that, we'll implement two new features. These include preventing users from registering an address with a duplicated name and implementing a mechanism to ensure that only the owner of a registered name can remove it.
The require
function
One of the most popular methods to verify certain conditions during a method execution is the require
function. It allows us to specify the condition we want to enforce and an optional error message.
Get hands-on with 1200+ tech skills courses.