Why the destructuring way on one and none for the others?


Why the validate const has to be destruct and the others not, what’s the reason?

const { validate } = use(‘Validator’)
const Mail = use(‘Mail’)
const Helpers = use(‘Helpers’)


The question is similar to why Validator exposes an object and other exposes single value.

There is no hard rule to destruct it, you can use it as follows.

const Validator = use('Validator')


I can use instead of this :

const { sanitize } = use(‘Validator’)
const data = sanitize(request.all(), rules)


const sanitize = use(‘Validator’)
const data = sanitize(request.all(), rules)



You probably need to learn about destructuring and how it works.
Also, I don’t get where you are trying to go by wanting to change this import.


i get that if i want to use sanitize function from Validator i have to destruct as a sanitize object first, but i don’t get it why for helpers and email are not destruct as object…

Let’s take this example:
const Helpers = use(‘Helpers’)
const welcomeView = Helpers.viewsPath(‘welcome.edge’)

so if i want the viewspath function from Helpers i do this:
const {viewsPath} = use(‘Helpers’)
const welcomeView =viewsPath(‘welcome.edge’)

like that


As stated above, you should learn more about destructuring and named import.

Helpers is a class, Validator is a litteral object.


How to figure out which is an object and which is a class if the loading is the same ?

For mail:
adonis install @adonisjs/mail
const providers = [

For validator:
adonis install @adonisjs/validator
const providers = [

and the using are not the same :
const Mail = use(‘Mail’)
const { validate } = use(‘Validator’)


It’s not that simple to find that out, since Adonis tries to simplify the usage of code.

If you want purity in your code, then inbuilt modules of AdonisJs will not help you much.

Instead of doing this

const Mail = require('@adonisjs/mail')
const config = require('./config/mail')

const mail = new Mail(config)


we provide a clean abstraction, where the instantiation and lifecycle of classes is managed transparently.

const Mail = use('Mail')


Now, ofcourse it does hide the structure of object or also it’s instantiation, but that’s the core of AdonisJs and is never going to change.

In short, going through the docs or the source code is the way to know more about it.


i will try to figure out
it is so confusing, don’t understand why use objects and classes when it is better to use only a way to build…