skit 0.1.3 → 0.1.4

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: 9cc33f58131c2335f5dad719b28ea332d0f79f2c449009197c918807337c7516
4
- data.tar.gz: 5c2f69177a3080487c122fec666c097faa46ea08eccacdc427aec38b9eb467ca
3
+ metadata.gz: 6be21ee56f12e3c9f8423fa6f2c7c78c037b343da6fe28f22d671c6f4c14f63c
4
+ data.tar.gz: e321613784a55eafc86ac36fce373389ce9cfa95952f4a72d5c14ca4a5f4baa2
5
5
  SHA512:
6
- metadata.gz: 2ce51b62c7eaa852e78500c2626b072d3b0c37b96182c2d71787c266449afd8300849f83e60cf31d0934058607b1a626ec90efbd36e3307bcb1c304cb35f08b2
7
- data.tar.gz: 408211448ab4abd8c0cbe498bc9a49b9c2e39ea3b1a0a9b592fedf00c845a90bcb878ba144cdbbb6647dc0c8b0590a8192682b91900c421376b4f848d0381c62
6
+ metadata.gz: d9638fe3360d250991fc3515e5aff65ae189e71121cbf6e9f4ae93873e00ada3918a831aff0abf27962697f07d35d8ce39731284c8af92a3fce05c8645f9344b
7
+ data.tar.gz: 8380a8ffe62a99b4916c0a77b1ff53a491152bf2457af6ffa54ad0be226ee30727e74bdece9cd45240160f819384eee11b138e131fce92cbd577ddc073d471d8
@@ -6,7 +6,8 @@ module Skit
6
6
  module Definitions
7
7
  # Type alias: Union type of PropertyType-related classes
8
8
  PropertyTypes = T.type_alias do
9
- T.any(PropertyType, ArrayPropertyType, HashPropertyType, UnionPropertyType, ConstType, EnumType)
9
+ T.any(PropertyType, ArrayPropertyType, HashPropertyType, UnionPropertyType, ConstType, EnumType,
10
+ TuplePropertyType)
10
11
  end
11
12
  end
12
13
  end
@@ -73,6 +73,10 @@ module Skit
73
73
  case property_type
74
74
  when ArrayPropertyType
75
75
  types.concat(extract_types_from_property_type(property_type.item_type))
76
+ when TuplePropertyType
77
+ property_type.item_types.each do |item_type|
78
+ types.concat(extract_types_from_property_type(item_type))
79
+ end
76
80
  when HashPropertyType
77
81
  types.concat(extract_types_from_property_type(property_type.value_type))
78
82
  when UnionPropertyType
@@ -0,0 +1,35 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ module Skit
5
+ module JsonSchema
6
+ module Definitions
7
+ class TuplePropertyType
8
+ extend T::Sig
9
+
10
+ sig { returns(T::Array[PropertyTypes]) }
11
+ attr_reader :item_types
12
+
13
+ sig { returns(T::Boolean) }
14
+ attr_reader :nullable
15
+
16
+ sig { params(item_types: T::Array[PropertyTypes], nullable: T::Boolean).void }
17
+ def initialize(item_types:, nullable: false)
18
+ @item_types = item_types
19
+ @nullable = nullable
20
+ end
21
+
22
+ sig { returns(String) }
23
+ def to_sorbet_type
24
+ tuple_type = "[#{@item_types.map(&:to_sorbet_type).join(", ")}]"
25
+ nullable ? "T.nilable(#{tuple_type})" : tuple_type
26
+ end
27
+
28
+ sig { returns(TuplePropertyType) }
29
+ def with_nullable
30
+ TuplePropertyType.new(item_types: @item_types, nullable: true)
31
+ end
32
+ end
33
+ end
34
+ end
35
+ end
@@ -312,14 +312,26 @@ module Skit
312
312
  end
313
313
  end
314
314
 
315
- sig { params(schema: T::Hash[String, T.untyped], class_name_path: ClassNamePath).returns(Definitions::ArrayPropertyType) }
315
+ sig do
316
+ params(
317
+ schema: T::Hash[String, T.untyped],
318
+ class_name_path: ClassNamePath
319
+ ).returns(T.any(Definitions::ArrayPropertyType, Definitions::TuplePropertyType))
320
+ end
316
321
  def build_array_type(schema, class_name_path)
317
- if schema["items"]
318
- item_schema = T.cast(schema["items"], T::Hash[String, T.untyped])
319
- item_type = build_property_type(item_schema, class_name_path.append("item"))
322
+ # Support both prefixItems (2020-12) and items-as-array (draft-04/07) for tuple validation
323
+ tuple_items = schema["prefixItems"] || (schema["items"].is_a?(::Array) ? schema["items"] : nil)
324
+
325
+ if tuple_items
326
+ item_types = tuple_items.each_with_index.map do |item_schema, index|
327
+ item_schema_typed = T.cast(item_schema, T::Hash[String, T.untyped])
328
+ build_property_type(item_schema_typed, class_name_path.append("item#{index}"))
329
+ end
330
+ Definitions::TuplePropertyType.new(item_types: item_types)
331
+ elsif schema["items"].is_a?(::Hash)
332
+ item_type = build_property_type(schema["items"], class_name_path.append("item"))
320
333
  Definitions::ArrayPropertyType.new(item_type: item_type)
321
334
  else
322
- # Array of T.untyped when items is not specified
323
335
  untyped_item = Definitions::PropertyType.new(base_type: "T.untyped")
