zhangjinzhou
zhangjinzhou

Reputation: 2701

Set default value of validation

I am using Joi to validate a payload of a service in my node.js server using hapijs framework. It used to look like this (in my typescript code as well as after compiling to javascript):

payload: {
    para1: Joi.number().required(),
    para2: Joi.string()
}

Now I want to set default value of the two parameters. If the code is written in javascript, I can do this:

payload: {
    para1: Joi.number().required().default(1),
    para2: Joi.string().default("defaultstring")
}

I tested it in swagger and the default values actually became the values I set.

However, my project is written in typescript. I did the same thing and compiled typescript code. The result javascript looks like this:

 payload: {
    para1: Joi.number().required()["default"](1),
    para2: Joi.string()["default"]("defaultstring")
 }

In swagger, the default values are not applied.

Here are my questions:

  1. why the code becomes different after compiling?
  2. what does ["default"]("defaultstring") mean and what does it do?
  3. how can I write typescript code to make sure it can compiled as Joi.string().default("defaultstring")

Update

According to @rsp's post, the format in question 2 is just different way to access object's property. I also get reference from here. But it doesn't explain if they have any difference. Does anyone have any idea?

Update2

Here is the difference between the two ways accessing JS property. It seems there is no negative effect using brackets way. However, in my case, the default values are not reflected on swagger. Will be doing research on it.

Upvotes: 11

Views: 32381

Answers (4)

rbansal
rbansal

Reputation: 1360

taxAmount: Joi.number().default(0),
totalAmount: Joi.number().default(0),

This works for me. Do not use .reqquired() keyword. The result will be in the

const validationResult = schema.validate(data);
validationResult.value
 

Upvotes: 0

Sharan
Sharan

Reputation: 31

It still says key is required when we use Joi.boolean().required().default(true)

The below worked for me

JOI.boolean().default(false)

Upvotes: 0

Shubham Verma
Shubham Verma

Reputation: 9933

Use should use default() like in below code:

 validate: {
        payload: {
            para1: Joi.number().integer().min(1).max(100).default(10).required(),
            para2: Joi.string().min(1).max(100).default("TEST").required(),
        }
    }

Upvotes: 0

rsp
rsp

Reputation: 111336

In JavaScript this:

required().default(1)

is the same as this:

required()["default"](1)

because you can access object properties either as:

object["propertyName"]

or:

object.propertyName

(with certain restrictions in the second case).

So it's strange that TypeScript would output the longer style if it doesn't have to, but it's also strange that the longer style doesn't work exactly the same as the shorter one.

I would try to manually change the compiled JavaScript to the shorter version and see if that helps. If it doesn't then the problem is somewhere else. My suspicion is that it will not help.

The .default() should work in TypeScript because it is defined in @types/joi - see:

But on the other hand there is this comment:

// TODO express type of Schema in a type-parameter (.default, .valid, .example etc)

Which may suggest that .default() implementation is not ready yet - see:

and also there's this issue: joi.d.ts out of date, missing types

Upvotes: 8

Related Questions