Skip to main content

SchemaRegistry

Struct SchemaRegistry 

Source
pub struct SchemaRegistry { /* private fields */ }
Expand description

Schema registry with in-memory caching and bundled offline fallbacks.

Use SchemaRegistry::new for the default configuration (K8s 1.30, bundled schemas enabled). Call get_schema_sync to retrieve a serde_json::Value schema by type key (e.g. "k8s/deployment").

§Example

use devops_validate::schema::SchemaRegistry;

let mut registry = SchemaRegistry::new();

// Retrieve from bundled fallback (works offline)
let schema = registry.get_schema_sync("k8s/deployment").unwrap();
assert!(schema.is_object());

// Resolve remote URL (for async fetch — registry only provides the URL)
let url = registry.get_schema_url("gitlab-ci").unwrap();
assert!(url.contains("schemastore.org"));

Implementations§

Source§

impl SchemaRegistry

Source

pub fn new() -> Self

Create a schema registry with default settings.

Defaults: Kubernetes version 1.30.0, bundled fallback schemas enabled.

Source

pub fn with_k8s_version(version: String) -> Self

Create a registry targeting a specific Kubernetes version.

The version is used when constructing K8s schema URLs (e.g. "1.28.0"kubernetesjsonschema.dev/v1.28.0/...).

Source

pub fn set_bundled_fallback(&mut self, enabled: bool)

Set whether to use bundled fallback schemas

Source

pub fn set_k8s_version(&mut self, version: String)

Set Kubernetes version

Source

pub fn get_schema_sync( &mut self, schema_type: &str, ) -> Result<Value, SchemaError>

Look up a schema by type key (e.g. "k8s/deployment", "gitlab-ci").

Resolution order:

  1. In-memory cache — instant return if previously fetched.
  2. Bundled fallback — minimal embedded schema (works fully offline).

For remote schema fetching, retrieve the URL with get_schema_url, perform the HTTP request externally, then insert the result via cache_schema.

§Errors

Returns SchemaError::NotFound if schema_type is not in the built-in registry and no bundled fallback exists for it (e.g. for a custom schema key).

§Example
use devops_validate::schema::SchemaRegistry;

let mut r = SchemaRegistry::new();
let schema = r.get_schema_sync("k8s/deployment").unwrap();
assert_eq!(schema["type"], "object");

assert!(r.get_schema_sync("nonexistent/type").is_err());
Source

pub fn get_schema_url(&self, schema_type: &str) -> Option<String>

Resolve the remote URL for a schema type.

Returns None if schema_type is not in SCHEMA_URLS. For Kubernetes schemas the URL contains the configured K8s version.

§Example
use devops_validate::schema::SchemaRegistry;

let r = SchemaRegistry::with_k8s_version("1.28.0".to_string());
let url = r.get_schema_url("k8s/deployment").unwrap();
assert!(url.contains("1.28.0"));
assert!(r.get_schema_url("unknown").is_none());
Source

pub fn cache_schema(&mut self, schema_type: &str, schema: Value)

Insert a remotely-fetched schema into the in-memory cache.

Call this after fetching the schema JSON from the URL returned by get_schema_url.

Source

pub fn clear_cache(&mut self)

Evict all in-memory cached schemas.

Source

pub fn get_schema_for_type( &mut self, yaml_type: YamlType, ) -> Result<Value, SchemaError>

Convenience wrapper around get_schema_sync that accepts a YamlType instead of a string key.

Source

pub fn list_schema_types(&self) -> Vec<&'static str>

Return all schema type keys known to this registry.

These are the valid arguments to get_schema_sync and get_schema_url.

Trait Implementations§

Source§

impl Default for SchemaRegistry

Source§

fn default() -> Self

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.