diegohaz / schm

Composable schemas for JavaScript and Node.js

Home Page:https://git.io/schm

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Recursive parse

kivervinicius opened this issue · comments

No works with complex json example:

const schema = require("schm");
const { validate } = schema;
const userSchema = schema({
    name: String,
    age: Number,
    child: {
        name: String,
        age: Number,
    },
});

validate({ name: "John", age: 17, childs: {name: "Test", age: 1} }, userSchema)
    .then(parsedValues => {
        console.log("Yaay!", parsedValues);
    })
    .catch(errors => {
        console.log("Oops!", errors);
    });
commented

It should work. You're passing childs instead of child. Could that be the problem?

I'm sorry, was that, but should I accuse property that is not being passed right? instead of giving only the error "undefined"

commented

Sorry about the delayed response. It'll return the error if you make the properties required:

const userSchema = schema({
  name: {
    type: String,
    required: true
  }
});