Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error: unknown format "email" #112

Closed
leonardomdr opened this issue Jun 2, 2022 · 4 comments
Closed

Error: unknown format "email" #112

leonardomdr opened this issue Jun 2, 2022 · 4 comments

Comments

@leonardomdr
Copy link

leonardomdr commented Jun 2, 2022

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.

@leonardomdr
Copy link
Author

leonardomdr commented Jun 2, 2022

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.

@simonplend
Copy link
Collaborator

simonplend commented Jun 2, 2022

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

@leonardomdr
Copy link
Author

Aww... I totally missed that one.

Thank you.

@simonplend
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants