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


#1

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’)


#2

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')
Validator.validate()

#3

I can use instead of this :

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

this

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


#4

No.

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.


#5

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
right?


#6

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

Helpers is a class, Validator is a litteral object.


#7

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 = [
@adonisjs/mail/providers/MailProvider’
]

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

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


#8

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)

mail.send()

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

const Mail = use('Mail')

Mail.send()

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.


#9

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…