Skip to content

Standalone Validator Usage #1668

Answered by thetutlage
mastermunj asked this question in Help
Sep 25, 2020 · 2 comments · 6 replies
Discussion options

You must be logged in to vote

What you are looking for is a validator that is not hardly tied with HTTP requests only. In this case, I will recommend getting rid of ctx altogether (since outside HTTP requests, you will not have access to it) and accept just the data inside the constructor.

import { schema } from '@ioc:Adonis/Core/Validator'

export default class ProfileValidator {
  constructor (public data: any) {
  }

  public schema = schema.create({
  })

  public messages = {}
}

Now, you can use it as follows

import { validator } from '@ioc:Adonis/Core/Validator'
validator.validate(new ProfileValidator({}))

And during HTTP requests

request.validate(new ProfileValidator(request.all()))

Replies: 2 comments 6 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
6 replies
@mastermunj
Comment options

@thetutlage
Comment options

@mastermunj
Comment options

@thetutlage
Comment options

@mastermunj
Comment options

Answer selected by mastermunj
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Help
Labels
None yet
2 participants