簡體   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