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

Does this have support for yup.ref #2

Closed
alligatortower opened this issue Jul 6, 2020 · 5 comments
Closed

Does this have support for yup.ref #2

alligatortower opened this issue Jul 6, 2020 · 5 comments
Labels
enhancement New feature or request released

Comments

@alligatortower
Copy link

I'm referring to functionality like that seen here: https://github.com/WASD-Team/yup-ast/blob/master/source/tests/converter.test.js#L518

What I'm trying to do is refer to multiple fields so that I can run them through a custom yup method (which does seem to be working) and test whether they, all together, sum to greater than some value.

And fwiw, thanks for taking over this codebase, glad to have found this repo.

@spaceemotion spaceemotion added the question Further information is requested label Jul 6, 2020
@spaceemotion
Copy link
Contributor

Hey, thanks for the issue. For a quick test, I've copied the unit test and while it did succeed (all green), I had to change one detail:

Every yup-ast schema is an Array of Arrays, but the yup.ref call in the original is an array of strings. So calling the method like so will do the trick:

-['yup.linkedGreaterThan', ['yup.ref', 'testValueSimple']],
+['yup.linkedGreaterThan', [['yup.ref', 'testValueSimple']]],

@alligatortower
Copy link
Author

alligatortower commented Jul 6, 2020

It works!

However, what I'm trying to do is a slightly more complicated, and I'm not sure whether it was supported in the previous version either.

  function allLessThan(refs, msg) {
      return this.test({
          test: function(val) {
            let value = val
            refs.forEach( ref => {
              value += this.resolve(ref);
            })
            return 10 > value
          },
          message: msg || 'all must be less than 10',
      });
  }

  yup.addMethod(yup.number, 'allLessThan', allLessThan);
  const schema = transformAll([['yup.object', {
    foo: [['yup.number'], ['yup.allLessThan', ['refs', [['yup.ref', 'baz']], [['yup.ref', 'bar']]] ]],
    bar: [['yup.number'], ['yup.required']],
    baz: [['yup.number'], ['yup.required']],
  }]]);
  const testObj = {
    foo: 1,
    bar: 1,
    baz: 1,
  }
  console.log(schema.isValidSync(testObj))

I'd like my test function to be able to accept an arbitrary number of refs as an array. Maybe this case is outside the scope of functionality you want to support, I'm not sure.

@spaceemotion
Copy link
Contributor

Alright, I've tested the following and I seem to have found "the issue":

function allLessThan(...refs) {
  return this.test({
    test: function(val) {
      let value = val
      refs.forEach( ref => {
        value += this.resolve(ref);
      })
      return 10 > value
    },
    message: 'all must be less than 10',
  });
}

yup.addMethod(yup.number, 'allLessThan', allLessThan);
const schema = transformAll([['yup.object', {
  foo: [['yup.number'], ['yup.allLessThan', [['yup.ref', 'baz']], [['yup.ref', 'bar']]] ],
  bar: [['yup.number'], ['yup.required']],
  baz: [['yup.number'], ['yup.required']],
}]]);

const testObj = {
  foo: 1,
  bar: 1,
  baz: 1,
};

expect(schema.isValidSync(testObj)).toEqual(true);

The package does not evaluate nested yup-ast schemas ([[[ ... ]]]). However, your prototype needs the detection for those. I'll mark this as a feature request.

@spaceemotion spaceemotion added enhancement New feature or request and removed question Further information is requested labels Jul 6, 2020
@alligatortower
Copy link
Author

Great! I really appreciate it.

@github-actions
Copy link

github-actions bot commented Jul 6, 2020

🎉 This issue has been resolved in version 1.2.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request released
Projects
None yet
Development

No branches or pull requests

2 participants