babl-json 0.5.1 → 0.5.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/babl/schema/any_of.rb +29 -6
- data/lib/babl/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f5116d890ae716b94f6fcb09fd90591313beb68d
|
4
|
+
data.tar.gz: 4bba953360594c5f46ad6160870a2ab469a932af
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4467e3975a1f2f3130f68dc8fed5327b608838f2bcf8eebe61fae29fd5608ecc9de328192a2a2c5797ad54cce7ea68b6e9dc684c7c11ccf1c4a479f0147c54b9
|
7
|
+
data.tar.gz: effc3f204316f2d67f0e74a8f6704fedae840b03ff41129697316c06f169d3a7363a2ccd3f0bfa9042f9398e5fe450b209290d7a1b7c38b1620aff426d086634
|
data/lib/babl/schema/any_of.rb
CHANGED
@@ -12,12 +12,7 @@ module Babl
|
|
12
12
|
end
|
13
13
|
|
14
14
|
def json
|
15
|
-
|
16
|
-
|
17
|
-
simple_enum = { enum: primitives.map(&:value) }
|
18
|
-
return simple_enum if complex.empty?
|
19
|
-
|
20
|
-
{ anyOf: complex.map(&:json) + (primitives.empty? ? [] : [simple_enum]) }
|
15
|
+
json_only_primitives || json_coalesced_types || json_general_case
|
21
16
|
end
|
22
17
|
|
23
18
|
# Perform simple transformations in order to reduce the size of the generated
|
@@ -42,6 +37,34 @@ module Babl
|
|
42
37
|
|
43
38
|
private
|
44
39
|
|
40
|
+
def json_only_primitives
|
41
|
+
return { enum: choice_set.map(&:value) } if choice_set.all? { |c| Primitive === c }
|
42
|
+
end
|
43
|
+
|
44
|
+
def json_coalesced_types
|
45
|
+
remaining = choice_set.dup
|
46
|
+
json_types = []
|
47
|
+
|
48
|
+
[Primitive::NULL, Typed::INTEGER, Typed::BOOLEAN, Typed::NUMBER, Typed::STRING].each do |coalescible|
|
49
|
+
if remaining.include?(coalescible)
|
50
|
+
json_types << coalescible.json[:type]
|
51
|
+
remaining.delete(coalescible)
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
# Note: ideally, we would like to turn :
|
56
|
+
# {"anyOf": [{"type": "null"},{"type": "object", ...}]}
|
57
|
+
# into
|
58
|
+
# {"type": ["null", "object"], ...}
|
59
|
+
# but https://github.com/bcherny/json-schema-to-typescript has trouble converting
|
60
|
+
# from the latter format and that's an issue for us.
|
61
|
+
return { type: json_types.uniq } if remaining.empty?
|
62
|
+
end
|
63
|
+
|
64
|
+
def json_general_case
|
65
|
+
{ anyOf: choice_set.map(&:json) }
|
66
|
+
end
|
67
|
+
|
45
68
|
def simplify_integer_is_number
|
46
69
|
return unless choice_set.include?(Typed::INTEGER) && choice_set.include?(Typed::NUMBER)
|
47
70
|
AnyOf.canonicalized(choice_set - [Typed::INTEGER])
|
data/lib/babl/version.rb
CHANGED