Files
regorus/src/schema/meta.rs
Anand Krishnamoorthi 77f8544868 feat: Type System (#452)
Details:

- Implement complete Type enum with 12 variants: Any, Integer, Number, Boolean,
  Null, String, Array, Set, Object, Enum, Const, AnyOf
- Add Schema wrapper struct with reference counting for efficient sharing
- Support JSON Schema-compatible deserialization with serde
- Implement discriminated subobjects for polymorphic type definitions
- Add comprehensive test suite covering all type variants
- Include Azure resource schema examples (Storage, VM, Key Vault, App Service)
- Create meta-schema validation system with lazy static validator
- Add extensive edge case and corner case test coverage
- Implement custom deserializers for complex schema patterns

This establishes the foundation for type checking and validation of Rego
policies, particularly useful for cloud resource schemas and policy validation.

Regorus's type system is a first of many features intended to
enable type checking and various other constraints on Rego policies.

The type system is inspired from:
   - JSON schema
   - Bicep

Signed-off-by: Anand Krishnamoorthi <anakrish@microsoft.com>
2025-08-11 15:40:45 -05:00

59 lines
2.0 KiB
Rust

// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#![allow(dead_code)]
use crate::*;
use lazy_static::lazy_static;
const META_SCHEMA: &str = include_str!("meta.schema.json");
lazy_static! {
/// Lazy static JSON Schema validator for the Regorus meta-schema.
/// This validator is initialized once and can be used to validate
/// any schema definition against the Regorus meta-schema format.
static ref META_SCHEMA_VALIDATOR: jsonschema::Validator = {
let meta_schema_json: serde_json::Value = serde_json::from_str(META_SCHEMA)
.expect("META_SCHEMA should be valid JSON");
jsonschema::validator_for(&meta_schema_json)
.expect("META_SCHEMA should be a valid JSON Schema")
};
}
pub fn get_meta_schema() -> &'static str {
META_SCHEMA
}
/// Validates a schema definition against the Regorus meta-schema.
/// Returns true if the schema is valid, false otherwise.
pub fn validate_schema(schema: &serde_json::Value) -> bool {
META_SCHEMA_VALIDATOR.is_valid(schema)
}
/// Validates a schema definition against the Regorus meta-schema.
/// Returns Ok(()) if valid, or Err with validation errors if invalid.
pub fn validate_schema_detailed(schema: &serde_json::Value) -> Result<(), Vec<String>> {
if let jsonschema::BasicOutput::Invalid(errors) = META_SCHEMA_VALIDATOR.apply(schema).basic() {
let msgs: alloc::collections::BTreeSet<String> = errors
.iter()
.map(|e| format!("{}: {}", e.instance_location(), e.error_description()))
.collect();
let msgs: Vec<String> = msgs.into_iter().collect();
return Err(msgs);
}
Ok(())
}
/// Validates a schema definition from a JSON string.
/// Returns true if the schema is valid, false otherwise.
pub fn validate_schema_str(schema_str: &str) -> bool {
match serde_json::from_str::<serde_json::Value>(schema_str) {
Ok(schema) => validate_schema(&schema),
Err(_) => false, // Invalid JSON
}
}
#[cfg(test)]
mod tests;