对象数组的 SimpleSchema 验证

SimpleSchema validation for object array

我想验证这个对象数组:

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

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

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

使用 check() 验证会是什么样子?

经过验证的方法最棒的一点是,您可以为您的方法使用单独的架构,而不是为文档使用单独的架构。这允许您将对象和数组以及任何其他内容传递给该方法并代表数据处理您的文档。

那么您之前创建的模式呢?这个还在用!如果文档符合给定的架构,它会检查您集合中文档的每个插入或更新。

代码示例:

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 - 验证已验证方法的输入 文档架构 - 验证 mongo 集合

的 input/update 对象