question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Required nested properties valid even though they are empty

See original GitHub issue

We are using the library to generate forms for tasks configured by users as part of scientific workflows. Our schemas usually consist of a taskName property (string), and a parameters property (object with nested properties), for example:

{ "properties": {
      "taskName": {
        "type": "string",
        "title": "Task name",
        "description": "(required)",
        "placeholder": "Enter a name for this task"
      },
      "parameters": {
        "type": "object",
        "description": "Parameters",
        "properties": {
          "paramA": {
            "type": "string",
            "title": "Param A",
            "description": "(required)"
          },
          "paramB": {
            "type": "string",
            "title": "Param B",
            "description": "(not required)"
          }
        },
        "required": ["paramA"]
      }
    },
    "required": ["taskName", "parameters"]
}

In this case, top level property taskName is required, as is nested property paramA. Our issue is that required nested properties have the class has-success, with the input having the class ng-valid even though they are empty. But validation seems to be working, since errors for missing property paramA are displayed. This prevents us from using the has-error/has-success or ng-valid/ng-invalid classes to inform the user about which fields are valid and which are not. On the other hand, top level property taskName has the class 'has-error", with the input having the class ng-invalid as expected when empty.

Stackblitz reproducing the issue

Are we missing something here, or is it a bug?

Issue Analytics

  • State:open
  • Created 4 years ago
  • Reactions:2
  • Comments:7

github_iconTop GitHub Comments

2reactions
develAmancommented, Oct 5, 2020

Thank you @daniele-pecora for your quick answer, will try to customize the validator. Nice working sample btw, I will use them in the future 😃

0reactions
daniele-pecoracommented, Oct 1, 2020

Question 1: would be nice to have required working as expected, so how is it possible?

Answer Create a custom schema validator that corrects the error message path, so the error is mapped to the field itself. Working sample: custom-zschema-validator-factory.ts

Question 2: minLength is working as combination of minLength and required now, but what if a field is not required and needs to be set minLength?

Answer Create a custom schema validator that ignores the error if the field is not required. Working sample: fix-optional-empty-fields-z-schema-validator-factory.ts

Usage working sample https://github.com/daniele-pecora/ngx-schema-form-widgets-material

Read more comments on GitHub >

github_iconTop Results From Across the Web

Javax validation on nested objects - not working
Currently, I can validate in my LocationDto that the properties name and building are not null, but I can't validate the presence of...
Read more >
javascript - Check if object contains only empty nested objects
An object is empty if it is null or contains no own properties. If it has properties that are null or (defined as)...
Read more >
Handling JSON null and empty arrays and objects - IBM
JSON data has the concept of null and empty arrays and objects. This section explains how each of these concepts is mapped to...
Read more >
Type.GetNestedTypes Method (System) - Microsoft Learn
An array of Type objects representing the public types nested in the current Type (the search is not recursive), or an empty array...
Read more >
JSON.stringify() - JavaScript - MDN Web Docs
The replacer function is called for the initial object being stringified as well, in which case the key is an empty string (...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found