Sha256: 1d2f201cae31f3291ded7de560bf03e8db80f95e499b4464153f3c15f9e96a8e

Contents?: true

Size: 1.66 KB

Versions: 3

Compression:

Stored size: 1.66 KB

Contents

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

require 'chronicle/serialization'

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(record)
      case record
      when Entity
        result = EntitySchema.call(Chronicle::Serialization::JSONAPISerializer.serialize(record))
      when Activity
        result = ActivitySchema.call(Chronicle::Serialization::JSONAPISerializer.serialize(record))
      else
        raise ArgumentError, "Unsupported type: #{record.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

3 entries across 3 versions & 1 rubygems

Version Path
chronicle-core-0.2.2 lib/chronicle/schema/validator.rb
chronicle-core-0.2.1 lib/chronicle/schema/validator.rb
chronicle-core-0.2.0 lib/chronicle/schema/validator.rb