Debian Codegraph @

openapi_vocabulary/metadata.go

raw
1 // Copyright 2025 Princess B33f Heavy Industries / Dave Shanley
2 // SPDX-License-Identifier: MIT
3
4 package openapi_vocabulary
5
6 import (
7 "github.com/santhosh-tekuri/jsonschema/v6"
8 )
9
10 // exampleExtension handles the OpenAPI example keyword (metadata only)
11 type exampleExtension struct {
12 example any
13 }
14
15 func (e *exampleExtension) Validate(ctx *jsonschema.ValidatorContext, v any) {
16 // Example keyword is metadata only - no validation needed during runtime
17 }
18
19 // deprecatedExtension handles the OpenAPI deprecated keyword (metadata only)
20 type deprecatedExtension struct {
21 deprecated bool
22 }
23
24 func (d *deprecatedExtension) Validate(ctx *jsonschema.ValidatorContext, v any) {
25 // Deprecated keyword is metadata only - no validation needed during runtime
26 }
27
28 // compileExample compiles the example keyword
29 func CompileExample(ctx *jsonschema.CompilerContext, obj map[string]any, version VersionType) (jsonschema.SchemaExt, error) {
30 v, exists := obj["example"]
31 if !exists {
32 return nil, nil
33 }
34
35 // Example can be any valid JSON value, so we just store it
36 // The main validation is that it exists and is parseable (which it is if we got here)
37 return &exampleExtension{example: v}, nil
38 }
39
40 // compileDeprecated compiles the deprecated keyword
41 func CompileDeprecated(ctx *jsonschema.CompilerContext, obj map[string]any, version VersionType) (jsonschema.SchemaExt, error) {
42 v, exists := obj["deprecated"]
43 if !exists {
44 return nil, nil
45 }
46
47 // Validate that deprecated is a boolean
48 deprecated, ok := v.(bool)
49 if !ok {
50 return nil, &OpenAPIKeywordError{
51 Keyword: "deprecated",
52 Message: "deprecated must be a boolean value",
53 }
54 }
55
56 return &deprecatedExtension{deprecated: deprecated}, nil
57 }