Sha256: f4d79629635ec2fcd5e7150d2f8eb75044a3fb5d1548acedd8f30c04ed228a57

Contents?: true

Size: 925 Bytes

Versions: 2

Compression:

Stored size: 925 Bytes

Contents

# typed: strict

module Typed
  class Schema < T::Struct
    extend T::Sig
    include ActsAsComparable

    const :fields, T::Array[Field], default: []
    const :target, T.class_of(T::Struct)

    sig { params(struct: T.class_of(T::Struct)).returns(Typed::Schema) }
    def self.from_struct(struct)
      Typed::Schema.new(
        target: struct,
        fields: struct.props.map do |name, properties|
          Typed::Field.new(name: name, type: properties[:type], required: !properties[:fully_optional])
        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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
sorbet-schema-0.4.1 lib/typed/schema.rb
sorbet-schema-0.4.0 lib/typed/schema.rb