Created
December 24, 2023 17:10
-
-
Save akvgergo/98a689bc2a39eb7d47fadf10f3d3629e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
final EzSchema userProfileSchema = EzSchema.shape({ | |
"firstName": EzValidator<String>().required(), | |
"lastName": EzValidator<String>().required(), | |
"email": EzValidator<String>().required().email(), | |
"age": EzValidator<int>().min(18).max(100), | |
'contactDetails': EzSchema.shape({ | |
'mobile': EzValidator<String>() | |
.required() | |
.matches(RegExp(r'^\+\d{10,15}$'), 'Invalid phone number'), | |
'landline': EzValidator<String?>(optional: true), | |
}), | |
'address': EzSchema.shape({ | |
'street': EzValidator<String>().required(), | |
'city': EzValidator<String>().required(), | |
'state': EzValidator<String>().required(), | |
'zipCode': EzValidator<num>().required(), | |
'country': EzSchema.shape({ | |
'name': EzValidator<String>(defaultValue: 'TUNISIA').required(), | |
'code': EzValidator<String>().required(), | |
'continent': EzSchema.shape({ | |
'name': EzValidator<String>().required(), | |
'code': EzValidator<String>().required(), | |
}) | |
}), | |
}), | |
'employment': EzSchema.shape({ | |
'current': EzValidator<String?>(optional: true), | |
'previous': EzSchema.shape({ | |
'companyName': EzValidator<String>().required(), | |
'position': EzValidator<String>().required(), | |
'years': EzValidator<int>().min(1).max(50), | |
}), | |
}), | |
}); | |
// ----------------- | |
final (data, errors) = userProfileSchema.validateSync({ | |
'firstName': 'John', | |
'lastName': 'Doe', | |
'email': 'john.doe@example.com', | |
'age': 30, | |
'contactDetails': { | |
'mobile': '+12345678901', | |
}, | |
'address': { | |
'street': '123 Main St', | |
'city': 'Anytown', | |
'state': 'Anystate', | |
'zipCode': 12345, | |
'country': { }, // I will not define the country | |
}, | |
'employment': { | |
'current': 'Current Company', | |
'previous': { | |
'companyName': 'Previous Company', | |
'position': 'Previous Position', | |
'years': 5, | |
}, | |
}, | |
}); | |
print(data); | |
// Result of displayed data will contain country with default values | |
// { | |
// firstName: John, | |
// lastName: Doe, | |
// email: john.doe@example.com, | |
// age: 30, | |
// contactDetails: { mobile: +12345678901, landline: null }, | |
// address: | |
// { | |
// street: 123 Main St, | |
// city: Anytown, | |
// state: Anystate, | |
// zipCode: 12345, | |
// country: | |
// { name: TUNISIA, code: null, continent: { name: null, code: null } }, | |
// }, | |
// employment: | |
// { | |
// current: Current Company, | |
// previous: | |
// { | |
// companyName: Previous Company, | |
// position: Previous Position, | |
// years: 5, | |
// }, | |
// }, | |
// } | |
// | |
print(errors) | |
// {address: {country: {code: The field is required, continent: {name: The field is required, code: The field is required}}}} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment