Guides  ›  Validations  ›  Custom schemas ← PREV NEXT →

Custom schemas

A schema function can perform asynchronous checks.

module.exports = {
  paths: '/users',
  methods: 'POST',
  schema: async function (state) {
    var email = state.req.body.params?.email
    if (typeof email !== 'string' || !email) {
      return state.jarpc({ ok: false, message: 'Email required' })
    }
    var user = await state.plugins.mongodb
      .collection('users').findOne({ email })
    if (user) {
      return state.jarpc({ ok: false, message: 'Email in use' })
    }
  },
  handler: function (state) {
    return state.jarpc({ ok: true })
  }
}

Return nothing to continue. This example uses the database plugin.

Next: Messages →

Tools for business systems.