Meteor-Community-Packages / meteor-simple-schema

Meteor integration package for simpl-schema
https://github.com/Meteor-Community-Packages/meteor-simple-schema
MIT License
920 stars 162 forks source link

Nested / Multi Dimentional Arrays #665

Closed llvasconcellos closed 7 years ago

llvasconcellos commented 7 years ago

I'm having problems saving data with nested arrays. They are empty. Removing the schema works fine.

var Schema = {};

Schema.workHoursInterval = new SimpleSchema({
    start: {
        type: String,
        optional: true
    },
    end: {
        type: String,
        optional: true
    }
});

Schema.UserProfile = new SimpleSchema({
    firstName: {
        type: String
    },
    lastName: {
        type: String
    },
    group: {
        type: String
    }
});

Schema.User = new SimpleSchema({
    profile: {
        type: Schema.UserProfile,
        optional: true
    },
    emails: {
        type: Array
    },
    "emails.$": {
        type: Object
    },
    "emails.$.address": {
        type: String,
        regEx: SimpleSchema.RegEx.Email
    },
    "emails.$.verified": {
        type: Boolean
    },
    createdAt: {
        type: Date
    },
    services: {
        type: Object,
        optional: true,
        blackbox: true
    },
    roles: {
        type: Array,
        optional: true
    },
    'roles.$': {
        type: String
    },
    heartbeat: {
        type: Date,
        optional: true
    },
    specialties: {
        type: Array,
        optional: true
    },
    'specialties.$': {
        type: Specialties,
        autoform: {
            type: "typeahead",
            options: function () {
                var specialties = Specialties.find().fetch().map(function(specialty){ 
                    return {
                        value: specialty.name, 
                        label: specialty.name
                    }; 
                });
                return specialties;
            }
        }
    },
    workHours: {
        type: Array,
        optional: true
    },
    'workHours.$': {
        type: Array,
        optional: true
    },
    'workHours.$.$': {
        type: Schema.workHoursInterval,
        optional: true
    }
});

Meteor.users.attachSchema(Schema.User);

This is what I'm trying to do:

Meteor.users.update(userId, {$set: {
    workHours: [
        [{start: '08:00', end: '12:00'}],
        [{start: '13:30', end: '18:00'}]
    ]
}});

This is what I get:

{
    "_id" : "KXmGNLut4TyJex6Tn",
    "createdAt" : ISODate("2016-10-07T13:33:34.071Z"),
    "services" : {
        "password" : {
            "bcrypt" : "xxxxxxxxx"
        },
        "email" : {
            "verificationTokens" : [ 
                {
                    "token" : "xxxxxxxx",
                    "address" : "xxxxxxx@gmail.com",
                    "when" : ISODate("2016-10-07T13:33:34.099Z")
                }
            ]
        },
        "resume" : {
            "loginTokens" : [ 
                {
                    "when" : ISODate("2016-10-13T01:56:18.214Z"),
                    "hashedToken" : "xxxxxxxxxxx"
                }
            ]
        }
    },
    "emails" : [ 
        {
            "address" : "xxxxxxx@gmail.com",
            "verified" : true
        }
    ],
    "profile" : {
        "firstName" : "Denise",
        "lastName" : "Lima",
        "group" : "fdasfdafda",
        "language" : "en"
    },
    "roles" : [ 
        "default", 
        "super-admin"
    ],
    "isUserEnabled" : false,
    "group" : "administration",
    "specialties" : [ 
        "xxxxxxxxxxxx", 
        "aaaaaaaaa"
    ],
    "workHours" : [ 
        [ 
            {}
        ], 
        [ 
            {}
        ]
    ]
}

And this is what I get without the schema:

....
"group" : "administration",
    "specialties" : [ 
        "aaaaaa", 
        "ssssss"
    ],
    "workHours" : [ 
        [ 
            {
                "start" : "08:00",
                "end" : "12:00"
            }
        ], 
        [ 
            {
                "start" : "13:30",
                "end" : "18:00"
            }
        ]
    ],
    "heartbeat" : ISODate("2016-10-13T01:59:04.287Z")
llvasconcellos commented 7 years ago

I created this issue 18 days ago and still no reply. All meteor packages seems abandoned. 46 open issues, 14 open Pull Requests... Packages that the meteor docs advise on using. This is not good.

llvasconcellos commented 7 years ago

If anyone have this problem I solved it using the option "blackbox: true" like this:

    workHours: {
        type: [[Schema.workHours]],
        optional: true,
        blackbox: true
    }