json-schema-for-humans icon indicating copy to clipboard operation
json-schema-for-humans copied to clipboard

Hiding Fields

Open aelfric opened this issue 3 years ago • 4 comments

Is there any way to mark some fields in the schema as private so as to hide them from the generated html?

aelfric avatar Apr 16 '21 17:04 aelfric

This is not currently supported.

There is no built-in option for this in JSON schema, but this could be achieved with a special attribute like _hidden for example.

Or were you thinking of a different way?

dblanchette avatar Apr 16 '21 17:04 dblanchette

A special attribute would be fine for my use-case. Would that be difficult to implement?

aelfric avatar Apr 16 '21 18:04 aelfric

I don't think it would be very hard to do. It would have to be disabled by default. Then, the is_displayed property of SchemaNode could simply be changed to return False if the configuration calls for hidden fields and the field has a _hidden attribute in it.

for example, the following schema would not display the age attribute

{
  "$id": "https://example.com/person.schema.json",
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "Person",
  "type": "object",
  "properties": {
    "firstName": {
      "type": "string",
      "description": "The person's first name.",
      "title": "Person"
    },
    "lastName": {
      "type": "string",
      "description": "The person's last name.",
      "title": "Person"
    },
    "age": {
      "description": "Age in years which must be equal to or greater than zero.",
      "type": "integer",
      "minimum": 0,
      "title": "Person",
      "_hidden": true
    }
  }
}

Did you want to submit a PR? I could probably take a look soon-ish otherwise :)

dblanchette avatar Apr 16 '21 18:04 dblanchette

I was able to hack this together without much effort. In my case, I use custom templates and I wanted to hide all properties that started with a '$'. Therefore, I simply modified content.html and towards the end of the file, it reads:

{# Properties, pattern properties, additional properties #}
{%- for sub_property in schema.iterate_properties -%}
    {# Custom modification to remove $ properties from documentation #}
    {%- if not sub_property.property_display_name.startswith("$") -%}
        {% include "section_properties.html" %}
    {%- endif -%}
{%- endfor -%}

stevespringett avatar Dec 29 '21 22:12 stevespringett