对象数组的SimpleSchema验证

时间:2017-07-01 09:17:37

标签: javascript meteor simple-schema

我想验证这个对象数组:

[
    {
        _id     : 'NpxZFT4TwfDvwbtKX',
        parent: 'T4TwfDvwbtKXNpxZF',
        order:  1
    }
]

我该怎么做,因为这不起作用:

new SimpleSchema({
    _id   : { type: SimpleSchema.RegEx.Id },
    parent: { type: SimpleSchema.RegEx.Id },
    order : { type: Number }
})

使用check()

,验证效果如何?

1 个答案:

答案 0 :(得分:2)

关于验证方法的一个很酷的事情是,您可以为方法使用单独的模式而不是文档。这允许您将对象和数组以及其他任何内容传递给方法,并代表数据处理文档。

那么你之前创建的架构怎么样?这还在使用中!如果文档符合给定的模式,它正在检查FOR EACH插入或更新集合中的文档。

代码示例:

import {Meteor} from 'meteor/meteor';
import {Mongo} from 'meteor/mongo';
import {ValidatedMethod} from 'meteor/mdg:validated-method';
import {Random} from 'meteor/random';

import SimpleSchema from 'simpl-schema';

// schema for document on collectionm level
const documentSchema = new SimpleSchema({
    // id optional otherwise you can't insert new docs
    _id: {type: SimpleSchema.RegEx.Id, optional: true},
    parent: {type: SimpleSchema.RegEx.Id},
    order: {type: Number}
});

// attach schema to a new collection
const testCollection = new Mongo.Collection("testcollection");
testCollection.schema = documentSchema;
testCollection.attachSchema(documentSchema)


// create method schema
// see that we attach the document schema
// as type of the items of the array
const methodSchema = new SimpleSchema({
    data: {type: Array},
    "data.$": {type: documentSchema},
});

// insert method using the method schema
const insertArrayMethod = new ValidatedMethod({
    name: "insert.array.method",
    validate: methodSchema.validator(),
    run({data}){
        const ret = [];
        for (let input of data) {
            const tmp = Object.assign({}, input);
            tmp._id = testCollection.insert(input);
            ret.push(tmp);
        }
        return ret;
    },
});

// update method using the method schema
const updateArrayMethod = new ValidatedMethod({
    name: "update.array.method",
    validate: methodSchema.validator(),
    run({data}){
        const ret = [];
        for (let input of data) {
            ret.push(testCollection.update(input._id, {$set: {parent: input.parent, order: input.order}}));
        }
        return ret;
    },
});


Meteor.startup(() => {
    const data = [
        {parent: Random.id(17), order: 0},
        {parent: Random.id(17), order: 1},
        {parent: Random.id(17), order: 2},
    ];
    console.log("insert data")
    console.log(data);

    const results = Meteor.call("insert.array.method", {data: data});
    console.log("insert result");
    console.log(results);

    for (let element of results) {
        element.order += 5;
    }
    console.log("update data");
    console.log(results);

    // note thet the input variablle in the validated method has to be called data
    const updateResult = Meteor.call("update.array.method", {data: results});
    console.log("update result");
    console.log(updateResult);
});

控制台输出:

I20170701-12:21:38.302(2)? insert data        
I20170701-12:21:38.319(2)? [ { parent: 'wkh3C6NSvZqrewxLh', order: 0 },
I20170701-12:21:38.322(2)?   { parent: 'ezfBAtZrgXgG8dANy', order: 1 },
I20170701-12:21:38.342(2)?   { parent: 'H4eXyR6FJ9sts6Nn2', order: 2 } ]
I20170701-12:21:38.616(2)? insert result
I20170701-12:21:38.621(2)? [ { parent: 'wkh3C6NSvZqrewxLh',
I20170701-12:21:38.624(2)?     order: 0,
I20170701-12:21:38.626(2)?     _id: 'et4hCu2osH7DnbhHo' },
I20170701-12:21:38.633(2)?   { parent: 'ezfBAtZrgXgG8dANy',
I20170701-12:21:38.636(2)?     order: 1,
I20170701-12:21:38.641(2)?     _id: 'ysH3NaydR6PwdTQCr' },
I20170701-12:21:38.656(2)?   { parent: 'H4eXyR6FJ9sts6Nn2',
I20170701-12:21:38.659(2)?     order: 2,
I20170701-12:21:38.660(2)?     _id: 'AQExATqWhGr26FN7A' } ]
I20170701-12:21:38.673(2)? update data
I20170701-12:21:38.681(2)? [ { parent: 'wkh3C6NSvZqrewxLh',
I20170701-12:21:38.683(2)?     order: 5,
I20170701-12:21:38.684(2)?     _id: 'et4hCu2osH7DnbhHo' },
I20170701-12:21:38.696(2)?   { parent: 'ezfBAtZrgXgG8dANy',
I20170701-12:21:38.698(2)?     order: 6,
I20170701-12:21:38.700(2)?     _id: 'ysH3NaydR6PwdTQCr' },
I20170701-12:21:38.701(2)?   { parent: 'H4eXyR6FJ9sts6Nn2',
I20170701-12:21:38.705(2)?     order: 7,
I20170701-12:21:38.707(2)?     _id: 'AQExATqWhGr26FN7A' } ]
I20170701-12:21:38.712(2)? update result
I20170701-12:21:38.714(2)? [ 1, 1, 1 ]

<强>要点:

method-schema - 验证经过验证的方法的输入  document-schema - 验证mongo集合的输入/更新对象