json-schema-serializer 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5b3b3c8afb8d478db134ec5536930afed58f06dc36e062f6c31ae2282fc54c6a
4
- data.tar.gz: 6e5470e5d869e5a268f1521d223d68f8596e4774594f4651b4b4c60f660eb508
3
+ metadata.gz: d1a4d5629a30b0c5aedc94644fc57e231477a4987c338487ca579ec9141cad5e
4
+ data.tar.gz: 33df5ee8f27d068a35fa1637caeea613afa650dbbcee5dd62a24945c1b06c846
5
5
  SHA512:
6
- metadata.gz: 8bb17f472b57c9f7844c6558e8f9f7fe4fd14556a363707edbe1f263f05fe404f388fd09e720db81500db62ea7d6e6f33797857f4b99f145f4ae4c5d9728d5d3
7
- data.tar.gz: c923a36cf9e887328b3d16ec62f4d0aef36a24e50679cfdc8e5b407c6780581990e7e5f6a26b3c3f8e05554ba5fbf111814cf39c0352821e5489959c85faef94
6
+ metadata.gz: ae32409a4b3d94544a1f6e2ae4a1863762d40d6e8c62e53ddb289ce3782b0ae634a59cf9d8a30c5cfd5ef6f48b6a06fa5cdbfa092229934d10939ba4ec79d723
7
+ data.tar.gz: f0585aba113213442c38167ad46864b5402ae72893b5bb72d7e18316e287f40c1c7d0881478ad328d75e984c172bab7ea3981dda0bbf46611558d01b2de55fab
data/README.md CHANGED
@@ -31,12 +31,49 @@ schema = {
31
31
  properties: {
32
32
  id: { type: "integer" },
33
33
  name: { type: "string" },
34
+ fuzzy: { type: ["string", "integer", "null"] },
34
35
  },
36
+ required: ["id"],
35
37
  }
36
38
 
37
39
  serializer = JSON::Schema::Serializer.new(schema)
38
- serializer.serialize({id: 42, name: "me", foo: "bar"})
39
- # => {"id"=>42, "name"=>"me"}
40
+
41
+ serializer.serialize({id: "42", name: "me", foo: "bar", fuzzy: "1000"})
42
+ # => {"id"=>42, "name"=>"me", "fuzzy"=>"1000"}
43
+ # "42" -> 42! type coerced!
44
+
45
+ serializer.serialize({id: "42", name: "me", fuzzy: 42})
46
+ # => {"id"=>42, "name"=>"me", "fuzzy"=>42}
47
+ serializer.serialize({id: "42", name: "me"})
48
+ # => {"id"=>42, "name"=>"me", "fuzzy"=>nil}
49
+ # multiple type auto select!
50
+
51
+ serializer.serialize({})
52
+ # => {"id"=>0, "name"=>nil, "fuzzy"=>nil}
53
+ # nil -> 0! required property's type coerced!
54
+
55
+ serializer.serialize({id: 10, name: "I don't need null keys!"}).compact
56
+ # => {"id"=>10, "name"=>"I don't need null keys!"}
57
+ # compact it!
58
+
59
+ class A
60
+ def id
61
+ 42
62
+ end
63
+ end
64
+ serializer.serialize(A.new)
65
+ # => {"id"=>42, "name"=>nil, "fuzzy"=>nil}
66
+ # method also allowed
67
+
68
+ class Schema
69
+ def type
70
+ :string
71
+ end
72
+ end
73
+ serializer2 = JSON::Schema::Serializer.new(Schema.new)
74
+ serializer2.serialize(32)
75
+ # => "32"
76
+ # non-hash schema allowed
40
77
  ```
41
78
 
42
79
  ### Caution
@@ -19,7 +19,9 @@ module JSON
19
19
 
20
20
  def walk(schema, obj, required)
21
21
  type = try_hash(schema, :type)
22
+ default = try_hash(schema, :default)
22
23
  format = try_hash(schema, :format)
24
+ obj = default if obj.nil?
23
25
  type_coerce(schema, detect_type(type, obj), format, obj, required)
24
26
  end
25
27
 
@@ -1,7 +1,7 @@
1
1
  module Json
2
2
  module Schema
3
3
  module Serializer
4
- VERSION = "1.0.0".freeze
4
+ VERSION = "1.1.0".freeze
5
5
  end
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: json-schema-serializer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Narazaka