adonisjs / validator

Schema based validator for AdonisJS
MIT License
115 stars 40 forks source link

Get return type of validator #151

Closed matiasgarcia closed 2 years ago

matiasgarcia commented 2 years ago

I am wondering if this is a feature request or if I plain TS should achieve this.

Let's say I have this validator

export default class JumioCallbackValidator {
  public schema = schema.create({
    idScanImage: schema.string.optional(),
    idScanImageBackside: schema.string.optional(),
    idScanImageFace: schema.string.optional(),
    jumioIdScanReference: schema.string(),
    merchantIdScanReference: schema.string(),
    customerId: schema.string(),
  })

  public messages: CustomMessages = {}
}

And in a Controller its used like this

let callbackPayload = await request.validate(JumioCallbackValidator)

Is it possible to get the return type of JumioCallbackValidator without calling it? For example, to use it in a service definition attribute.

RomainLanz commented 2 years ago

Hey @matiasgarcia! 👋🏻

Yes, you can extract the type from the validator schema.

export type JumioCallbackData = JumioCallbackValidator['schema']['props']

export default class JumioCallbackValidator {
  public schema = schema.create({
    idScanImage: schema.string.optional(),
    idScanImageBackside: schema.string.optional(),
    idScanImageFace: schema.string.optional(),
    jumioIdScanReference: schema.string(),
    merchantIdScanReference: schema.string(),
    customerId: schema.string(),
  })

  public messages: CustomMessages = {}
}

Then, you can import JumioCallbackData anywhere in your application.

matiasgarcia commented 2 years ago

@RomainLanz thanks a lot!