Example: Basic

Let's start off with a straightforward example. The following Actor and its associated JSON Schema are performing superficial validation against the schema and returning an instance of the Model.

ExampleSchema.json
{
"title": "Model",
"description": "Example Schema for a Model",
"$id": "https://example.com/example/url/for/schema/hosting",
"type": "object",
"properties": {
"data": {
"type": "string"
}
},
"required": ["data"],
"additionalProperties": false
}
ExampleActor.js
import { Actor, Schema } from 'hive-io'
import ExampleSchema from './ExampleSchema.json'

class ExampleActor extends Actor {
async perform (_model, data) {
data.type = 'Model' // set data type
const model = await super.perform(_model, data)

return { model }
}
}

export default new Proxy(ExampleActor, {
construct: async function (ExampleActor) {
const exampleSchema = await new Schema(ExampleSchema)
return new ExampleActor(exampleSchema)
}
})
Please note the use of Proxy in the ExampleActor definition above. Due to the asynchronous requirement of Schemas, specifically that they can be hosted in a Schema Registry, Proxy is used to give us the ability to define asynchronous constructors to satisfy that requirement.

Now that we've taken a quick look at some straightforward domain logic with JSON Schemas and Actors, let's add to this with a fully functional REST service example.