Skip to content

Validation starting from sub-definition #1186

Closed

Description

@eirnym

Hi,

I have a Json Schema in a single file with multiple objects. root object is not defined and I have only objects in $defs. Could you please suggest how to validate a json against these defintions? E.g. from schema below, how to validate an object against schema which would "start" at #/$defs/Object1VariantA or #/$defs/Object1VariantB or #/$defs/Object1VariantC?

From my point of view nothing wrong could happen if there's a way to start validation from a specific definition, not from the top. This is not a canonical way to do that, but I haven't found anything in json schema RFCs which tells, that validation should start only from the top (#/ reference).

I can't alter the schema, or create multiple files around. Schema below is a very small part of the original one to show the relevant example.

Schema example (in yaml format, as it's way easier for me to write it):

$schema: "https://json-schema.org/draft/2020-12/schema"

$defs:
  IDType:
    type: string
    pattern: '[a-z][a-z0-9]{10}'

  Object1Common:
    type: object
    properties:
      title:
        type: string
        maxLength: 200
      description:
        type: string

  Object1VariantA:
    type: object
    allOf:
      - $ref: "#/$defs/Object1Common"
    required:
      - title

  Object1VariantB:
    type: object
    properties:
      id:
        $ref: "#/$defs/IDType"
    allOf:
      - $ref: "#/$defs/Object1Common"
    required:
      - id
      - title

  Object1VariantC:
    type: object
    allOf:
      - $ref: "#/$defs/Object1Common"

Activity

  1. locked and limited conversation to collaborators on Nov 1, 2023
  2. converted this issue into a discussion #1187 on Nov 1, 2023
  3. added
    InvalidNot a bug, PEBKAC, or an unsupported setup
    on Feb 16, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    InvalidNot a bug, PEBKAC, or an unsupported setup

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions