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

Dev/kbroch/poc sourcemeta jsonschema tool #401

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

kbroch-rivosinc
Copy link
Collaborator

@kbroch-rivosinc kbroch-rivosinc commented Jan 9, 2025

Just proof of concept of this tool https://github.com/sourcemeta/jsonschema
Don't expect this PR to go anywhere mostly just capturing results.

Has a lint feature that found some anti-patterns in the schema files. The --fix option also formats the files so I did that commit separately to make it easier to see the anti-patterns.

Here's the log output:

jsonschema lint schemas/.
/Users/kbroch/rvi/repos/riscv-software-src/riscv-unified-db/schemas/cert_class_schema.json:
  Setting `type` alongside `const` is considered an anti-pattern, as the constant already implies its respective type (const_with_type)
    at schema location "/properties/$schema"
/Users/kbroch/rvi/repos/riscv-software-src/riscv-unified-db/schemas/cert_class_schema.json:
  Setting `type` alongside `const` is considered an anti-pattern, as the constant already implies its respective type (const_with_type)
    at schema location "/properties/kind"
/Users/kbroch/rvi/repos/riscv-software-src/riscv-unified-db/schemas/cert_class_schema.json:
  Setting `type` alongside `enum` is considered an anti-pattern, as the enumeration choices already imply their respective types (enum_with_type)
    at schema location "/properties/processor_kind"
/Users/kbroch/rvi/repos/riscv-software-src/riscv-unified-db/schemas/cert_class_schema.json:
  Setting `type` alongside `enum` is considered an anti-pattern, as the enumeration choices already imply their respective types (enum_with_type)
    at schema location "/properties/mandatory_priv_modes/items"
/Users/kbroch/rvi/repos/riscv-software-src/riscv-unified-db/schemas/cert_model_schema.json:
  Setting `type` alongside `const` is considered an anti-pattern, as the constant already implies its respective type (const_with_type)
    at schema location "/properties/kind"
/Users/kbroch/rvi/repos/riscv-software-src/riscv-unified-db/schemas/cert_model_schema.json:
  Setting `type` alongside `const` is considered an anti-pattern, as the constant already implies its respective type (const_with_type)
    at schema location "/properties/$schema"
/Users/kbroch/rvi/repos/riscv-software-src/riscv-unified-db/schemas/cert_model_schema.json:
  Setting `type` alongside `enum` is considered an anti-pattern, as the enumeration choices already imply their respective types (enum_with_type)
    at schema location "/properties/base"
/Users/kbroch/rvi/repos/riscv-software-src/riscv-unified-db/schemas/config_schema.json:
  Setting `type` alongside `enum` is considered an anti-pattern, as the enumeration choices already imply their respective types (enum_with_type)
    at schema location "/properties/type"
/Users/kbroch/rvi/repos/riscv-software-src/riscv-unified-db/schemas/config_schema.json:
  Setting `type` alongside `const` is considered an anti-pattern, as the constant already implies its respective type (const_with_type)
    at schema location "/properties/$schema"
/Users/kbroch/rvi/repos/riscv-software-src/riscv-unified-db/schemas/config_schema.json:
  Setting `type` alongside `const` is considered an anti-pattern, as the constant already implies its respective type (const_with_type)
    at schema location "/properties/kind"
/Users/kbroch/rvi/repos/riscv-software-src/riscv-unified-db/schemas/json-schema-draft-07.json:
  Setting the `items` keyword to the true schema does not add any further constraint (items_schema_default)
    at schema location "/properties/examples"
/Users/kbroch/rvi/repos/riscv-software-src/riscv-unified-db/schemas/json-schema-draft-07.json:
  Setting the `items` keyword to the true schema does not add any further constraint (items_schema_default)
    at schema location "/properties/enum"
/Users/kbroch/rvi/repos/riscv-software-src/riscv-unified-db/schemas/manual_schema.json:
  Setting `type` alongside `const` is considered an anti-pattern, as the constant already implies its respective type (const_with_type)
    at schema location "/properties/$schema"
/Users/kbroch/rvi/repos/riscv-software-src/riscv-unified-db/schemas/manual_schema.json:
  Setting `type` alongside `const` is considered an anti-pattern, as the constant already implies its respective type (const_with_type)
    at schema location "/properties/kind"
/Users/kbroch/rvi/repos/riscv-software-src/riscv-unified-db/schemas/manual_version_schema.json:
  Setting `type` alongside `const` is considered an anti-pattern, as the constant already implies its respective type (const_with_type)
    at schema location "/properties/$schema"
