How to make check if a key exists in mapping or not in Solidity when value is of type enum?

I have the following contract in solidity.

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

contract PocketCrypto {
    enum Role{ GUARDIAN, WARD} 

    mapping(address => Role) public role;

    function setRole(Role _role) public {
        role[msg.sender] = _role;
    }
}

I want to check if for a given address a Role is set or not. But since default value for mapping will be 0, and for enum it will mean first Role, how do I accomplish this?

>Solution :

You can extend the enum to reflect the 0th index (default) value as none.

enum Role{ NONE, GUARDIAN, WARD} 

Leave a Reply