Class: Rgltf::Validation::Context
- Inherits:
-
Object
- Object
- Rgltf::Validation::Context
- Defined in:
- lib/rgltf/validation/context.rb
Instance Attribute Summary collapse
-
#document ⇒ Object
readonly
Returns the value of attribute document.
-
#errors ⇒ Object
readonly
Returns the value of attribute errors.
-
#json ⇒ Object
readonly
Returns the value of attribute json.
-
#warnings ⇒ Object
readonly
Returns the value of attribute warnings.
Instance Method Summary collapse
- #array(key) ⇒ Object
- #bytes_for_buffer(index) ⇒ Object
- #bytes_for_image(index) ⇒ Object
- #error(code, path, message) ⇒ Object
- #finite_number?(value) ⇒ Boolean
-
#initialize(json, document: nil) ⇒ Context
constructor
A new instance of Context.
- #non_negative_integer(value, path, minimum: 0) ⇒ Object
- #numeric_array(value, length, path) ⇒ Object
- #object(value, path) ⇒ Object
- #reference(collection, value, path, required: false) ⇒ Object
- #warning(code, path, message) ⇒ Object
Constructor Details
#initialize(json, document: nil) ⇒ Context
Returns a new instance of Context.
8 9 10 11 12 13 14 |
# File 'lib/rgltf/validation/context.rb', line 8 def initialize(json, document: nil) @json = json.is_a?(Hash) ? json : {} @document = document @errors = [] @warnings = [] error(:invalid_document, '', 'glTF root must be an object') unless json.is_a?(Hash) end |
Instance Attribute Details
#document ⇒ Object (readonly)
Returns the value of attribute document.
6 7 8 |
# File 'lib/rgltf/validation/context.rb', line 6 def document @document end |
#errors ⇒ Object (readonly)
Returns the value of attribute errors.
6 7 8 |
# File 'lib/rgltf/validation/context.rb', line 6 def errors @errors end |
#json ⇒ Object (readonly)
Returns the value of attribute json.
6 7 8 |
# File 'lib/rgltf/validation/context.rb', line 6 def json @json end |
#warnings ⇒ Object (readonly)
Returns the value of attribute warnings.
6 7 8 |
# File 'lib/rgltf/validation/context.rb', line 6 def warnings @warnings end |
Instance Method Details
#array(key) ⇒ Object
16 17 18 19 20 21 22 23 |
# File 'lib/rgltf/validation/context.rb', line 16 def array(key) value = json[key] return [] if value.nil? return value if value.is_a?(Array) error(:invalid_type, key, 'must be an array') [] end |
#bytes_for_buffer(index) ⇒ Object
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/rgltf/validation/context.rb', line 74 def bytes_for_buffer(index) return unless document buffer = document.buffers[index] return if buffer&.send(:meshopt_decode_target?) buffer&.bytes rescue Error => e error(:missing_buffer, "buffers/#{index}", e.) nil end |
#bytes_for_image(index) ⇒ Object
86 87 88 89 90 91 92 93 |
# File 'lib/rgltf/validation/context.rb', line 86 def bytes_for_image(index) return unless document document.images[index]&.bytes rescue Error => e error(:missing_image, "images/#{index}", e.) nil end |
#error(code, path, message) ⇒ Object
64 65 66 67 |
# File 'lib/rgltf/validation/context.rb', line 64 def error(code, path, ) issue = Validator::Issue.new(code:, path:, message:) errors << issue unless errors.include?(issue) end |
#finite_number?(value) ⇒ Boolean
50 51 52 53 54 |
# File 'lib/rgltf/validation/context.rb', line 50 def finite_number?(value) value.is_a?(Numeric) && (!value.respond_to?(:finite?) || value.finite?) rescue StandardError false end |
#non_negative_integer(value, path, minimum: 0) ⇒ Object
43 44 45 46 47 48 |
# File 'lib/rgltf/validation/context.rb', line 43 def non_negative_integer(value, path, minimum: 0) return true if value.is_a?(Integer) && value >= minimum error(:invalid_integer, path, "must be an integer greater than or equal to #{minimum}") false end |
#numeric_array(value, length, path) ⇒ Object
56 57 58 59 60 61 62 |
# File 'lib/rgltf/validation/context.rb', line 56 def numeric_array(value, length, path) unless value.is_a?(Array) && value.length == length && value.all? { |entry| finite_number?(entry) } error(:invalid_numeric_array, path, "must contain exactly #{length} finite numbers") return false end true end |
#object(value, path) ⇒ Object
25 26 27 28 29 30 |
# File 'lib/rgltf/validation/context.rb', line 25 def object(value, path) return value if value.is_a?(Hash) error(:invalid_type, path, 'must be an object') {} end |
#reference(collection, value, path, required: false) ⇒ Object
32 33 34 35 36 37 38 39 40 41 |
# File 'lib/rgltf/validation/context.rb', line 32 def reference(collection, value, path, required: false) if value.nil? error(:missing_reference, path, 'is required') if required return false end return true if value.is_a?(Integer) && value >= 0 && value < array(collection).length error(:invalid_reference, path, "must reference an existing #{collection} entry") false end |