/Users/kbroch/rvi/repos/riscv-software-src/riscv-unified-db/schemas/manual_version_schema.json:
  Setting `type` alongside `const` is considered an anti-pattern, as the constant already implies its respective type (const_with_type)
    at schema location "/properties/kind"
/Users/kbroch/rvi/repos/riscv-software-src/riscv-unified-db/schemas/profile_class_schema.json:
  Setting `type` alongside `const` is considered an anti-pattern, as the constant already implies its respective type (const_with_type)
    at schema location "/properties/$schema"
/Users/kbroch/rvi/repos/riscv-software-src/riscv-unified-db/schemas/profile_class_schema.json:
  Setting `type` alongside `const` is considered an anti-pattern, as the constant already implies its respective type (const_with_type)
    at schema location "/properties/kind"
/Users/kbroch/rvi/repos/riscv-software-src/riscv-unified-db/schemas/profile_class_schema.json:
  Setting `type` alongside `enum` is considered an anti-pattern, as the enumeration choices already imply their respective types (enum_with_type)
    at schema location "/properties/processor_kind"
/Users/kbroch/rvi/repos/riscv-software-src/riscv-unified-db/schemas/profile_release_schema.json:
  Setting `type` alongside `const` is considered an anti-pattern, as the constant already implies its respective type (const_with_type)
    at schema location "/properties/$schema"
/Users/kbroch/rvi/repos/riscv-software-src/riscv-unified-db/schemas/profile_release_schema.json:
  Setting `type` alongside `const` is considered an anti-pattern, as the constant already implies its respective type (const_with_type)
    at schema location "/properties/kind"
/Users/kbroch/rvi/repos/riscv-software-src/riscv-unified-db/schemas/profile_schema.json:
  Setting `type` alongside `const` is considered an anti-pattern, as the constant already implies its respective type (const_with_type)
    at schema location "/properties/$schema"
/Users/kbroch/rvi/repos/riscv-software-src/riscv-unified-db/schemas/profile_schema.json:
  Setting `type` alongside `const` is considered an anti-pattern, as the constant already implies its respective type (const_with_type)
    at schema location "/properties/kind"

@kbroch-rivosinc kbroch-rivosinc self-assigned this Jan 9, 2025
@kbroch-rivosinc kbroch-rivosinc marked this pull request as draft January 9, 2025 06:47
@dhower-qc
Copy link
Collaborator

Cool tool. Regarding the anti-pattern it's pointing out, we do actually (currently) depend on both "type" and "const" being present in some cases. For example, when printing the type of a parameter in generated documentation, we always look at "type" rather than calculating the inferred type from "const".

That said, I see this tool also has a canonicalize command. I wonder if that would add "type" to a "const"-only schema?

@kbroch-rivosinc
Copy link
Collaborator Author

Cool tool. Regarding the anti-pattern it's pointing out, we do actually (currently) depend on both "type" and "const" being present in some cases. For example, when printing the type of a parameter in generated documentation, we always look at "type" rather than calculating the inferred type from "const".

That said, I see this tool also has a canonicalize command. I wonder if that would add "type" to a "const"-only schema?

Good to know the usage, so actually having a check to make sure both are included might be useful.

I tried canonicalize and got something else entirely (NOTE: starting point is from branch with type stripped):

❯ diff profile_schema.json profile_schema.canonicalize.json
8a9
>   "minProperties": 3,
11c12,14
<       "const": "profile_schema.json#"
---
>       "enum": [
>         "profile_schema.json#"
>       ]
14c17,19
<       "const": "profile"
---
>       "enum": [
>         "profile"
>       ]
18c23,24
<       "type": "string"
---
>       "type": "string",
>       "minLength": 0

But given this help description is BinPack related there's probably something to that:

   canonicalize <schema.json>

       Pre-process a JSON Schema into JSON BinPack's canonical form
       for static analysis.

And for fun lint that new result:

❯ sm-jsonschema lint profile_schema.canonicalize.json
/Users/kbroch/rvi/repos/riscv-software-src/riscv-unified-db/schemas/profile_schema.canonicalize.json:
  Setting `minProperties` to a number less than `required` does not add any further constraint (unsatisfiable_min_properties)
    at schema location ""
/Users/kbroch/rvi/repos/riscv-software-src/riscv-unified-db/schemas/profile_schema.canonicalize.json:
  An `enum` of a single value can be expressed as `const` (enum_to_const)
    at schema location "/properties/$schema"
/Users/kbroch/rvi/repos/riscv-software-src/riscv-unified-db/schemas/profile_schema.canonicalize.json:
  An `enum` of a single value can be expressed as `const` (enum_to_const)
    at schema location "/properties/kind"

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