simonplend / express-json-validator-middleware

Express middleware for validating requests against JSON schema

Home Page:https://npm.im/express-json-validator-middleware

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Error: unknown format "email"

leonardomdr opened this issue · comments

Hello,

I'm trying to use the email format, but no success.

Here's the schema:
{ "type": "object", "required": ["email", "password"], "properties": { "email": { "type": "string", "format": "email" }, "password": { "type": "string" } } }

Then when the server is starting (even before any request), I get this error:
Error: unknown format "email" ignored in schema at path "#/properties/email"

My Validator is being stantiated with this:
const { validate } = new Validator({ allErrors: true });

If I remove "format": "email" from the schema, validation works fine.

What I'm doing wrong?

Edit: already installed ajv-formats package too, but no success; I don't know how to add the formats I would like to use and connect with the middleware.

Well, I've done like this...

import { Validator } from "express-json-validator-middleware";
import Ajv from "ajv";
import addFormats from "ajv-formats";

const ajv = new Ajv();
addFormats(ajv, { mode: "full" });
const validateEmail = ajv.compile({ format: "email" });

const { validate } = new Validator({
  allErrors: true,
  formats: {
    email: validateEmail,
  },
});

Not sure if it's the best approach, but at least it seems to work.

The Ajv instance is available via the Validator.ajv property. You can configure it to use ajv-formats like this:

const { validate, ajv } = new Validator({});

addFormats(ajv);

Related documentation: https://github.com/simonplend/express-json-validator-middleware#ajv-instance

Aww... I totally missed that one.

Thank you.

Aww... I totally missed that one.

Thank you.

No problem! I'll see if I can improve this part of the docs, perhaps by adding a more concrete example with ajv-formats.