json-schema-diff 0.1.0 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 1aea7e4fe80d247f0391631e587a149d622839d7b78b99443e334eea7c422df1
4
- data.tar.gz: 4a9aeceeb2c7d356306cb237c03f8710eb4bd8291d010f583e2d1bb433110046
3
+ metadata.gz: 05eac26b2e1df57105bd4adddc22d72062db1450b442a393a3af227393ec47d6
4
+ data.tar.gz: 6accbbf59486d9ed5ee7b4a725dfb751e3d71038885eefddb5f12bf3071fb8e4
5
5
  SHA512:
6
- metadata.gz: 86c942503b94f7ab01c896fabefa07e9def0701daabff6bf17725dbf9e148c49282293a95e52ebb06a9ee941905c767124134a38243533591bab35b1a59a06c0
7
- data.tar.gz: 5ef4bbfcf3fc8957ec04800a8ea3ea3c3696553dec7f746050194ef6e241c8b55170cfbc87b0505c6238f0af4c5bccef205fe571b451d22cf9f27dcae0727e79
6
+ metadata.gz: 2bfaabdb040888fe7e908a7a52fdf66f6904b506df445c634ec5e14870799c0d80657c1dde023ecdb6d249286926627531152b5d7ef51efa0da6a38e23589f4d
7
+ data.tar.gz: 43356130be48c1566844f3af16a66bb4247ffc053073c3eacc51526949cceeb19fc9e2539df200f6dae5bc8be2d4a478254a47f87014273f464a0effcc644e49
data/CHANGELOG.md CHANGED
@@ -7,6 +7,26 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.2.0] - 2025-01-26
11
+
12
+ ### Added
13
+
14
+ - JSON Schema validation capabilities with `json-schema` gem dependency
15
+ - New CLI options for validation control:
16
+ - `--[no-]validate` for JSON Schema format validation (enabled by default)
17
+ - `--[no-]validate-json` for JSON content validation against schema (disabled by default)
18
+ - Comprehensive error handling for invalid schemas and JSON validation failures
19
+ - Schema structure validation with clear error messages for malformed schemas
20
+ - Type checking validation ensuring JSON data types match schema expectations
21
+ - Required field validation for object schemas
22
+ - Fallback validation approach for complex schemas with external references
23
+
24
+ ### Changed
25
+
26
+ - Updated `json-schema` dependency to version ~> 5.0 for improved performance and compatibility
27
+ - Removed `bigdecimal` dependency (no longer needed with json-schema v5)
28
+ - Enhanced error messages with specific validation failure details
29
+
10
30
  ## [0.1.0] - 2025-01-26
11
31
 
12
32
  ### Added
data/README.md CHANGED
@@ -60,6 +60,19 @@ json-schema-diff --format json schema.json old.json new.json
60
60
  json-schema-diff --no-color schema.json old.json new.json
61
61
  ```
62
62
 
63
+ ### Validation Options
64
+
65
+ ```bash
66
+ # Enable JSON validation against schema (helps catch data format errors)
67
+ json-schema-diff --validate-json schema.json old.json new.json
68
+
69
+ # Disable schema format validation (for malformed schemas)
70
+ json-schema-diff --no-validate schema.json old.json new.json
71
+
72
+ # Both validation options
73
+ json-schema-diff --validate-json --validate schema.json old.json new.json
74
+ ```
75
+
63
76
  ### Ignoring Fields
64
77
 
65
78
  ```bash
@@ -83,6 +96,8 @@ Options:
83
96
  -f, --format FORMAT Output format (pretty, json)
84
97
  -i, --ignore-fields FIELDS Comma-separated list of field paths to ignore
85
98
  --[no-]color Enable/disable colored output (default: enabled)
99
+ --[no-]validate Enable/disable JSON Schema format validation (default: enabled)
100
+ --[no-]validate-json Enable/disable JSON validation against schema (default: disabled)
86
101
  -h, --help Show help message
87
102
  -v, --version Show version
