Skip to content

Instantly share code, notes, and snippets.

@js-ms
Created March 12, 2019 14:35
Show Gist options
  • Save js-ms/fa9fb6b0ce3204e7fb76a85a06c9bb3a to your computer and use it in GitHub Desktop.
Save js-ms/fa9fb6b0ce3204e7fb76a85a06c9bb3a to your computer and use it in GitHub Desktop.
Validation Provider for Phone AdonisJS extending Validator
const { ServiceProvider } = require('@adonisjs/fold')
class PhoneProvider extends ServiceProvider {
async phone(data, field, message, args, get){
const value = get(data, field);
if (!value) {
return
}
let number = value;
let result = number.match(/^(\(?\+?[0-9]*\)?)?[0-9_\- \(\)]*$/);
if (!result) {
throw message
}
};
/**
* Attach context getter when all providers have
* been registered
*
* @method boot
*
* @return {void}
*/
boot () {
const Validator = use('Validator');
Validator.extend('phone', this.phone.bind(this), 'error in phone validation');
//
}
}
module.exports = PhoneProvider;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment