Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Using Joi.attempt with { convert: false } does not prevent conversion. #2746

Closed
Leo843 opened this issue Feb 21, 2022 · 1 comment
Closed
Assignees
Labels
bug Bug or defect
Milestone

Comments

@Leo843
Copy link

Leo843 commented Feb 21, 2022

Context

  • node version: 16.13.0
  • module version with issue: 17.6.0
  • last module version without issue: unknown
  • environment (e.g. node, browser, native): tested on node and browser.
  • used with (e.g. hapi application, another framework, standalone, ...): none
  • any other relevant information: none

What are you trying to achieve or the steps to reproduce?

I am trying to validate a schema containing an ISO Date string. However by default, Joi converts the string (2022-02-21T21:28:00Z is converted into 2022-02-21T21:28:00.000Z). To prevent the conversion, i set the convert option to false and I noticed that it works when using any.validate but not when using Joi.attempt.

Below is a simple test showing how to reproduce the error. I made two tests, one uses Joi.attempt and the other uses any.validate. The result of these two tests are included as well.

import Joi from 'joi';

test(
  ''
  + "any.validate does not convert the returned value when 'option' convert is "
  + "set to 'false'",
  () => {
    const schema = Joi.string().isoDate();
    const now = '2022-02-21T21:28:00Z';
    const { value } = schema.validate(now, { convert: false });
    expect(value).toBe(now);
  },
);

test(
  ''
  + "Joi.attempt does not convert the returned value when 'option' convert is s"
  + "et to 'false'",
  () => {
    const schema = Joi.string().isoDate();
    const now = '2022-02-21T21:28:00Z';
    const message = '';
    const value = Joi.attempt(now, schema, message, { convert: false });
    expect(value).toBe(now);
  },
);
 FAIL  convert.test.js
  ✓ any.validate does not convert the returned value when 'option' convert is set to 'false' (4 ms)
  ✕ Joi.attempt does not convert the returned value when 'option' convert is set to 'false' (4 ms)

  ● Joi.attempt does not convert the returned value when 'option' convert is set to 'false'

    expect(received).toBe(expected) // Object.is equality

    Expected: "2022-02-21T21:28:00Z"
    Received: "2022-02-21T21:28:00.000Z"

      22 |     const message = '';
      23 |     const value = Joi.attempt(now, schema, message, { convert: false });
    > 24 |     expect(value).toBe(now);
         |                   ^
      25 |   },
      26 | );
      27 |

      at Object.<anonymous> (convert.test.js:24:19)

Test Suites: 1 failed, 1 total
Tests:       1 failed, 1 passed, 2 total
Snapshots:   0 total
Time:        1.872 s, estimated 2 s
@Marsup Marsup self-assigned this Sep 17, 2022
@Marsup Marsup added the bug Bug or defect label Sep 17, 2022
@Marsup Marsup added this to the 17.6.1 milestone Sep 17, 2022
Marsup added a commit that referenced this issue Sep 17, 2022
@Marsup
Copy link
Collaborator

Marsup commented Sep 17, 2022

Fixed by #2813.

@Marsup Marsup closed this as completed Sep 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug or defect
Projects
None yet
Development

No branches or pull requests

2 participants