【问题标题】:Meteor - node simple schema validate data to match schemaMeteor - 节点简单模式验证数据以匹配模式
【发布时间】:2017-11-01 07:38:11
【问题描述】:

我想将我的 Rest-API 验证更改为 node simple schema 用于架构定义和 collection2@core 用于架构验证。

我想使用Person schema来验证用户提供的data

Schemas = {};
Schemas.Person = new SimpleSchema({
    name: {
        type: String,
        label: "Person's Name",
        unique: true,
        max: 200
    },
    surname: {
        type: String,
        unique: true,
        label: "person's surname"
    },
};

validData = API.utility.validate(data, Schemas.Person });

API: {
  utility: {
    validate: function(data, schema) {
      return "The SimpleSchema Validation";
    }
  }
};

【问题讨论】:

    标签: rest meteor simple-schema


    【解决方案1】:

    这个案例在simpl-schema documentation中有描述

    使用您的架构定义,您可以这样做:

    Schemas.person.validate(data);
    

    如果之后您想查看结果或错误:

    Schemas.person.isValid();
    Schemas.person.validationErrors();
    

    【讨论】:

      猜你喜欢
      • 2023-03-05
      • 1970-01-01
      • 2014-08-26
      • 2023-03-24
      • 2018-10-21
      • 2011-03-04
      • 1970-01-01
      • 1970-01-01
      • 2021-11-03
      相关资源
      最近更新 更多