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)
| 720 | /// |
| 721 | /// * `DscError` - Error if the schema is invalid |
| 722 | pub 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 | /// |
no test coverage detected