Created
March 12, 2019 14:35
-
-
Save js-ms/fa9fb6b0ce3204e7fb76a85a06c9bb3a to your computer and use it in GitHub Desktop.
Validation Provider for Phone AdonisJS extending Validator
This file contains hidden or 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
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