question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

What’s The Issue?

The validator is mutating request body payloads.

We think we’d like to be able to configure the validator to do validation only.

Where?

See here: specifically, coerceTypes: true

    const aoav = new middlewares.RequestValidator(this.context.apiDoc, {
      coerceTypes: true,
      removeAdditional: true,
      useDefaults: true,
    });

With coerceTypes: true — the hardcoded value — we see a property being sent in the request body being transformed from the boolean literal true to the string literal "true".

"majorReferenceWork": true . // before validation

"majorReferenceWork": "true" // after validation

When I change the module so that coerceTypes: false then the boolean property remains a boolean literal. This is our desired behaviour.

How To Reproduce?

I can, and will, add a reproducible test case… but I wanted to have a quick discussion about this behaviour before doing that because you might tell me I’m doing something dumb 😄

Fix?

I don’t think the current behaviour is wrong and coerceTypes defaulting to true is fine: but, it’d be nice if we could turn this off via a configuration parameter.

Totally open to other suggestions.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
cdimasciocommented, Aug 11, 2019

@richdouglasevans i put together a test to try out your case. as far as i can tell it works correctly

Given the following spec for Pet (which is used in the POST request below)

    NewPet:
      required:
        - name
      properties:
        name:
          type: string
        tag:
          type: string
        is_cat:
          type: boolean

I created the following test (note that is_cat is specified as a boolean in the spec above, however, it is passed in the request as a string and is thus is a candidate for type coercion)

     it.only('should coerce the string 'true' to the boolean true', async () =>
        request(app)
          .post(`${basePath}/pets`)
          .send({
            name: 'test',
            is_cat: 'true', 
          })
          .expect(200)
          .then(r => {
           // return the request body after validation
            expect(r.body.is_cat).to.be.a('boolean');
          }));

The test case works as expected, the string "is_cat": true is properly coerced to boolean which i echo back in the response

Next, I created a similar test but passed a boolean in the request. Here, I want to ensure that the boolean true remain a boolean after validation. Th

     it.only('should keep the boolean true as a boolean', async () =>
        request(app)
          .post(`${basePath}/pets`)
          .send({
            name: 'test',
            is_cat: true, 
          })
          .expect(200)
          .then(r => {
            expect(r.body.is_cat).to.be.a('boolean');
          }));

This test also looks good

All in all, it’s very possible that my tests don’t match your use case. If so, please let me know. An example is of course appreciated.

On another note. as a sanity check, it may be worth verifying that type for majorReferenceWork is defined with type boolean and not string

1reaction
cdimasciocommented, Aug 10, 2019

Thanks @richdouglasevans. I’ll have a look at providing an option coerceTypes.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Type coercion - MDN Web Docs Glossary
Type coercion is the automatic or implicit conversion of values from one data type to another (such as strings to numbers). Type conversion...
Read more >
JavaScript type coercion explained - freeCodeCamp
Type coercion is the process of converting value from one type to another (such as string to number, object to boolean, and so...
Read more >
What is Type Coercion in JavaScript ? - GeeksforGeeks
Type Coercion refers to the process of automatic or implicit conversion of values from one data type to another. This includes conversion ...
Read more >
JavaScript Type Coercion (Implicit Conversion) and Why to ...
Type coercion is the automatic or implicit conversion of values from one data type to another. For example, converting a string value to...
Read more >
Type Coercion - Apache Tapestry
Type Coercion is the conversion of one type of object to a another object of a different type with similar content. Tapestry frequently...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found