kristianmandrup / schema-to-yup

Schema to Yup validation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Type Date Error

TommyBrooks opened this issue · comments

Version: 1.11.10

Trying to use "type": "date" for a property is causing a runtime error.

{ "properties": { "DateTest": { "type": "date" } }

I just ran the date tests and they all passed.

npm test date

> schema-to-yup@1.11.10 test
> npx jest "date"

 PASS  test/types/date/date.test.js
  toYupDate
    ✓ null - %
    ✓ empty value - ok (1 ms)
    ✓ no key - throws missing key
    maxDate
      schema opts
        ✓ bad string - ignored? (1 ms)
        ✓ negative number - ok
      validate
        ✓ less date (1 ms)
        ✓ equal date - not valid? (1 ms)
        ✓ more date
    minDate
      schema opts
        ✓ bad string - ignored?
        ✓ negative number - ok (1 ms)
      validate
        ✓ less date
        ✓ equal date - valid? (1 ms)
        ✓ more date

Test Suites: 1 passed, 1 total
Tests:       13 passed, 13 total

Could you please add your specific test suite to the date tests and make a PR. Then I will make adjustments to make the test pass.

I added the following tests to date.test.js

describe("is date type", () => {
    describe("schema opts", () => {
      test("bad string - ignored?", () => {
        expect(() => createDate({})).not.toThrow();
      });
    });

    describe("validate", () => {
      const opts = createDate({});
      const schema = createSchema(opts);
      console.log({opts, schema: schema.fields.createdAt })

      test("is date type", () => {
        const valid = schema.isValidSync({
          createdAt: new Date(Date.now() - oneDay)
        });
        expect(valid).toBeTruthy();
      });

      test("is not date type", () => {
        const valid = schema.isValidSync({
          createdAt: "abc123"
        });
        expect(valid).toBeFalsy();
      });
    });
  })

These tests (without additional date constraints such as minDate or maxDate) all pass.

I've added these extra date tests to the latest commit. You are welcome to add your specific case to the test suite and give it a go.

See the person-schema.test.js for how to test it as part of a full schema if you find that easier.

Please also provide the runtime error and the full schema and input data you are testing it with.

Hi,
I am facing the same issue. Using the following schema:

{
  $id: "/schemas/default-patient-info.json",
  $schema: "http://json-schema.org/draft-07/schema#",
  title: "default-patient-info",
  type: "object",

  properties: {
    birthday: {
      type: "date",
    },
    job: {
      description: "",
      type: "string",
      maxLength: 256,
    },
    bio: {
      description: "",
      type: "string",
      maxLength: 1024,
    },
  },
  required: [],
};

the error I am getting is:
image

I also have tried typeHandlers method as below, but didn't work:

const myCustomDateHandler = (obj, config) => {
    return yup.date();
  };

  const yupSchema2 = buildYup(formData, {
    typeHandlers: {
      date: myCustomDateHandler,
    },
  });

Hi @mahsad71, could you please fork this repo and add your test case to the test suite, then make a draft PR and I will have a look at it. Thanks.

For your custom date handler, you will need to implement getValidator()

  getValidator() {
    return yup;
  }  

This is where it breaks:

  // this.validator = this.getValidator()

  get validatorInstance() {
    return this.validator.mixed();
  }

I've updated the docs to better explain how to create custom type handler. I've also added convenience props there such as this.constraints for an easier path to display custom error messages that reflect the type constraints