324
336
  Definitions::ArrayPropertyType.new(item_type: untyped_item)
325
337
  end
@@ -7,6 +7,7 @@ require_relative "json_schema/definitions/property_type"
7
7
  require_relative "json_schema/definitions/const_type"
8
8
  require_relative "json_schema/definitions/enum_type"
9
9
  require_relative "json_schema/definitions/property_types"
10
+ require_relative "json_schema/definitions/tuple_property_type"
10
11
  require_relative "json_schema/definitions/array_property_type"
11
12
  require_relative "json_schema/definitions/hash_property_type"
12
13
  require_relative "json_schema/definitions/union_property_type"
@@ -0,0 +1,78 @@
1
+ # typed: strict
2
+ # frozen_string_literal: true
3
+
4
+ module Skit
5
+ module Serialization
6
+ module Processor
7
+ class Tuple < Base
8
+ extend T::Sig
9
+
10
+ sig { override.params(type_spec: T.untyped).returns(T::Boolean) }
11
+ def self.handles?(type_spec)
12
+ type_spec.is_a?(T::Types::FixedArray)
13
+ end
14
+
15
+ sig { params(type_spec: T.untyped, registry: Registry).void }
16
+ def initialize(type_spec, registry:)
17
+ super
18
+ unless type_spec.is_a?(T::Types::FixedArray)
19
+ raise ArgumentError, "Expected T::Types::FixedArray, got #{type_spec.class}"
20
+ end
21
+
22
+ @element_types = T.let(type_spec.types, T::Array[T.untyped])
23
+ end
24
+
25
+ sig { override.params(value: T.untyped, path: Path).returns(T::Array[T.untyped]) }
26
+ def serialize(value, path: Path.new)
27
+ raise SerializeError.new("Expected Array, got #{value.class}", path: path) unless value.is_a?(::Array)
28
+
29
+ value.each_with_index.map do |item, index|
30
+ element_type = @element_types[index]
31
+ if element_type
32
+ processor = @registry.processor_for(element_type)
33
+ processor.serialize(item, path: path.append(index))
34
+ else
35
+ item
36
+ end
37
+ end
38
+ end
39
+
40
+ sig { override.params(value: T.untyped, path: Path).returns(T::Array[T.untyped]) }
41
+ def deserialize(value, path: Path.new)
42
+ raise DeserializeError.new("Expected Array, got #{value.class}", path: path) unless value.is_a?(::Array)
43
+
44
+ value.each_with_index.map do |item, index|
45
+ element_type = @element_types[index]
46
+ if element_type
47
+ processor = @registry.processor_for(element_type)
48
+ processor.deserialize(item, path: path.append(index))
49
+ else
50
+ item
51
+ end
52
+ end
53
+ end
54
+
55
+ sig do
56
+ override.params(
57
+ value: T.untyped,
58
+ path: Path,
59
+ blk: T.proc.params(type_spec: T.untyped, node: T.untyped, path: Path).void
60
+ ).void
61
+ end
62
+ def traverse(value, path: Path.new, &blk)
63
+ super
64
+
65
+ return unless value.is_a?(::Array)
66
+
67
+ value.each_with_index do |item, index|
68
+ element_type = @element_types[index]
69
+ if element_type
70
+ processor = @registry.processor_for(element_type)
71
+ processor.traverse(item, path: path.append(index), &blk)
72
+ end
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
@@ -16,6 +16,7 @@ require_relative "serialization/processor/date"
16
16
  require_relative "serialization/processor/time"
17
17
  require_relative "serialization/processor/struct"
18
18
  require_relative "serialization/processor/simple_type"
19
+ require_relative "serialization/processor/tuple"
19
20
  require_relative "serialization/processor/array"
20
21
  require_relative "serialization/processor/hash"
21
22
  require_relative "serialization/processor/nilable"
@@ -39,6 +40,7 @@ module Skit
39
40
  # Register processors in order of specificity (most specific first)
40
41
  registry.register(Processor::Nilable)
41
42
  registry.register(Processor::Union)
43
+ registry.register(Processor::Tuple)
42
44
  registry.register(Processor::Array)
43
45
  registry.register(Processor::Hash)
44
46
  registry.register(Processor::JsonSchemaConst)
data/lib/skit/version.rb CHANGED
@@ -2,5 +2,5 @@
2
2
  # frozen_string_literal: true
3
3
 
4
4
  module Skit
5
- VERSION = "0.1.3"
5
+ VERSION = "0.1.4"
6
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: skit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: 0.1.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Speria, inc.
@@ -81,6 +81,7 @@ files:
81
81
  - lib/skit/json_schema/definitions/property_types.rb
82
82
  - lib/skit/json_schema/definitions/struct.rb
83
83
  - lib/skit/json_schema/definitions/struct_property.rb
84
+ - lib/skit/json_schema/definitions/tuple_property_type.rb
84
85
  - lib/skit/json_schema/definitions/union_property_type.rb
85
86
  - lib/skit/json_schema/naming_utils.rb
86
87
  - lib/skit/json_schema/schema_analyzer.rb
@@ -103,6 +104,7 @@ files:
103
104
  - lib/skit/serialization/processor/struct.rb
104
105
  - lib/skit/serialization/processor/symbol.rb
105
106
  - lib/skit/serialization/processor/time.rb
107
+ - lib/skit/serialization/processor/tuple.rb
106
108
  - lib/skit/serialization/processor/union.rb
107
109
  - lib/skit/serialization/registry.rb
108
110
  - lib/skit/version.rb