Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

127
Views
Joi, add extension type with argument

I want to add splitArray type to Joi which converts string to an array using method Array.split. As you know, Array.split can split by argument, and I want to pass this argument during creation of the schema and not during it's usage.

So, currently I have this extension that allows me to split string to an array during validation with the use of helpers:

const splitArrayExt = joi => ({
  base: joi.array(),
  coerce: (value, helpers) => ({
    value: value.split ? value.split(helpers.prefs.split ? helpers.prefs.split : /\s+/) : value
  }),
  type: 'splitArray',
)}

The problems is - I have to pass { split: <ANOTHER SPLIT ARG> } every time I call validate.

Is it possible to just do something like this:

const JoiBase = require('joi');
...
const Joi = JoiBase.extend(splitArrayExt);

const schema1 = Joi.splitArray().splitBy('|').items(Joi.string())
const schema2 = Joi.splitArray().splitBy(',').items(Joi.string())
schema1.validate('A|B,C') // values: [ 'A', 'B,C' ]
schema2.validate('A|B,C') // values: [ 'A|B', 'C' ]
about 4 years ago · Juan Pablo Isaza
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!