Skip to content

Conversation

@cdoshi
Copy link

@cdoshi cdoshi commented Jun 19, 2019

Issue is that if we use custom validators, the library throws an error saying it's not a function. Issue is that the custom validator is already a schema based on your documentation hence it just needs to be returned as is

Copy link
Contributor

@tavurth tavurth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps it would be better to check if the gotFunc is instanceof a schema?

@cdoshi
Copy link
Author

cdoshi commented Jun 20, 2019

@tavurth yes, it makes sense to check if its a schema. Take a look at how I have done it. I am not sure if that's the right way to do it

@tavurth
Copy link
Contributor

tavurth commented Jun 20, 2019

I see from the Yup documentation, that they have a few different schema types.

Perhaps something like this:

+/**
+ * Returns true if the passed item is a yup.Schema
+ * @param {maybeSchema} item - Item to check
+ */
+function isSchema(item) {
+  return [
+    // All possible yup schema types
+    yup.mixed,
+    yup.date,
+    yup.array,
+    yup.string,
+    yup.number,
+    yup.object,
+    yup.boolean,
+  ].some(schemaType => item instanceof schemaType);
+}
+

@@ -129,7 +146,7 @@ function convertArray(arrayArgument, previousInstance = yup) {
         if (convertedArguments.filter(i => i).length < 1) {
           if (typeof gotFunc === 'function') {
             return gotFunc();
-          } else if (typeof gotFunc === 'object' && gotFunc.constructor.name.toLowerCase().indexOf('schema') > -1) {
+          } else if (isSchema(gotFunc)) {
             return gotFunc;
           }
         }

@tavurth
Copy link
Contributor

tavurth commented Jun 20, 2019

I'm unable to test the above but it seems a little more friendly.

Also please be aware that this repository owner does not seem to accept PRs at this time

@cdoshi
Copy link
Author

cdoshi commented Jun 24, 2019

@tavurth What do you recommend? Create another npm module?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants