ealush / vest

Vest ✅ Declarative validations framework
https://vestjs.dev/
MIT License
2.57k stars 84 forks source link

isDate alway fails #1120

Closed NormandoHall closed 10 months ago

NormandoHall commented 10 months ago

Fail:

enforce('2020-04-15').isDate();
enforce('15/04/2020').isDate({ format: 'DD/MM/YYYY' });

No fails:

enforce(new Date()).isDate();

Node: v18.18 Vest: 5.2.3

The same happens importing external rules from validator or using built in plugins.

EDIT: Seems an issue with validator?

https://github.com/validatorjs/validator.js/issues/2256

EDIT2:

Yes, there is an issue with validator. A quick and dirty workaround for me is parsing with dayjs and convert to Date object. No fails with the format, but not validates de date.

import { create, test, enforce } from 'vest';
import 'vest/enforce/date';
import dayjs from 'dayjs';
import customParseFormat from 'dayjs/plugin/customParseFormat.js';

dayjs.extend(customParseFormat);

const suite = create((data = {}, currentField) => {

  // ...

  test('birthDate', 'Fecha Nacimiento inválida', () => {
    enforce(dayjs(data.birthDate, 'DD/MM/YYYY').toDate()).isDate();
  });

});

export default suite;
ealush commented 10 months ago

Hi @NormandoHall, thanks for reaching out!

I just checked both examples that you supplied, and they both seem to be passing: image

On the right hand side you're seeing the proxy Object being returned from enforce, instead of an error being thrown - which means that isDate does indeed pass.

I am wondering how it looks inside your suite, there may be something else at play. Do you have a quick repro sandbox that I can take a look at?

NormandoHall commented 10 months ago

Hi @ealush, Thanks for your quick reply.

I have forked your svelte example sandbox, and added the form field "birthDate" and added the test in the suite, and still fails.

https://codesandbox.io/p/sandbox/svelte-vest-5-isdate-fail-vtdzjw

ealush commented 10 months ago

@NormandoHall No changes to your code whatsoever, it seems to work correctly.

Can you try just inputting this: 01/08/1990 I am wondering if it's related to trimming, special ascii characters, or something else I can't think of - because your code works.

See screencap birthdate

NormandoHall commented 10 months ago

It is very strange. I tested 01/08/1990 and fails. Tested with Chrome, Edge, and Firefox

https://github.com/ealush/vest/assets/2943127/9bd40c8c-ebcf-455c-bd28-6e3eb248bfed

NormandoHall commented 10 months ago

Well, with vest@5.1.0 runs ok (maybe because uses validator@13.9.0)

ealush commented 10 months ago

OK. I think you are right. I had the 13.9.0 locally installed and that's why I did not experience this. I downgraded and now I see this.

Releasing now a patch version of Vest with the lower validator version. Will be out in a few moments.

NormandoHall commented 10 months ago

@ealush, now runs like a charm.

Thank you!