MCPcopy Create free account
hub / github.com/PowerShell/DSC / validate_properties

Function validate_properties

lib/dsc-lib/src/dscresources/dscresource.rs:722–750  ·  view source on GitHub ↗

Validates the properties of a resource against its schema. # Arguments `properties` - The properties of the resource to validate. `schema` - The schema to validate against. # Returns `Result<(), DscError>` - Ok if valid, Err with message if invalid. # Errors `DscError` - Error if the schema is invalid

(resource: &DscResource, properties: &Value)

Source from the content-addressed store, hash-verified

720///
721/// * `DscError` - Error if the schema is invalid
722pub fn validate_properties(resource: &DscResource, properties: &Value) -> Result<(), DscError> {
723 // if so, see if it implements validate via the resource manifest
724 let type_name = resource.type_name.clone();
725 if let Some(schema) = &resource.schema {
726 debug!("{}: {type_name} ", t!("dscresources.dscresource.validatingAgainstSchema"));
727 let schema = serde_json::to_value(schema)?;
728 return validate_json(&resource.type_name, &schema, properties);
729 }
730 if let Some(manifest) = resource.manifest.clone() {
731 if manifest.validate.is_some() {
732 debug!("{}: {type_name} ", t!("dscresources.dscresource.resourceImplementsValidate"));
733 let resource_config = properties.to_string();
734 let result = resource.validate(&resource_config)?;
735 if !result.valid {
736 let reason = result.reason.unwrap_or(t!("dscresources.dscresource.noReason").to_string());
737 return Err(DscError::Validation(format!("{}: {type_name} {reason}", t!("dscresources.dscresource.resourceValidationFailed"))));
738 }
739 return Ok(())
740 }
741 // use schema validation
742 trace!("{}: {type_name}", t!("dscresources.dscresource.resourceDoesNotImplementValidate"));
743 let Ok(schema) = resource.schema() else {
744 return Err(DscError::Validation(format!("{}: {type_name}", t!("dscresources.dscresource.noSchemaOrValidate"))));
745 };
746 let schema = serde_json::from_str(&schema)?;
747 return validate_json(&resource.type_name, &schema, properties)
748 }
749 Err(DscError::Validation(format!("{}: {type_name}", t!("dscresources.dscresource.noManifest"))))
750}
751
752/// Validate the JSON against the schema.
753///

Callers 2

add_metadataFunction · 0.85
validate_configFunction · 0.85

Calls 4

validate_jsonFunction · 0.85
schemaMethod · 0.80
from_strFunction · 0.50
validateMethod · 0.45

Tested by

no test coverage detected