Skip to content

Commit

Permalink
Validate structure of messages and resources during creation
Browse files Browse the repository at this point in the history
  • Loading branch information
diehuxx committed Dec 14, 2023
1 parent a40c636 commit 1ef9114
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 10 deletions.
6 changes: 4 additions & 2 deletions packages/protocol/src/message-kinds/close.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class Close extends Message<'close'> {
* Creates a close message with the given options
* @param opts - options to create a close message
*/
static create(opts: CreateCloseOptions) {
static async create(opts: CreateCloseOptions) {
const metadata: MessageMetadata<'close'> = {
...opts.metadata,
kind : 'close' as const,
Expand All @@ -31,7 +31,9 @@ export class Close extends Message<'close'> {
}

const message = { metadata, data: opts.data }
return new Close(message)
const close = new Close(message)
await Message.validate(close)
return close
}

/** an explanation of why the exchange is being closed */
Expand Down
7 changes: 5 additions & 2 deletions packages/protocol/src/message-kinds/order-status.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import type { MessageKind, MessageKindModel, MessageMetadata } from '../types.js'
import { Message } from '../message.js'
import { order } from '../../generated/compiled-validators.js'

/**
* Options passed to {@link OrderStatus.create}
Expand All @@ -23,7 +24,7 @@ export class OrderStatus extends Message<'orderstatus'> {
* Creates an order status with the given options
* @param opts - options to create an order status
*/
static create(opts: CreateOrderStatusOptions) {
static async create(opts: CreateOrderStatusOptions) {
const metadata: MessageMetadata<'orderstatus'> = {
...opts.metadata,
kind : 'orderstatus' as const,
Expand All @@ -32,7 +33,9 @@ export class OrderStatus extends Message<'orderstatus'> {
}

const message = { metadata, data: opts.data }
return new OrderStatus(message)
const orderStatus = new OrderStatus(message)
await Message.validate(order)
return orderStatus
}

/** Current status of Order that's being executed (e.g. PROCESSING, COMPLETED, FAILED etc.) */
Expand Down
6 changes: 4 additions & 2 deletions packages/protocol/src/message-kinds/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export class Order extends Message<'order'> {
* Creates an order with the given options
* @param opts - options to create an order
*/
static create(opts: CreateOrderOptions) {
static async create(opts: CreateOrderOptions) {
const metadata: MessageMetadata<'order'> = {
...opts.metadata,
kind : 'order' as const,
Expand All @@ -31,6 +31,8 @@ export class Order extends Message<'order'> {
}

const message = { metadata, data: {} }
return new Order(message)
const order = new Order(message)
await Message.validate(order)
return order
}
}
6 changes: 4 additions & 2 deletions packages/protocol/src/message-kinds/quote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export class Quote extends Message<'quote'> {
* Creates a quote message with the given options
* @param opts - options to create a quote
*/
static create(opts: CreateQuoteOptions) {
static async create(opts: CreateQuoteOptions) {
const metadata: MessageMetadata<'quote'> = {
...opts.metadata,
kind : 'quote' as const,
Expand All @@ -32,7 +32,9 @@ export class Quote extends Message<'quote'> {
}

const message = { metadata, data: opts.data }
return new Quote(message)
const quote = new Quote(message)
await Message.validate(quote)
return quote
}

/** When this quote expires. Expressed as ISO8601 */
Expand Down
6 changes: 4 additions & 2 deletions packages/protocol/src/message-kinds/rfq.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export class Rfq extends Message<'rfq'> {
* @param opts - options to create an rfq
* @returns {@link Rfq}
*/
static create(opts: CreateRfqOptions) {
static async create(opts: CreateRfqOptions) {
const id = Message.generateId('rfq')
const metadata: MessageMetadata<'rfq'> = {
...opts.metadata,
Expand All @@ -44,7 +44,9 @@ export class Rfq extends Message<'rfq'> {
// TODO: hash `data.payoutMethod.paymentDetails` and set `private`

const message = { metadata, data: opts.data }
return new Rfq(message)
const rfq = new Rfq(message)
await Message.validate(rfq)
return rfq
}

/**
Expand Down

0 comments on commit 1ef9114

Please sign in to comment.