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

Does the Fastify.js have trouble with complicated Regex patterns? #921

Closed
hessaam opened this issue Aug 14, 2023 · 4 comments
Closed

Does the Fastify.js have trouble with complicated Regex patterns? #921

hessaam opened this issue Aug 14, 2023 · 4 comments
Labels
help wanted Extra attention is needed

Comments

@hessaam
Copy link

hessaam commented Aug 14, 2023

I declare a simple API route in routes/api/user/v1.0 directory in the index.js file as below:

"use strict";

const { userRegistration } = require("../../../../controller/user/userRegistrationV1");
const createUserFirstVersionSchema = require("../../../../schemas/userRegistrationSchema")

const { authenticator } = require("../../../../handler/auth");

module.exports = async function (fastify, opts) {
  fastify.post("/", { preValidation: authenticator, schema: {body: createUserFirstVersionSchema} }, userRegistration);
  }

This is createUserFirstVersionSchema:

const createUserFirstVersionSchema = {
  type: "object",
  properties: {
    name: { type: "string", minLength: 2, maxLength: 30 },
    family: { type: "string", minLength: 2, maxLength: 30 },
    username: { type: "string", minLength: 2, maxLength: 30, pattern: "^[a-zA-Z0-9_]+$" },
    password: {
      type: "string",
      pattern: "^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[~@#_^*%/.+:;=])[A-Za-z\d~@#_^*%/.+:;=]{8,}$",
    },
    roleName: { type: "string" },
    roleId: {
      type: "string",
      pattern:
        "^[0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12}$",
    },
    type: { type: "string", enum: ["Person", "Company"] },
    status: { type: "string", enum: ["active", "inactive"] },
  },
  required: ["name", "family", "username", "password", "roleId", "type", "status"],
};

module.exports = createUserFirstVersionSchema;

And here is my request cURL:

curl --location 'http://localhost:2000/api/user/v1.0' \

--header 'Content-Type: application/json' \
--header 'Authorization: Bearer xxxx' \
--data-raw '{
    "name": "John",
    "family": "Doe",
    "username": "John_Doe",
    "password": "soheKKyl@2W",
    "type": "Person"
}

As you can see the sent request has a valid password and I expect an error to occur for the lack of roleId but this error occurred:

{
    "code": "VALIDATION_ERROR",
    "message": "Request validation failed",
    "errors": [
        {
            "field": "password",
            "message": "should match pattern \"^(?=.*[a-z])(?=.*[A-Z])(?=.*d)(?=.*[~@#_^*%/.+:;=])[A-Za-zd~@#_^*%/.+:;=]{8,}$\""
        }
    ]
}

Does the Fastify.js have trouble with complicated Regex patterns?

  • node version: 20.2.0
  • fastify version: 3.21.3
  • os: Windows
@hessaam hessaam added the help wanted Extra attention is needed label Aug 14, 2023
@hessaam hessaam changed the title Help Does the Fastify.js have trouble with complicated Regex patterns? Aug 14, 2023
@Uzlopak
Copy link

Uzlopak commented Aug 14, 2023

@ivan-tymoshenko
Potential issue when using the regex to validate in fast-json-stringify?

@hessaam
Copy link
Author

hessaam commented Aug 14, 2023

I created the project with Fastify-CLI. Does it need additional packages?

@metcoder95
Copy link
Member

Did you try the scenario using a custom ajv instance?
As well, maybe if print out the password value on the preValidation hook, you might have an idea of what are you receiving after parsing the request

@Uzlopak
Copy link

Uzlopak commented Feb 3, 2024

Closing due to inactivity

@Uzlopak Uzlopak closed this as completed Feb 3, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

3 participants