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

Problem with array validation #18

Closed
mikeschaekermann opened this issue Aug 28, 2013 · 9 comments
Closed

Problem with array validation #18

mikeschaekermann opened this issue Aug 28, 2013 · 9 comments

Comments

@mikeschaekermann
Copy link

Hi there,

first of all, I would like to express a big Thank You to you for investing the time to create and maintain the simple-schema, collection2 and autoform packages. They are very helpful to me!

Today, however, I experienced an odd issue with array validation. My collection definition and insertion look like the following:

StudiesSmartCollection = new Meteor.SmartCollection 'studies'
@Studies = new Meteor.Collection2 StudiesSmartCollection,
    schema:
        'name':
            label: "study name"
            type: String
        'researchers':
            label: "researchers"
            type: [Object]
            minCount: 1
        'researchers.$._id':
            label: "researcher's ID"
            type: String
        'researchers.$.role':
            label: "researcher's role"
            type: String
            allowedValues: allowedStudyRoleValues

newStudyId = Studies.insert
    name: 'Give-me-a-name study'
    researchers: [
        {
            '_id': Meteor.user()._id
            'role': "creator"
        }
    ]

I am getting the following error trace in the client console:

insert failed: Error: failed validation
    at _.extend._insertOrUpdate (http://localhost:3000/packages/collection2.js?dd6c26f37df1d4741e85f32967cc7f36d5ce8ca2:186:21)
    at _.extend.insert (http://localhost:3000/packages/collection2.js?dd6c26f37df1d4741e85f32967cc7f36d5ce8ca2:196:21)
    at Object.Template.studies.events.click .bd-create-study (http://localhost:3000/studies/main.coffee.js?6f62ebfa8370124f8bc9e97ad5f895778c5e72ba:87:28)
    at Object.<anonymous> (http://localhost:3000/packages/templating.js?05b762a6c16769651cfe346f68018b47eacac716:196:32)
    at Spark.attachEvents._renderer.annotate.range.handler (http://localhost:3000/packages/spark.js?c9939ae1b11c764e0ce2709f852e72f6382467e6:878:38)
    at http://localhost:3000/packages/spark.js?c9939ae1b11c764e0ce2709f852e72f6382467e6:775:11
    at Array.forEach (native)
    at Function._.each._.forEach (http://localhost:3000/packages/underscore.js?a1286cdb983623d4d035c8d7cd7a1e8e15274cf5:130:11)
    at http://localhost:3000/packages/spark.js?c9939ae1b11c764e0ce2709f852e72f6382467e6:774:9
    at http://localhost:3000/packages/universal-events.js?5bfa39513d2caa4af4f829d7f4907afae3601adf:143:26 

Printing the invalid keys to the console (using Studies.simpleSchema().invalidKeys()), yields the following output:

invalid key 1: Object {name: "researchers.$._id", type: "required", message: "researcher's ID is required"}
invalid key 2: Object {name: "researchers.$.role", type: "required", message: "researcher's role is required"}

Adding an attribute optional: true to the fields researchers.$._id and researchers.$.role seems to suppress the error from being thrown and the fields are correctly inserted into the database. However, I would like to make them required fields. Am I missing an important detail here or is that a bug that needs to be fixed?

Cheers,
Mike

@aldeed
Copy link
Collaborator

aldeed commented Aug 29, 2013

Looks like a bug. I will fix it in the devel branch, which I hope to release in a couple days.

@mikeschaekermann
Copy link
Author

Sounds great! Thank you for your immediate reply! :)

@aldeed
Copy link
Collaborator

aldeed commented Aug 29, 2013

This is fixed now in the devel branch. Still some work to do before everything can be released, though.

@mikeschaekermann
Copy link
Author

Thanks for fixing it!

@mikeschaekermann
Copy link
Author

Hi @aldeed, after trying out the new release, I am not quite sure if the array validation is working well now, because I got a Error: failed validation error when trying to push an element into the researchers array (see above):

Studies.update {_id: studyId}, {$push: {'researchers': {_id: userId, role: role}}}

I could not find an error in my code, so I suspect the validation to be broken, because both userId and role have reasonable values when the error is thrown.

Any help would be greatly appreciated! :)

Cheers,
Mike

@aldeed
Copy link
Collaborator

aldeed commented Sep 3, 2013

Hmm, I don't think I tested pushing an object. I'll check it out.

@mikeschaekermann
Copy link
Author

Would be awesome! Thanks! :)

@aldeed
Copy link
Collaborator

aldeed commented Sep 4, 2013

I believe this should be fixed now. Let me know if you're still having problems.

@mikeschaekermann
Copy link
Author

Looks very good, thank you for reacting so quickly! :)

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

No branches or pull requests

2 participants