diff --git a/lib/typed/schema.rb b/lib/typed/schema.rb index 512dc45..44c15cf 100644 --- a/lib/typed/schema.rb +++ b/lib/typed/schema.rb @@ -17,5 +17,15 @@ def self.from_struct(struct) end ) end + + sig { params(hash: Typed::HashSerializer::InputHash).returns(Typed::Serializer::DeserializeResult) } + def from_hash(hash) + Typed::HashSerializer.new(schema: self).deserialize(hash) + end + + sig { params(json: String).returns(Typed::Serializer::DeserializeResult) } + def from_json(json) + Typed::JSONSerializer.new(schema: self).deserialize(json) + end end end diff --git a/test/typed/schema_test.rb b/test/typed/schema_test.rb index 9ab2179..af6ea89 100644 --- a/test/typed/schema_test.rb +++ b/test/typed/schema_test.rb @@ -1,8 +1,8 @@ # typed: true class SchemaTest < Minitest::Test - def test_from_struct_returns_schema - expected_schema = Typed::Schema.new( + def setup + @schema = Typed::Schema.new( fields: [ Typed::Field.new(name: :name, type: String), Typed::Field.new(name: :age, type: Integer), @@ -10,7 +10,24 @@ def test_from_struct_returns_schema ], target: Person ) + @person = Person.new(name: "Max", age: 29) + end + + def test_from_struct_returns_schema + assert_equal(@schema, Typed::Schema.from_struct(Person)) + end + + def test_from_hash_create_struct + result = @schema.from_hash({name: "Max", age: 29}) + + assert_success(result) + assert_payload(@person, result) + end + + def test_from_json_creates_struct + result = @schema.from_json('{"name": "Max", "age": 29}') - assert_equal(expected_schema, Typed::Schema.from_struct(Person)) + assert_success(result) + assert_payload(@person, result) end end