Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/schemas/base-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ export const signatureSchema = Schema.string().case('lower').hex().length(128).l

export const subscriptionSchema = Schema.string().min(1).max(255).label('subscriptionId')

const seconds = (value: any, helpers: any) => (Number.isSafeInteger(value) && Math.log10(value) < 10) ? value : helpers.error('any.invalid')

export const createdAtSchema = Schema.number().min(0).multiple(1).custom(seconds)

// [<string>, <string> 0..*]
export const tagSchema = Schema.array()
.ordered(Schema.string().max(255).required().label('identifier'))
Expand Down
5 changes: 2 additions & 3 deletions src/schemas/event-schema.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import Schema from 'joi'

import {
createdAtSchema,
idSchema,
kindSchema,
pubkeySchema,
Expand All @@ -9,8 +10,6 @@ import {
} from './base-schema'


export const seconds = (value: any, helpers: any) => (Math.log10(value) < 10) ? value : helpers.error('any.invalid')

/**
* {
* "id": <32-bytes sha256 of the the serialized event data>
Expand All @@ -30,7 +29,7 @@ export const eventSchema = Schema.object({
// NIP-01
id: idSchema.required(),
pubkey: pubkeySchema.required(),
created_at: Schema.number().min(0).multiple(1).custom(seconds).required(),
created_at: createdAtSchema.required(),
kind: kindSchema.required(),
tags: Schema.array().items(tagSchema).required(),
content: Schema.string()
Expand Down
6 changes: 3 additions & 3 deletions src/schemas/filter-schema.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import Schema from 'joi'

import { kindSchema, prefixSchema } from './base-schema'
import { createdAtSchema, kindSchema, prefixSchema } from './base-schema'

export const filterSchema = Schema.object({
ids: Schema.array().items(prefixSchema.label('prefixOrId')).max(1000),
authors: Schema.array().items(prefixSchema.label('prefixOrAuthor')).max(1000),
kinds: Schema.array().items(kindSchema).max(20),
since: Schema.number().min(0).multiple(1),
until: Schema.number().min(0).multiple(1),
since: createdAtSchema,
until: createdAtSchema,
limit: Schema.number().min(0).multiple(1).max(10000),
}).pattern(/^#[a-z]$/, Schema.array().items(Schema.string().max(1024)).max(256))
1 change: 1 addition & 0 deletions test/unit/schemas/event-schema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ describe('NIP-01', () => {
{ message: 'is required', transform: omit(['pubkey']) },
],
created_at: [
{ message: 'contains an invalid value', transform: assocPath(['created_at'], 1672295751103) },
{ message: 'must be a number', transform: assocPath(['created_at'], null) },
{ message: 'must be greater than or equal to 0', transform: assocPath(['created_at'], -1) },
{ message: 'must be a multiple of 1', transform: assocPath(['created_at'], Math.PI) },
Expand Down
2 changes: 2 additions & 0 deletions test/unit/schemas/filter-schema.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,13 @@ describe('NIP-01', () => {
{ message: 'must be a multiple of 1', transform: assocPath(['kinds', 0], Math.PI) },
],
since: [
{ message: 'contains an invalid value', transform: assocPath(['since'], 1672295751103) },
{ message: 'must be a number', transform: assocPath(['since'], null) },
{ message: 'must be greater than or equal to 0', transform: assocPath(['since'], -1) },
{ message: 'must be a multiple of 1', transform: assocPath(['since'], Math.PI) },
],
until: [
{ message: 'contains an invalid value', transform: assocPath(['until'], 1672295751103) },
{ message: 'must be a number', transform: assocPath(['until'], null) },
{ message: 'must be greater than or equal to 0', transform: assocPath(['until'], -1) },
{ message: 'must be a multiple of 1', transform: assocPath(['until'], Math.PI) },
Expand Down