0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-06 13:08:40 +08:00
discourse/spec/requests/api/shared
Martin Brennan cd98d818dc
DEV: Improve JSON schema failure output in API specs (#41182)
The shared JSON endpoint examples used to validate schemas with a
boolean
assertion and print one ad hoc hint to stdout. A failure was hard to act
on:

```
  VALUE AT "/access_control": {"mandatory_acl" => {}}
  POSSIBLE ISSUE W/: /access_control

  expected: true
       got: false
```

That output hid the real validator error, only showed the first failure,
and did not explain whether the response or schema needed to change.

Build a proper RSpec failure message from all JSONSchemer validation
results
instead. Each error now includes the issue, validator error, data path,
schema path, offending value, optional parent/details context, and a
concrete
suggested fix.

For unexpected properties:

```
  JSON schema validation failed with 1 error:

  1. Unexpected property at /access_control
     Error: object property at `/access_control` is a disallowed additional property
     Data path: /access_control
     Schema path: /additionalProperties
     Value:
       {
         "mandatory_acl": {}
       }
     Suggested fix: If this response/request field is intentional, add this entry
     to the parent schema's `properties` object:
       {
         "access_control": {
           "type": "object",
           "additionalProperties": false,
           "properties": {
             "mandatory_acl": {
               "type": "object",
               "additionalProperties": true
             }
           },
           "required": [
             "mandatory_acl"
           ]
         }
       }
       If the field is always present, also add "access_control" to the parent
       schema's `required` array.
```

For missing required properties, the message now groups missing keys and
shows
the response root plus validator details:

```
  Missing required properties default_archetype, notification_types at root
  Error: object at root is missing required properties: ...
  Details:
    {
      "missing_keys": [
        "default_archetype",
        "notification_types"
      ]
    }
  Suggested fix: Add default_archetype, notification_types to the
  response/request, or remove them from `required` at root schema.
```

For type mismatches, the message now points at the mismatched schema
node and
suggests an inferred replacement shape:

```
  Error: value at `/access_control` is not an array
  Data path: /access_control
  Schema path: /properties/access_control
  Value:
    {
      "mandatory_acl": {}
    }
  Suggested fix: Update the payload to match the documented `type`, or replace
  the schema at /properties/access_control with:
    {
      "type": "object",
      "additionalProperties": false,
      "properties": {
        "mandatory_acl": {
          "type": "object",
          "additionalProperties": true
        }
      },
      "required": [
        "mandatory_acl"
      ]
    }
```
2026-06-25 15:55:40 +10:00
..
shared_examples.rb DEV: Improve JSON schema failure output in API specs (#41182) 2026-06-25 15:55:40 +10:00