rsuite / schema-typed

Schema for data modeling & validation
MIT License
198 stars 28 forks source link

feat(Schema): support nested object check with `checkForField` and `checkForFieldAsync` #76

Closed simonguo closed 7 months ago

simonguo commented 7 months ago
const schema = new Schema({
  url: StringType().isURL("Should be a url"),
  user: ObjectType().shape({
    email: StringType().isEmail("Should be an email"),
    age: NumberType().min(18, "Age should be greater than 18"),
    parent: ObjectType().shape({
      email: StringType().isEmail("Should be an email"),
      age: NumberType().min(50, "Age should be greater than 50"),
    }),
  }),
});

const checkResult = schema.checkForField(
  "user.parent.age",
  { user: { parent: { age: 40 } } },
  { nestedObject: true }
);

// output
// { hasError: true, errorMessage: 'Age should be greater than 50' }