88
103
  ```
@@ -20,12 +20,18 @@ module Json
20
20
  schema_file, old_file, new_file = args
21
21
 
22
22
  begin
23
- schema = SchemaParser.new(schema_file)
23
+ schema = SchemaParser.new(schema_file, validate_schema: options[:validate])
24
24
  comparer = Comparer.new(schema, options[:ignore_fields] || [])
25
25
 
26
26
  old_json = JSON.parse(File.read(old_file))
27
27
  new_json = JSON.parse(File.read(new_file))
28
28
 
29
+ # Validate JSON files against schema if requested
30
+ if options[:validate_json]
31
+ schema.validate_json(old_json)
32
+ schema.validate_json(new_json)
33
+ end
34
+
29
35
  diff_result = comparer.compare(old_json, new_json)
30
36
 
31
37
  formatter = Formatter.new(options[:format], options[:color])
@@ -48,7 +54,9 @@ module Json
48
54
  options = {
49
55
  format: "pretty",
50
56
  color: true,
51
- ignore_fields: []
57
+ ignore_fields: [],
58
+ validate: true,
59
+ validate_json: false
52
60
  }
53
61
 
54
62
  parser = OptionParser.new do |opts|
@@ -77,6 +85,14 @@ module Json
77
85
  options[:color] = color
78
86
  end
79
87
 
88
+ opts.on("--[no-]validate", "Enable/disable JSON Schema format validation (default: enabled)") do |validate|
89
+ options[:validate] = validate
90
+ end
91
+
92
+ opts.on("--[no-]validate-json", "Enable/disable JSON validation against schema (default: disabled)") do |validate_json|
93
+ options[:validate_json] = validate_json
94
+ end
95
+
80
96
  opts.on("-h", "--help", "Show this help message") do
81
97
  puts opts
82
98
  exit 0
@@ -6,14 +6,42 @@ module Json
6
6
  class SchemaParser
7
7
  attr_reader :schema
8
8
 
9
- def initialize(schema_file)
9
+ def initialize(schema_file, validate_schema: true)
10
10
  @schema = JSON.parse(File.read(schema_file))
11
+
12
+ if validate_schema
13
+ validate_basic_schema_structure!
14
+ end
11
15
  rescue JSON::ParserError => e
12
16
  raise Error, "Invalid JSON schema: #{e.message}"
13
17
  rescue Errno::ENOENT => e
14
18
  raise Error, "Schema file not found: #{e.message}"
15
19
  end
16
20
 
21
+ def validate_json(json_data)
22
+ # Simple structural validation - check if JSON structure roughly matches schema expectations
23
+ begin
24
+ # Basic check - if schema has type "array", JSON should be array, etc.
25
+ if @schema["type"] == "array" && !json_data.is_a?(Array)
26
+ raise Error, "JSON validation failed: Expected array but got #{json_data.class.name.downcase}"
27
+ elsif @schema["type"] == "object" && !json_data.is_a?(Hash)
28
+ raise Error, "JSON validation failed: Expected object but got #{json_data.class.name.downcase}"
29
+ end
30
+
31
+ # If we have required fields in schema, check they exist in JSON
32
+ if @schema["required"] && json_data.is_a?(Hash)
33
+ missing_fields = @schema["required"] - json_data.keys
34
+ unless missing_fields.empty?
35
+ raise Error, "JSON validation failed: Missing required fields: #{missing_fields.join(', ')}"
36
+ end
37
+ end
38
+
39
+ true
40
+ rescue StandardError => e
41
+ raise Error, "JSON validation error: #{e.message}"
42
+ end
43
+ end
44
+
17
45
  def get_field_info(path)
18
46
  field_schema = traverse_schema(path.split('.'))
19
47
  return {} unless field_schema
@@ -49,6 +77,18 @@ module Json
49
77
 
50
78
  private
51
79
 
80
+ def validate_basic_schema_structure!
81
+ # Basic structural validation for schema
82
+ unless @schema.is_a?(Hash)
83
+ raise Error, "Schema must be a JSON object"
84
+ end
85
+
86
+ # Check for basic schema structure
87
+ if @schema["type"].nil? && @schema["properties"].nil? && @schema["items"].nil?
88
+ raise Error, "Schema appears to be missing basic JSON Schema structure (no type, properties, or items)"
89
+ end
90
+ end
91
+
52
92
  def traverse_schema(path_parts)
53
93
  current = @schema
54
94
 
@@ -3,7 +3,7 @@
3
3
  module Json
4
4
  module Schema
5
5
  module Diff
6
- VERSION = "0.1.0"
6
+ VERSION = "0.2.0"
7
7
  end
8
8
  end
9
9
  end
@@ -1,6 +1,7 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "json"
4
+ require "json-schema"
4
5
  require "optparse"
5
6
  require_relative "diff/version"
6
7
  require_relative "diff/cli"
metadata CHANGED
@@ -1,14 +1,28 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json-schema-diff
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Nesbitt
8
8
  bindir: exe
9
9
  cert_chain: []
10
10
  date: 1980-01-02 00:00:00.000000000 Z
11
- dependencies: []
11
+ dependencies:
12
+ - !ruby/object:Gem::Dependency
13
+ name: json-schema
14
+ requirement: !ruby/object:Gem::Requirement
15
+ requirements:
16
+ - - "~>"
17
+ - !ruby/object:Gem::Version
18
+ version: '5.0'
19
+ type: :runtime
20
+ prerelease: false
21
+ version_requirements: !ruby/object:Gem::Requirement
22
+ requirements:
23
+ - - "~>"
24
+ - !ruby/object:Gem::Version
25
+ version: '5.0'
12
26
  description: A Ruby gem that performs semantic diffs between JSON files, using JSON
13
27
  Schema to guide and annotate the diff output with type information, field metadata,
14
28
  and structured change detection.