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

feat(ui): introduce fields validation - WF-140 #719

Open
wants to merge 2 commits into
base: dev
Choose a base branch
from

Conversation

madeindjs
Copy link
Collaborator

@madeindjs madeindjs commented Jan 3, 2025

Introduce validator key on components fields to validate the field value using JSON Schema with Ajv like this:

export default {
  writer: {
    name: "Rating Input",
    // ...
    fields: {
      // ...
      minValue: {
        name: "Minimum value",
        type: FieldType.Number,
        default: "1",
        desc: "Valid values are 0 and 1.",
        validator: { type: "number", minimum: 0, maximum: 1 },
      },
    },
  },
};

Everything is handled by a new composable useFieldsErrors which will:

  1. evaluate each field of the component
  2. retrieve the validation of each field
  3. run the validator if any and store the error

image

@madeindjs madeindjs self-assigned this Jan 3, 2025
@madeindjs madeindjs marked this pull request as ready for review January 6, 2025 16:41
@ramedina86
Copy link
Collaborator

Screenshot 2025-01-07 at 11 35 58 When I type a non-object into an Object it fails and then the page stops working.

@ramedina86
Copy link
Collaborator

Screenshot 2025-01-07 at 13 07 53 I realized the messages comes straight from the library, but can we change the formatting so that the first letter is capitalized, as shown in the picture? Right now it starts with "must be" (lowercase).

@ramedina86
Copy link
Collaborator

Also would probably be a good idea to have custom messages, the "yes,no" is also irritating me a bit, I'd much prefer "yes, no".

@madeindjs madeindjs marked this pull request as draft January 7, 2025 17:05
@madeindjs
Copy link
Collaborator Author

Also would probably be a good idea to have custom messages, the "yes,no" is also irritating me a bit, I'd much prefer "yes, no".

@ramedina86, yes, most of the message is coming from the library, but I customized this a bit

message += error.message;
if (Array.isArray(error.params?.allowedValues)) {
message += `: ${error.params.allowedValues}`;
}

I simply have to add a .join(', ') to fix the issue you pointed

@madeindjs
Copy link
Collaborator Author

When I type a non-object into an Object it fails and then the page stops working.

I fixed it throught 04c008d . The issue was not linked to this feature though, it was because the string was converted to an object with the spread operator like this

const a = {..."src"} // { '0': 's', '1': 'r', '2': 'c' }

And a number is not a valid HTML attribute. So I applied a REGEX to filter invalid keys

@madeindjs madeindjs marked this pull request as ready for review January 9, 2025 08:08
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

Successfully merging this pull request may close these issues.

2 participants