react-native-moyasar-sdk
Version:
Official React Native Moyasar SDK - Integrate Credit Cards, Apple Pay, Samsung Pay, and STC Pay with simple interfaces for a seamless payment experience in your React Native app
46 lines (43 loc) • 1.51 kB
JavaScript
;
export class FieldValidator {
rules = [];
shouldErr = false;
addRule(error, predicate) {
this.rules.push({
predicate,
error
});
}
// TODO: Refactor to named params
/**
* Validates input but only shows errors after the field has been touched
* @param value The value to validate
* @param creditCardNumber Optional credit card number
* @param supportedNetworks Optional array of supported credit card networks (Needed for supported networks validation)
* @returns Error message if validation fails and the field has been touched, null otherwise
*/
visualValidate(value, creditCardNumber, supportedNetworks) {
this.shouldErr = this.shouldErr || value !== '';
if (!this.shouldErr) {
return null;
}
return this.validate(value, creditCardNumber, supportedNetworks);
}
// TODO: Refactor to named params
/**
* Validates input against defined rules
* @param value The value to validate
* @param creditCardNumber Optional credit card number
* @param supportedNetworks Optional array of supported credit card networks (Needed for supported networks validation)
* @returns Error message if validation fails, null if validation passes
*/
validate(value, creditCardNumber, supportedNetworks) {
for (const rule of this.rules) {
if (rule.predicate(value, creditCardNumber, supportedNetworks)) {
return rule.error;
}
}
return null;
}
}
//# sourceMappingURL=field_validator.js.map