繁体   English   中英

对象数组的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-验证输入以验证方法document-schema-验证mongo集合的输入/更新对象

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM