Sha256: 0fbcd2b15257e277715cb10bae8aac8ab262ff4e67089fe67ecf19a97ac9d359

Contents?: true

Size: 1.54 KB

Versions: 1

Compression:

Stored size: 1.54 KB

Contents

require 'dry/schema' # TODO: Lazy load this
Dry::Schema.load_extensions(:hints)
Dry::Schema.load_extensions(:info)

module Chronicle::Schema
  class Validator
    EntitySchema = Dry::Schema.Params do
      required(:type).filled(:string, eql?: 'entities')
      required(:attributes).type(:hash)
    end

    EntityIdentifiableSchema = Dry::Schema.Params do
      required(:provider).filled(:string)
    end

    ActorEntitySchema = Dry::Schema.Params do
      required(:represents).filled(:string, eql?: 'identity')
    end

    ActivitySchema = Dry::Schema.Params do
      required(:type).filled(:string, eql?: 'activities')
      required(:attributes).schema do
        required(:verb).filled(:string)
        optional(:start_at).maybe(:string) # Adjust type as needed
        optional(:end_at).maybe(:string)   # Adjust type as needed
      end
      required(:relationships).schema do
        required(:actor).hash do
          required(:data).hash(EntitySchema) do
            required(:attributes).schema(EntityIdentifiableSchema & ActorEntitySchema)
          end
        end
      end
    end

    def self.validate(input)
      case input
      when Entity
        result = EntitySchema.call(input.to_h_jsonapi)
      when Activity
        result = ActivitySchema.call(input.to_h_jsonapi)
      else
        raise ArgumentError, "Unsupported type: #{input.class}"
      end

      if result.success?
        { success: true, data: result.to_h }
      else
        { success: false, errors: result.errors.to_h, hints: result.hints.to_h }
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
chronicle-core-0.1.0 lib/chronicle/schema/validator.rb