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
impl SchemaRegistry
Sourcepub fn new() -> Self
pub fn new() -> Self
Create a schema registry with default settings.
Defaults: Kubernetes version 1.30.0, bundled fallback schemas enabled.
Sourcepub fn with_k8s_version(version: String) -> Self
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/...).
Sourcepub fn set_bundled_fallback(&mut self, enabled: bool)
pub fn set_bundled_fallback(&mut self, enabled: bool)
Set whether to use bundled fallback schemas
Sourcepub fn set_k8s_version(&mut self, version: String)
pub fn set_k8s_version(&mut self, version: String)
Set Kubernetes version
Sourcepub fn get_schema_sync(
&mut self,
schema_type: &str,
) -> Result<Value, SchemaError>
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:
- In-memory cache — instant return if previously fetched.
- 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());Sourcepub fn get_schema_url(&self, schema_type: &str) -> Option<String>
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());Sourcepub fn cache_schema(&mut self, schema_type: &str, schema: Value)
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.
Sourcepub fn clear_cache(&mut self)
pub fn clear_cache(&mut self)
Evict all in-memory cached schemas.
Sourcepub fn get_schema_for_type(
&mut self,
yaml_type: YamlType,
) -> Result<Value, SchemaError>
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.
Sourcepub fn list_schema_types(&self) -> Vec<&'static str>
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.