restmeteorsimple-schema

Meteor - node simple schema validate data to match schema


I want to change my Rest-API validation to node simple schema for schema definition and collection2@core for schema validation.

I want to use the Person schema to validate the data provided by the users.

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";
    }
  }
};

Solution

  • This case is described in the simpl-schema documentation

    With your schema definition you can just do:

    Schemas.person.validate(data);
    

    If right after that you want to look at the result or the errors:

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