Joi extension for age validation

Usage no npm install needed!

<script type="module">
  import joiAge from '';



Greenkeeper badge

Joi extension for age validation.

This Joi extension is extended from base. So, the schema can be constructed using

Why not using .max(), .min(), .less(), .greater() for age validation?

For example

// 18 year ago from "now"
const date18YearsAgo = new Date( - 1000 * 60 * 60 * 24 * 365 * 18);
// try to validate the age larger than or equal to 18 years old
const ageSchema =;

// with same input "2002-02-22" as birthdate with different time to validate.
// if "now" is 2019-12-29, it got ValidationError because the age is under 18
// if "now" is 2020-03-29, it still got ValidationError even if the age now is 18 years old

Running this on server side to validate min 18 years old, it doesn't move dynamically as time passes.

Note: This Joi extension disable some default function date, .max(), .min(), .less(), .greater() for avoid ambiguity validation error message.


  • This requires Joi v17 or newer.

If you are not familiar with Joi, you should read Joi Documentation and API


$ npm install joi-age --save


const Joi = require("@hapi/joi");
const customJoi = Joi.extend(require("joi-age"));

const schema =;

For more usage, check it out test file.