{"record":{"id":"6323a7690a5134fc","repo":"crowdsecurity/crowdsec","slug":"failed-to-validate-schema-s-w","errorCode":null,"errorMessage":"failed to validate schema %s: %w","messagePattern":"failed to validate schema (.+?): %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pkg/appsec/api_validation/api_validation.go","lineNumber":333,"sourceCode":"\tif err := options.OnMethodNotAllowed.validate(); err != nil {\n\t\treturn fmt.Errorf(\"on_method_not_allowed: %w\", err)\n\t}\n\tif err := options.OnUnsupportedSecurityScheme.validate(); err != nil {\n\t\treturn fmt.Errorf(\"on_unsupported_security_scheme: %w\", err)\n\t}\n\n\tloader := openapi3.NewLoader()\n\trv.loaders[ref] = loader\n\n\tdoc, err := loader.LoadFromData([]byte(schema))\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to load schema %s: %w\", ref, err)\n\t}\n\n\t// Is it a valid OpenAPI schema?\n\t// TODO: look into opts, should we expose some of them to the user ?\n\tif err := doc.Validate(loader.Context, openapi3.DisableExamplesValidation()); err != nil {\n\t\treturn fmt.Errorf(\"failed to validate schema %s: %w\", ref, err)\n\t}\n\n\trv.warnUnsupportedSecuritySchemes(ref, doc, options.OnUnsupportedSecurityScheme)\n\n\trouter, err := legacyrouter.NewRouter(doc)\n\tif err != nil {\n\t\treturn fmt.Errorf(\"failed to create router for schema ref %s: %w\", ref, err)\n\t}\n\n\trv.openAPISchemas[ref] = SchemaData{\n\t\tSchema:  doc,\n\t\tRouter:  router,\n\t\tOptions: options,\n\t}\n\n\trv.logger.Infof(\"loaded schema for ref %s\", ref)\n\treturn nil\n}","sourceCodeStart":315,"sourceCodeEnd":351,"githubUrl":"https://github.com/crowdsecurity/crowdsec/blob/909b5157986a2b2c2163300fdaef5ed01289f7d2/pkg/appsec/api_validation/api_validation.go#L315-L351","documentation":"After parsing, LoadSchema calls doc.Validate to confirm the document is a valid OpenAPI 3 schema (with examples validation disabled). Structural violations — missing required fields like 'info' or 'paths', invalid parameter/response objects, bad $ref targets inside the document — produce this wrapped error, so a broken spec never reaches request validation.","triggerScenarios":"Loading an OpenAPI document that parses but violates the OpenAPI 3 spec: missing openapi/info/paths, operation without responses, a $ref to a non-existent local component, wrong types on schema attributes.","commonSituations":"Specs exported from tools targeting OpenAPI 2 (Swagger) instead of 3; hand-written specs missing required top-level fields; edited specs with dangling $ref pointers; version drift between spec generator and OpenAPI 3 validation rules.","solutions":["Fix each violation listed in the wrapped validation error message (they name the JSON path).","Convert Swagger 2.0 specs to OpenAPI 3 (e.g. with swagger2openapi) before loading.","Run a local validator (kin-openapi/openapi3filter, spectral) against the file to see all errors up front.","Restore missing required sections: openapi: 3.x.x, info:, paths:."],"exampleFix":"// before\ninfo:            # missing version\n  title: my api\n\n// after\nopenapi: 3.0.3\ninfo:\n  title: my api\n  version: 1.0.0","handlingStrategy":"validation","validationCode":"// full spec validation before registering\nloader := openapi3.NewLoader()\ndoc, err := loader.LoadFromData([]byte(schema))\nif err == nil {\n    if verr := doc.Validate(loader.Context, openapi3.DisableExamplesValidation()); verr != nil {\n        return fmt.Errorf(\"spec %q invalid: %w\", ref, verr)\n    }\n}","typeGuard":null,"tryCatchPattern":"if err := rv.LoadSchema(ref, schema, opts); err != nil {\n    if strings.Contains(err.Error(), \"failed to validate schema\") {\n        log.Errorf(\"openapi validation failed for %s: %v\", ref, err)\n    }\n    return err\n}","preventionTips":["Convert Swagger 2.0 specs to OpenAPI 3 before use","Run doc.Validate (or spectral) in CI as a pre-deploy gate","Keep required top-level fields: openapi, info (with version), paths","Check every $ref resolves within the document"],"tags":["openapi","schema-validation","appsec"],"backgroundTag":"schema-validation-failed","analyzedSha":"909b5157986a2b2c2163300fdaef5ed01289f7d2","analyzedAt":"2026-09-06T12:27:26.012Z","contentChangedAt":"2026-09-06T12:27:26.012Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}