| 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 |
// compileNullable compiles the nullable keyword based on OpenAPI version |
| 11 |
func CompileNullable(_ *jsonschema.CompilerContext, obj map[string]any, version VersionType) (jsonschema.SchemaExt, error) { |
| 12 |
v, exists := obj["nullable"] |
| 13 |
if !exists { |
| 14 |
return nil, nil |
| 15 |
} |
| 16 |
|
| 17 |
// check if nullable is used in OpenAPI 3.1+ (not allowed) |
| 18 |
if version == Version31 || version == Version32 { |
| 19 |
return nil, &OpenAPIKeywordError{ |
| 20 |
Keyword: "nullable", |
| 21 |
Message: "The `nullable` keyword is not supported in OpenAPI 3.1+. Use `type: ['string', 'null']` instead", |
| 22 |
} |
| 23 |
} |
| 24 |
|
| 25 |
// validate that nullable is a boolean |
| 26 |
_, ok := v.(bool) |
| 27 |
if !ok { |
| 28 |
return nil, &OpenAPIKeywordError{ |
| 29 |
Keyword: "nullable", |
| 30 |
Message: "nullable must be a boolean value", |
| 31 |
} |
| 32 |
} |
| 33 |
return nil, nil |
| 34 |
} |