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

JSON Schema support - 'format' keyword is not taken into account #3892

Open
pdalfarr opened this issue Apr 15, 2024 · 3 comments
Open

JSON Schema support - 'format' keyword is not taken into account #3892

pdalfarr opened this issue Apr 15, 2024 · 3 comments
Labels

Comments

@pdalfarr
Copy link

pdalfarr commented Apr 15, 2024

Description
JSON Schema 'format' keyword seems not to be honored by Formly.

Minimal Reproduction

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 the '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:

  • "format" (from "email")
    which is not checked / taken into account.
    (as you can see it on screenshot: no error messages near "email" )

image

Any idea on how I can make this work?

Environment

  • Angular version: 15.1.0
  • Formly version: 6.3.0

Additional context

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

Thanks for making the new ticket. It's clear what you were needing!

So currently I believe the format keyword is not or fully supported.

There is however a map function that can be passed to the json schema parser where you can generically add things on to the field based on config.

Internal projects I maintain do this until I can find the time to add the format validation to formly.

@pdalfarr
Copy link
Author

pdalfarr commented Apr 15, 2024

Thanks a lot for your reply.

I'll see what I can do on my side in the meantime. Maybe I'll be able to dynamically change

'format': 'email' 

to something like

'pattern': '<EMAIL_REGEX>'

I think JSON SCHEMA support is an important feature, because it makes Formly compatible with a standard.
And it brings Formly closer to what I would call a 'generic library' / a 'standard library'
( Don't get me wrong: Formly expressions are still great and allow to do a lot of things: great job! )

note

FYI, I added a like towards JSON Schema documentation in the issue description:
JSON Schema 'format' keyword

@pdalfarr
Copy link
Author

pdalfarr commented Apr 15, 2024

Hi,

Maybe I did a mistake on my side...

I just changed my Java backend code from

@NotNull
@Email
private String email;

to

@NotNull
@Email(regexp = "^[a-zA-Z0-9_!#$%&'*+/=?`{|}~^.-]+@[a-zA-Z0-9.-]+$")
private String email;

and generated JSON is now correct: there is now a "pattern" in it 😄
(I just need to improve / tweak a bit the pattern )

  "email": {
    "type": "string",
    "title": "email (String)",
    "format": "email",
    "pattern": "^[a-zA-Z0-9_!#$%&'*+/=?`{|}~^.-]+@[a-zA-Z0-9.-]+$"
  },

And now generated Form is checking the email! Formly is working fine 😄

I suggest we put this issue on hold.
I'll keep investigating a bit about 'format' keyword on my side, and I let you check how 'compatible' Formly is with JSON Schema, more precisely with the "format"keyword.

Thanks again for this great piece of software and for your time 👍

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

No branches or pull requests

2 participants