Skip to content

Provide more useful error than "Invalid type, expected array" #625

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

Closed
upq opened this issue Jan 10, 2016 · 32 comments
Closed

Provide more useful error than "Invalid type, expected array" #625

upq opened this issue Jan 10, 2016 · 32 comments

Comments

@upq
Copy link

upq commented Jan 10, 2016

I get this error while the output is exactly how I expect it, the problem is that the form is invalid.

Form

form =    {
                                        "key": "data.answers",
                                        "type": "array",
                                        "items": [
                                            {
                                                "key": "data.answers[]",

                                                "items": [
                                                    {
                                                        "key": "data.answers[].text",
                                                        "type": "text",
                                                        "title": "Question",
                                                    },
                                                ]
                                            }
                                        ]
                                    },

Schema

        scope.schema = {
                        "type": "object",
                        "title": "Types",
                        "properties": {
                            "data": {
                                "type": "object",
                                "title": "data",
                                "properties": {
                                    "answers": {
                                        "type": "array",
                                        "title":"Answers",
                                        "default": [],
                                        "items": {
                                            "type": "object",
                                            "title": "Answer",
                                            "properties": {
                                                "text": {
                                                    "title": "Answer",
                                                    "type": "text",
                                                    "required": true,
                                                },
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    };

expected output

data = {answers: [{text: "answer1"}, {text: "answer2"}], correct: 1, question: "questioning?"}

Now the expected output is right but the form is invalid saying that it expected an array, anything Im missing here?

@toomuchdesign
Copy link

Same problem here. The validation error (Invalid type, expected array) appears when adding a second item into an array field.

It seems to be a problem related to angular-schema-form-bootstrap. I have no troubles when using the old decorators included in ASF folder.

This is my current setup:

  • angular 1.4.8
  • angular-schema-form 0.8.12
  • angular-schema-form-bootstrap 0.2.0

I'm going to investigate on this in the next days and set up a plunkr if needed.
Any hint would be great!

@toomuchdesign
Copy link

@upq back to your example: are you sure about setting "type" properties to "text"?

I suppose the correct type for your text items in Schema declaration should be "string" ;-)

Follow the schema in the official examples:
http://schemaform.io/examples/bootstrap-example.html#/ce1076e82a119133ebd6

"text": {
    "title": "Answer",
    "type": "string",
    ...
}

@toomuchdesign
Copy link

Meanwhile I found an acceptable solution.

I had the validation issue when trying to get an array of textarea-strings:

[ "Lorem ispsum", "Lorem ispsum", "Lorem ispsum",]

I was able to overtake the validation issue by setting the field up as an array of objects as described in the demo:

[ { "text": "Lorem ispsum" }, { "text": "Lorem ispsum" }, { "text": "Lorem ispsum" }]

@upq
Copy link
Author

upq commented Jan 16, 2016

@toomuchdesign changing the type to string actually worked! its always something dead silly and simple that is missing..

@upq upq closed this as completed Jan 16, 2016
@jugglingcats
Copy link

I'm not sure this should have been closed. While @upq had a typo in his schema, @toomuchdesign did not and had to use a workaround that changed the schema.

We have the following schema fragment:

                    "features" : {
                        "type" : "array",
                        "title" : "Features",
                        "items" : {
                            "type" : "string",
                            "required" :true
                        }
                    }

This works fine with the old decorator but breaks with the new one giving the on-screen error described. We can't easily use the workaround as the documents already created follow the schema above.

@Anthropic
Copy link
Member

I have recently had the same issue but it was due to the data returned from the server changing null to empty string on a field, but rather than show a useful error, I got the above. Suffice to say it took some time to determine that the returned string was failing the array item as it was defined as being an integer value. I think we need to look at improving the feedback on this one.

@toomuchdesign
Copy link

@Anthropic, I confirm your issue: empty strings values returned by the server breaking the validation. I ended up with iterating the returned data object and removing empty values before sending data to the server.

@Anthropic Anthropic changed the title Invalid type, expected array Provide more useful error than "Invalid type, expected array" Mar 22, 2016
@Anthropic Anthropic added this to the Backlog milestone Mar 22, 2016
@Anthropic Anthropic modified the milestone: Backlog Apr 24, 2016
@edi9999
Copy link

edi9999 commented May 20, 2016

I've got the same issue, but with valid input . Here is my code :

angular.module('my_app')
.controller('FoobarController', function($scope) {
    $scope.schema = {
        type: "object",
        properties: {
            "install_packages": {
                "items": {
                    "type": "string"
                },
                "type": "array"
            },
            "install_groups": {
                "items": {
                    "type": "string"
                },
                "type": "array"
            },
        }
  };


  $scope.form = [
    "*",
    {
      type: "submit",
      title: "Save"
    }
  ];

  $scope.model = {};
});

and in my view :

<div ng-controller="FoobarController">
    <form sf-schema="schema" sf-form="form" sf-model="model"></form>
</div>

Now if I fill in the two arrays with multiple text elements, I get the error message : "Invalid type, expected array"

@edi9999
Copy link

edi9999 commented May 20, 2016

This is very strange, since if I set the model to :

$scope.model = {"install_packages":["ewfwef","ewfwe"],"install_groups":["dsafasdf","asdf"]}

However, If I do the same thing manually, the error appears.

@Anthropic
Copy link
Member

@edi9999 it isn't strange at all it matches exactly what was described in the thread above your comments. If you set it to empty to start with it errors. You cannot reset it, too late. This needs work to fix.

@edi9999
Copy link

edi9999 commented May 24, 2016

I was able to get rid of the issue by not using : angular-schema-form-bootstrap 0.2.0, but instead use the one that is in the dist folder of this repository (when you use npm or bower)

@Anthropic
Copy link
Member

@edi9999 thanks for the info :)

@donalmurtagh
Copy link
Contributor

I also get this issue when I use angular-schema-form-bootstrap 0.2.0 with angular-schema-form 0.8.13. Here's a plunker demo. As soon as you add one item to the array you get the field error "Invalid type, expected array" is displayed.

@Anthropic
Copy link
Member

@donalmurtagh I have a fix for that based on comments in #649, will get it out in alpha.3 this week, should have in dev branch in the next day or two I hope.

@donalmurtagh
Copy link
Contributor

@Anthropic Great, I'll try that out alpha3 as soon as it's available. It would really be appreciated if you could mention in the release notes of alpha3 which version of the bootstrap decorator it should be used with.

@mark007
Copy link

mark007 commented Mar 21, 2017

Hi, Id be interested to see your fix for this in alpha 3 also Anthropic. Is a patch already merged for the fix?

@Anthropic
Copy link
Member

@mark007 due to family emergency I haven't gotten alpha 3 out yet, I am hoping to get something out this weekend if everything lines up for me.

@mark007
Copy link

mark007 commented Mar 22, 2017

Thanks Anthropic. In general is there a list of features in lets say json schema draft 4, that asf currently does / doesn't support? I couldn't see any particular list on the github pages themselves, ie whether theres support for validation of things like string types with particular formats, like ipv4 etc. From searching the code I assume things like that aren't in there currently, but just wanted to know was there a place to go to see what is / isn't in there. Its a great project, we would love to contribute to it when we get more familiar with its workings.

@mark007
Copy link

mark007 commented Apr 4, 2017

Can I just confirm guys, that this issue, is different to this other one ( #750 ) one that has had fixes pushed up for it. Thanks very much.

@Anthropic
Copy link
Member

@mark007 It is different, I believe, but I do believe that this issue is fixed by either alpha 3 already or 4 in dev branch. I can't change the error message which comes from tv4, but at least now when you change the value it should remove the error and it should display only when it is accurate.

@mark007
Copy link

mark007 commented Apr 4, 2017

Thanks Anthropic. Thats why I wasn't sure, in my bower.json I'm pulling down todays latest push for angular-schema-form-bootstrap, but on page load, under my array field I still see the error appearing, even after I fill in the individual array items to make them valid.

Do you think I should put together an example or is it easy to also see this on your end. I'm using angular 1.6.4. This is a snippet of what I have in my schema incase you wanted to try.

"ntp_external_servers": {
"type": "array",
"items": {
"type": "string"
},
"minItems": 1,
"default": ["1.2.3.4"]
}

If I load the page with actual data set for this field its fine, its only when I load the page where the user has to initially give data, it doesn't like that this field has nothing set for it yet.

@Anthropic
Copy link
Member

Anthropic commented Apr 4, 2017

@mark007 If you use the example and make a gist with a schema and form definition I can load that on the latest version locally and debug. Which versions is bower pulling down? ps. back on tomorrow

@mark007
Copy link

mark007 commented Apr 4, 2017

Bower sees alpha 3 of angular schema form bootstrap, not an alpha 4 so I use the actual git commit id to get the absolute latest. I'm not sure how to change the versions in the gist link, to use angular schema form bootstrap latest, but even this example should show the issue, if the latest version is used.

http://schemaform.io/examples/bootstrap-example.html#/d127f6b978d7dff299afe010e91c2ea8

@EtiAggarwal
Copy link

I still see the issue on alpha 3 when using array of strings

Anthropic added a commit that referenced this issue Apr 14, 2017
This doesn't remove the error it removes it when an array of strings or
numbers is specified and fails validation by inserting null to force a
value into the array.
@Anthropic Anthropic modified the milestones: 1.0.0, Backlog Apr 14, 2017
@Anthropic
Copy link
Member

@EtiAggarwal @mark007 to be clearer, the main thing I fixed was that once the error appeared you couldn't get rid of it, to fix this entire issue I have updated ASF just now to put in an empty string instead of null, it will be in the next alpha update or you can try the version in the dev branch.

@mark007
Copy link

mark007 commented Apr 18, 2017

Thanks Anthropic, I can't seem to pick it up yet using the schema form bootstrap bundled min file. I guess its not merged yet, I will pick it up once its there.

Thanks again.

@Anthropic
Copy link
Member

@mark007 it should be, what version of the file are you using, what's the version headers say?

@mark007
Copy link

mark007 commented Apr 18, 2017

This is from the header of the bundled asf bootstrap file, looks like it was just built yesterday.

/*!

  • angular-schema-form-bootstrap
  • @Version 1.0.0-alpha.4
  • @Date Mon, 17 Apr 2017 08:57:43 GMT

I still have the issue if I don't set all of the array fields to [] before passing to the form, the error is appearing for me. Whats the easiest way to reproduce this for you. gist doesn't seem to have the ability to let me pick the exact version of the ASF bootstrap.

I wonder did the bundled bootstrap one get the fix merged in from the other repo.

@Anthropic
Copy link
Member

Anthropic commented Apr 18, 2017

@mark007 plunkers are the best method @warmy1004 has made some great ones, he bothered to put in multiple versions so you can compare, try this one and modify how you like the one thing I would ask is that you make sure the schema and form definition are json compatible and not js or I have to go and add quote on all the properties if I want to test it in my local test instance :)

@Tarjei400
Copy link

Tarjei400 commented Jul 10, 2017

Hi I took a liberty to tweak this example and to show there still is issue going here here Invalid type, expected array. To reproduce you just need to add new element. Also if any of child elements pass validation and you remove last one, it works properly

@Tarjei400
Copy link

Tarjei400 commented Jul 10, 2017

It seems like the parent form isn't updated when sub item changes. If I add a requirement on a string in items array like maxLength: 5 it gets even more weirder

@Anthropic
Copy link
Member

@Tarjei400 that's using alpha.2 the current one is alpha.4 and dev branch is a little ahead of that already.

daklaus pushed a commit to daklaus/angular-schema-form that referenced this issue Jul 10, 2019
This doesn't remove the error it removes it when an array of strings or numbers is specified and fails validation by inserting null to force a value into the array.
rstijerina pushed a commit to TACC/Core-Portal that referenced this issue Mar 25, 2020
rstijerina pushed a commit to TACC/Core-Portal that referenced this issue Mar 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

9 participants