kristianmandrup / schema-to-yup

Schema to Yup validation

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

strict uppercase doesn't work with minLength and maxLength

harunkara opened this issue · comments

name: {
  type: 'string', //string
  required: true,
  strict: true,
  uppercase: 'Sadece büyük harf giriniz',
  minLength: 2,
  maxLength: 30
}

Gives "Uncaught TypeError: Cannot read properties of undefined (reading 'min') at U.minLength" error. When I comment strict and uppercase or minLength and maxLength it starts to work. Why I can't use both of them at same time.

Good question! 😊 I recommend that you clone the repo, add a failing test case which covers your scenario, create a draft PR and then we can try to make it pass together.

https://codesandbox.io/s/schema-to-yup-error-bhli6m?file=/src/App.js
You can test it from this link. When the lines 17,18,19,20 are opened at same time it goes to throw error. But line 17&18 and line 19&20 are working truely when we use them seperately. @kristianmandrup

Thanks. The issue seems to be with strict: true. The constraints all work in combination when strict is disabled

        type: "string",
        minLength: 2,
        maxLength: 10,
        // strict: true,
        uppercase: "please only use uppercase characters"

So I think the error is related to the fact that strict is defined as a special true value constraint

  get constraintsMap() {
    return {
      simple: ["default", "required", "notRequired", "nullable"],
      trueValue: ["strict"],
    };
  }
  addTrueValueConstraint(propName, opts) {
    const constraint = this.constraintBuilder.addTrueValueConstraint(
      propName,
      opts
    );
    if (constraint) {
      const { base } = constraint;
      this.base = base;
    }
    return this;
  }

Compared to

  addConstraint(propName, opts) {
    const constraint = this.build(propName, opts);
    if (constraint) {
      this.typeHandler.base = constraint;
      // const { _whitelist } = constraint;
      // const list = _whitelist && _whitelist.list;
      return constraint;
    }
    return false;
  }

It looks like the logic is off and should mimic this.typeHandler.base = constraint; ie. for addTrueValueConstraint it should look like

    if (constraint) {
      this.base = constraint;
    }

I would think. I added a test to verify it and now the test with your example passes in the fix-strict branch :)

Branch has now been merged to master. Please verify it fixes the issue. Cheers.

@kristianmandrup Again maxLength+minLength can't be used at same time with uppercase control. When the whole 16,17,18 and 19 lines are not commented, it does only uppercase control. maxLength and minLength doesn't work.
version:1.12.10
Example: https://codesandbox.io/s/schema-to-yup-error-bhli6m?file=/src/App.js

I believe I fixed the issue in the code and I've added a test case based on your codepen example. Could you please clone the repo and run: npx jest followed by the name of the test then verify that the test case indeed duplicates your case.

I'll have a look at the codepen example shortly