aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/src/json
diff options
context:
space:
mode:
authornsfisis <nsfisis@gmail.com>2026-06-25 04:57:03 +0900
committernsfisis <nsfisis@gmail.com>2026-06-25 04:57:20 +0900
commit541a8b4fd2c4b9538e2c88c5a7c4f9ffb5ca25eb (patch)
treecbe1e5e9c8edec2a3a875ef0eade99aa4d38e7c4 /crates/shirabe/src/json
parentc65cab710bb3d79db20862c9361d4dbbac8ac5d7 (diff)
downloadphp-shirabe-541a8b4fd2c4b9538e2c88c5a7c4f9ffb5ca25eb.tar.gz
php-shirabe-541a8b4fd2c4b9538e2c88c5a7c4f9ffb5ca25eb.tar.zst
php-shirabe-541a8b4fd2c4b9538e2c88c5a7c4f9ffb5ca25eb.zip
feat(json): validate JSON schema via the jsonschema crate
Replace the todo!() json_schema::Validator stub with the jsonschema crate. Errors are surfaced as 'property : message'; the message wording follows the jsonschema crate and is *not* justinrainbow-compatible. Port the ComposerSchemaTest and JsonFileTest schema cases to the new wording (noted per test). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Diffstat (limited to 'crates/shirabe/src/json')
-rw-r--r--crates/shirabe/src/json/json_file.rs52
1 files changed, 42 insertions, 10 deletions
diff --git a/crates/shirabe/src/json/json_file.rs b/crates/shirabe/src/json/json_file.rs
index e6cd990..5da1a27 100644
--- a/crates/shirabe/src/json/json_file.rs
+++ b/crates/shirabe/src/json/json_file.rs
@@ -5,7 +5,6 @@ use crate::util::Silencer;
use anyhow::Result;
use indexmap::IndexMap;
use shirabe_external_packages::composer::pcre::{CaptureKey, Preg};
-use shirabe_external_packages::json_schema::Validator;
use shirabe_external_packages::seld::json_lint::JsonParser;
use shirabe_external_packages::seld::json_lint::ParsingException;
use shirabe_php_shim::{
@@ -371,7 +370,7 @@ impl JsonFile {
if schema == Self::STRICT_SCHEMA && is_composer_schema_file {
schema_data = json_decode(&file_get_contents(&schema_file).unwrap_or_default(), false)?;
- if let PhpMixed::Array(map) = &mut schema_data {
+ if let PhpMixed::Object(map) = &mut schema_data {
map.insert("additionalProperties".to_string(), PhpMixed::Bool(false));
map.insert(
"required".to_string(),
@@ -394,16 +393,31 @@ impl JsonFile {
schema_data = PhpMixed::Array(m);
}
- let mut validator = Validator::new();
// convert assoc arrays to objects
- let data_converted = json_decode(&json_encode_ex(data, 0).unwrap_or_default(), false)?;
- validator.check(&data_converted, &schema_data)?;
+ let schema_value = serde_json::to_value(&schema_data)?;
+ let data_value = serde_json::to_value(data)?;
+ let validator = jsonschema::options()
+ .with_retriever(FileRetriever)
+ .build(&schema_value)
+ .map_err(|e| anyhow::anyhow!("{e}"))?;
- if !validator.is_valid() {
- // TODO(phase-c): Validator::get_errors currently returns Vec<String>; original PHP
- // exposes [{property, message}, ...]. Until the validator shim is enriched, surface raw
- // error strings without prop/message splitting.
- let errors: Vec<String> = validator.get_errors();
+ let errors: Vec<String> = validator
+ .iter_errors(&data_value)
+ .map(|error| {
+ let property = error
+ .instance_path()
+ .as_str()
+ .trim_start_matches('/')
+ .replace('/', ".");
+ if property.is_empty() {
+ error.to_string()
+ } else {
+ format!("{} : {}", property, error)
+ }
+ })
+ .collect();
+
+ if !errors.is_empty() {
return Err(JsonValidationException::new(
format!("\"{}\" does not match the expected JSON schema", source),
errors,
@@ -550,3 +564,21 @@ impl JsonFile {
Self::INDENT_DEFAULT.to_string()
}
}
+
+#[derive(Debug)]
+struct FileRetriever;
+
+impl jsonschema::Retrieve for FileRetriever {
+ fn retrieve(
+ &self,
+ uri: &jsonschema::Uri<String>,
+ ) -> Result<serde_json::Value, Box<dyn std::error::Error + Send + Sync>> {
+ match uri.scheme().as_str() {
+ "file" => {
+ let file = std::fs::File::open(uri.path().as_str())?;
+ Ok(serde_json::from_reader(file)?)
+ }
+ scheme => Err(format!("Unknown scheme {scheme}").into()),
+ }
+ }
+}