schema

package
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 16, 2025 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ApplyDefaults

func ApplyDefaults(target map[string]any, structural *apiextschema.Structural) map[string]any

ApplyDefaults applies schema default values to a target object using Kubernetes defaulting logic.

This function walks the structural schema and target object in parallel, filling in default values for missing fields. The defaulting algorithm follows the same rules as Kubernetes API server defaulting for CustomResourceDefinitions.

Behavior:

  • Missing fields with defaults are added
  • Existing fields are not overwritten (even if they differ from the default)
  • Nested objects are defaulted recursively
  • Array items are not defaulted (Kubernetes limitation)

The target map is modified in place and also returned for convenience.

Example:

Schema: {replicas: {type: "integer", default: 1}, image: {type: "string"}}
Input:  {image: "nginx"}
Output: {image: "nginx", replicas: 1}

func ToJSONSchema

func ToJSONSchema(def Definition) (*extv1.JSONSchemaProps, error)

ToJSONSchema converts a schema definition into an OpenAPI v3 JSON schema.

This is the primary entry point for converting ComponentType and Trait schemas from the shorthand format into standard JSON Schema that can be used for validation.

Process:

  1. Merge all schema maps (parameters, envOverrides, trait config) into one
  2. Convert from shorthand syntax to full JSON Schema via extractor
  3. Sort required fields for deterministic output

Example input (shorthand):

schemas: [{replicas: "integer | default=1"}, {environment: "string"}]

Example output (JSON Schema):

{type: "object", properties: {replicas: {type: "integer", default: 1}, environment: {type: "string"}}}

func ToStructural

func ToStructural(def Definition) (*apiextschema.Structural, error)

ToStructural converts the definition into a Kubernetes structural schema.

Structural schemas are a stricter variant of JSON Schema used by Kubernetes for:

  1. Server-side validation of CRD instances
  2. Defaulting field values based on schema defaults
  3. Pruning unknown fields during admission

The conversion process:

  1. Convert to standard JSON Schema (OpenAPI v3)
  2. Convert from v1 to internal API version
  3. Validate and convert to structural format (enforces additional constraints)

Structural schema constraints include:

  • All objects must have explicit type: "object"
  • All arrays must specify item schema
  • No x-kubernetes-* extensions in certain contexts

This is primarily used with ApplyDefaults to populate default values.

Types

type Definition

type Definition struct {
	Types   map[string]any
	Schemas []map[string]any
}

Definition represents a schematized object assembled from one or more field maps.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL