0

I have tried creating a schema and validating yaml file structures based on their datatype and I have been constantly getting the same error no matter how I try to resolve it.

The source code where I'm getting this error is here: Openapi_validation

Error Message:

❌ Validation Error: PointerToNowhere: '/components/schemas/CustomAttribute' does not exist within {'oneOf': [{'type': 'string', 'minLength': 0, 'maxLength': 100, 'default': '', 'pattern': '.*', 'description': 'A string value.'}, {'type': 'integer', 'minimum': 0, 'maximum': 100, 'default': 0, 'description': 'An integer value.'}, {'type': 'boolean', 'default': False, 'enum': [True, False], 'description': 'A boolean value.'}, {'type': 'array', 'items': {'oneOf': [{'type': 'string'}, {'type': 'integer'}, {'type': 'boolean'}, {'$ref': '#/components/schemas/CustomAttribute'}]}, 'default': [], 'description': 'An array of values.'}, {'type': 'object', 'properties': {'default': {'type': 'object'}, 'description': {'type': 'string'}, 'properties': {'type': 'object', 'additionalProperties': {'$ref': '#/components/schemas/CustomAttribute'}}}, 'required': ['default', 'description', 'properties']}]}

Codes to run and check in local machine

openapi_schema.yaml

openapi: 3.0.0
info:
  title: Custom CRD Validation Schema
  version: 1.0.0
components:
  schemas:
    CustomAttribute:
      oneOf:
        - type: string
          minLength: 0
          maxLength: 100
          default: ""
          pattern: ".*"
          description: "A string value."
        - type: integer
          minimum: 0
          maximum: 100
          default: 0
          description: "An integer value."
        - type: boolean
          default: false
          enum: [true, false]
          description: "A boolean value."
        - type: array
          items:
            oneOf:
              - type: string
              - type: integer
              - type: boolean
              - $ref: "#/components/schemas/CustomAttribute"
          default: []
          description: "An array of values."
        - type: object
          properties:
            default:
              type: object
            description:
              type: string
            properties:
              type: object
              additionalProperties:
                $ref: "#/components/schemas/CustomAttribute"
          required: [default, description, properties]

validate_crd.py

import yaml
import json
from openapi_schema_validator import validate
import sys

# Load OpenAPI schema
with open("openapi_schema.yaml", "r") as f:
    openapi_schema = yaml.safe_load(f)

# Load CRD file
with open(sys.argv[1], "r") as f:
    crd_data = yaml.safe_load(f)

# Extract the schema definition correctly
custom_schema = openapi_schema["components"]["schemas"]["CustomAttribute"]

# Validate CRD file
try:
    validate(instance=crd_data["customAttribute"], schema=custom_schema)
    print("✅ CRD is valid.")
except Exception as e:
    print(f"❌ Validation Error: {e}")

valid_crd.yaml

customAttribute:
  type: object
  default: {}
  description: "A complex object with multiple properties."
  properties:
    name:
      type: string
      minLength: 3
      maxLength: 20
      default: "John Doe"
      pattern: "^[A-Za-z ]+$"
      description: "The name of the user."
    age:
      type: integer
      minimum: 18
      maximum: 100
      default: 25
      description: "The age of the user."
    isActive:
      type: boolean
      default: true
      enum: [true, false]
      description: "Indicates if the user is active."
    roles:
      type: array
      default: ["user", "admin"]
      description: "List of user roles."
      items:
        type: string
    settings:
      type: object
      default: {}
      description: "User settings."
      properties:
        theme:
          type: string
          default: "dark"
          description: "Preferred theme."
        notifications:
          type: boolean
          default: true
          description: "Enable or disable notifications."

Steps to Reproduce

  • Run pip install pyyaml openapi_schema_validator
  • Run python validate_crd.py valid_crd.yaml

It would really help if there is any fix for this

1
  • Hello and welcome to stackoverflow. Your schema is complex and unusual. Use of oneOf should be limited to very specific use cases. What is your use case? Commented Feb 24 at 14:16

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.