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

Support for JSON Schema patternProperties #3750

Open
vsiguero opened this issue Jul 26, 2023 · 3 comments
Open

Support for JSON Schema patternProperties #3750

vsiguero opened this issue Jul 26, 2023 · 3 comments

Comments

@vsiguero
Copy link

vsiguero commented Jul 26, 2023

Hey!
I'm actively using your library, thank you very much for your great work, well done. I was wondering though whether you plan to support patternProperties any time soon in your roadmap.

https://json-schema.org/understanding-json-schema/reference/object.html#patternProperties

Thanks in advance!

@pdalfarr
Copy link

pdalfarr commented Apr 14, 2024

@aitboudad
I am using https://github.com/victools/jsonschema-generator/blob/main/slate-docs/source/includes/_javax-validation-module.md to generate JSON Schema on server side.
Formly is able to render most of it, but 'pattern' and 'format' keyword seems not to be honored.

Example:

This Java class

public class User {

    @NotBlank
    private String firstName;

    @NotBlank
    private String lastName;

    @NotNull
    @Min(value = 18)
    private Integer age;

    @NotNull
    @Email
    private String email;

    @NotNull
    @Pattern(regexp = "^[A-Za-z0-9]+$")
    private String password;

    @NotBlank
    @Pattern(regexp = "(\\d{1,3}\\.){3}\\d{1,3}")
    private String ip;

    private String phone;

    private String country;

}

leads to this (generated) JSON schema.
(more precisely, a JSON object containing both the schema and the model)

{
  "schema": {
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "type": "object",
    "properties": {
      "age": {
        "type": "integer",
        "title": "age (Integer)",
        "minimum": 18
      },
      "country": {
        "type": "string",
        "title": "country (String)"
      },
      "email": {
        "type": "string",
        "title": "email (String)",
        "format": "email"
      },
      "firstName": {
        "type": "string",
        "title": "firstName (String)",
        "minLength": 1
      },
      "ip": {
        "type": "string",
        "title": "ip (String)",
        "minLength": 1,
        "pattern": "(\\d{1,3}\\.){3}\\d{1,3}"
      },
      "lastName": {
        "type": "string",
        "title": "lastName (String)",
        "minLength": 1
      },
      "password": {
        "type": "string",
        "title": "password (String)",
        "pattern": "^[A-Za-z0-9]+$"
      },
      "phone": {
        "type": "string",
        "title": "phone (String)"
      }
    },
    "required": [
      "age",
      "email",
      "firstName",
      "ip",
      "lastName",
      "password"
    ]
  },
  "model": {
    "firstName": null,
    "lastName": null,
    "age": null,
    "email": null,
    "password": null,
    "ip": null,
    "phone": null,
    "country": null
  }
}

Formly handles most of it, except:

  • "pattern": "(\d{1,3}\.){3}\d{1,3}" (from "ip" ) and "pattern": "^[A-Za-z0-9]+$" ( from "password")
  • "format" (from "email")

which are not checked / taken into account by Formly.
(as you can see it on screenshot: no error messages near "ip" and "email" )

Any idea on how I can make this work?

image

NOTE

Schema and model here belong to a PoC project to test "Formly and jsonschema-generator" all together.
Having these 2 working fine together would be great!!

@kenisteward
Copy link
Collaborator

@aitboudad
I am using https://github.com/victools/jsonschema-generator/blob/main/slate-docs/source/includes/_javax-validation-module.md to generate JSON Schema on server side.
Formly is able to render most of it, but 'pattern' and 'format' keyword seems not to be honored.

Example:

This Java class

public class User {

    @NotBlank
    private String firstName;

    @NotBlank
    private String lastName;

    @NotNull
    @Min(value = 18)
    private Integer age;

    @NotNull
    @Email
    private String email;

    @NotNull
    @Pattern(regexp = "^[A-Za-z0-9]+$")
    private String password;

    @NotBlank
    @Pattern(regexp = "(\\d{1,3}\\.){3}\\d{1,3}")
    private String ip;

    private String phone;

    private String country;

}

leads to this (generated) JSON schema.
(more precisely, a JSON object containing both the schema and the model)

{
  "schema": {
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "type": "object",
    "properties": {
      "age": {
        "type": "integer",
        "title": "age (Integer)",
        "minimum": 18
      },
      "country": {
        "type": "string",
        "title": "country (String)"
      },
      "email": {
        "type": "string",
        "title": "email (String)",
        "format": "email"
      },
      "firstName": {
        "type": "string",
        "title": "firstName (String)",
        "minLength": 1
      },
      "ip": {
        "type": "string",
        "title": "ip (String)",
        "minLength": 1,
        "pattern": "(\\d{1,3}\\.){3}\\d{1,3}"
      },
      "lastName": {
        "type": "string",
        "title": "lastName (String)",
        "minLength": 1
      },
      "password": {
        "type": "string",
        "title": "password (String)",
        "pattern": "^[A-Za-z0-9]+$"
      },
      "phone": {
        "type": "string",
        "title": "phone (String)"
      }
    },
    "required": [
      "age",
      "email",
      "firstName",
      "ip",
      "lastName",
      "password"
    ]
  },
  "model": {
    "firstName": null,
    "lastName": null,
    "age": null,
    "email": null,
    "password": null,
    "ip": null,
    "phone": null,
    "country": null
  }
}

Formly handles most of it, except:

  • "pattern": "(\d{1,3}\.){3}\d{1,3}" (from "ip" ) and "pattern": "^[A-Za-z0-9]+$" ( from "password")
  • "format" (from "email")

which are not checked / taken into account by Formly.
(as you can see it on screenshot: no error messages near "ip" and "email" )

Any idea on how I can make this work?

image

NOTE

Schema and model here belong to a PoC project to test "Formly and jsonschema-generator" all together.
Having these 2 working fine together would be great!!

Is your issue with patternProperties or the pattern / format keyword?

If the latter I would instead make a new issue so it can be tracked there.

@pdalfarr
Copy link

@aitboudad I tested again on my side, and the 'pattern' keyword is working fine.

I forgot to add {name: 'pattern', message: patternValidationMessage} ... my mistake.
About that, where can I find the list of all 'keywords' supported by Formly?

About the 'format' keyword:
I created a new issue here: #3892

Let me know if it's OK.

Thanks

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

No branches or pull requests

4 participants