23

Simple joi validation snippet in javascript.It will simply return an error object when validation fails.

validate.js

const Joi =require("joi");

function validateObject (input) {
const schema = {
    key: Joi.string().required(),
  };
  return Joi.validate(input, schema);
};

let {error} = validateObject({key:5})
console.log(error)

Now I am learning typescript and like to do the exact functionality in TS.I am aware that Joi is a javascript library but can we make use of it in Typescript.When exploring I came across some alternatives like https://github.com/joiful-ts/joiful.

I am curious to know if there is any straightforward approach using Joi directly in typescript. Or little bit of changes to make the Joi work exactly like in Javascript.

WHAT I TRIED

validate.ts

import * as Joi from "joi";

export const validateObject = (input: object) => {
const schema = {
    home: Joi.string().required(),
  };
  return Joi.validate(input, schema);
};
validateObject({key:5})

While compiling, I got the error

Cannot find name 'Iterable'.

703 map(iterable: Iterable<[string | number | boolean | symbol, symbol]> | { [key: string]: symbol }): this;

UPDATE
I have installed @types/joi as suggested in the answer but still the same error

I am basically looking for validating string,boolean,number,array and object keys as it can be done easily with Joi in Javascript

Community
  • 1
  • 1
Sathya Narayanan GVK
  • 849
  • 2
  • 16
  • 32
  • Can you add your `tsconfig.json` here or any other `tsc` options you might be using? I don't think this is a `joi` problem and is just a configuration problem. Also confirm your `typescript` version. – Cuthbert Oct 17 '19 at 15:29
  • 1
    What version of joi were you using? Your code works just fine on stackblitz with version 14: https://stackblitz.com/edit/typescript-ccvxmy – alexandru Jul 20 '20 at 13:28

5 Answers5

22

Please change your import

-const Joi =require("joi");
+import Joi from "joi";

And make sure you've installed the types by using

npm install --save-dev @types/joi
Édouard Lopez
  • 40,270
  • 28
  • 126
  • 178
Zakthedev
  • 347
  • 4
  • 7
3

Type definitions for Joi exists: @types/joi or @types/hapi__joi (for joi version 17).

Add those to your package.json, and you should be able to use Joi with Typescript. Generally speaking you should not be downloading seperate libraries to make a package work in Typescript, some definitions should do

alexandru
  • 103
  • 5
Phillip
  • 6,033
  • 3
  • 24
  • 34
3

Try to use this code instead.

import * as Joi from "joi";

export const validateObject = (input: object) => {
    const schema = Joi.object().keys({
      home: Joi.string().required(),
    });
  return schema.validate(input);
};
validateObject({key:5})

I hope you will manage to make it work.

0

I doubt that import * as Joi from "joi" is going to give you much joy. You are importing all exported members with that.

Is there an individual export you are wanting? Use import { IndividualExport } from "joi"

Is there a default export you are wanting? Use import Joi from "joi"

Also is there a reason you are calling Joi in the second example but not the first?

numberjak
  • 1,065
  • 4
  • 13
  • 28
  • sorry joi() was a typo – Sathya Narayanan GVK Oct 16 '19 at 09:44
  • But i get a different error while compiling, ```Cannot find name 'Iterable'. 703 map(iterable: Iterable<[string | number | boolean | symbol, symbol]> | { [key: string]: symbol }): this;``` – Sathya Narayanan GVK Oct 16 '19 at 09:45
  • `import joi from 'joi'` should really do the trick. Then use `joi.validate()` as you would normally. Which version are you running? `14.3.1` works fine for me when importing and using via that method. Also, I'm not sure if it would help but maybe try `const schema = joi.object({ key: joi.string().required() })` for your schema? – Reece Daniels Apr 21 '20 at 19:38
0

Zod seems to be a popular Joi alternative for TypeScrip:

https://www.npmjs.com/package/zod

DBencz
  • 849
  • 1
  • 9
  • 15