Skip to content

Commit

Permalink
add some schema for common nips, global tags, etc
Browse files Browse the repository at this point in the history
  • Loading branch information
dskvr committed Jan 8, 2025
1 parent 7a14cdc commit 9932f0b
Show file tree
Hide file tree
Showing 35 changed files with 316 additions and 14 deletions.
97 changes: 97 additions & 0 deletions libraries/schemata-js-ajv/build.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
import esbuild from 'esbuild';
import { polyfillNode } from 'esbuild-plugin-polyfill-node';
import fs from 'fs';
import path from 'path';
import { promises as fsp } from 'fs';

const isWatchMode = process.argv.includes('--watch');

const plugins = [];

const browserConfig = {
entryPoints: ['src/index.ts'],
bundle: true,
outdir: 'dist/web',
platform: 'browser',
format: 'esm',
allowOverwrite: true,
external: ['src/nips/*/index.js', 'ajv'],
plugins: [
polyfillNode({
globals: { process: true, Buffer: true, global: true },
}),
],
};

const serverConfig = {
entryPoints: ['src/index.ts'],
bundle: true,
outdir: 'dist/server',
platform: 'node',
format: 'esm',
allowOverwrite: true,
plugins: [
...plugins,
],
};

async function cleanDist() {
const distWebPath = path.resolve('./dist/web');
const distServerPath = path.resolve('./dist/server');
try {
if (fs.existsSync(distWebPath)) {
console.log('Clearing dist/web directory...');
await fsp.rm(distWebPath, { recursive: true, force: true });
console.log('dist/web directory cleared.');
}
if (fs.existsSync(distServerPath)) {
console.log('Clearing dist/server directory...');
await fsp.rm(distServerPath, { recursive: true, force: true });
console.log('dist/server directory cleared.');
}
} catch (error) {
console.error('Error clearing dist directories:', error);
throw error;
}
}

async function build() {
try {
await cleanDist();

if (isWatchMode) {
const browserContext = await esbuild.context(browserConfig);
const serverContext = await esbuild.context(serverConfig);

await Promise.all([
browserContext.watch(),
serverContext.watch(),
]);

console.log("Watching for changes in src...");
watchSrcDirectory();
} else {
await esbuild.build(browserConfig);
console.log("Browser build completed.");

await esbuild.build(serverConfig);
console.log("Server build completed.");

console.log("Build complete for both web and server targets.");
}
} catch (error) {
console.error("Build failed:", error);
process.exit(1);
}
}


function watchSrcDirectory() {
fs.watch('./src', { recursive: true }, (eventType, filename) => {
if (filename && (filename.endsWith('.ts') || filename.endsWith('.json'))) {
build().then(() => dereferenceSchemas());
}
});
}

build();
2 changes: 2 additions & 0 deletions libraries/schemata-js-ajv/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
"dependencies": {
"@nostrwatch/schemata": "workspace:^",
"@nostrwatch/utils": "workspace:^",
"@types/ajv-errors": "^2.0.5",
"ajv-errors": "^3.0.0",
"nostr-tools": "^2.10.4"
},
"peerDependencies": {
Expand Down
5 changes: 4 additions & 1 deletion libraries/schemata-js-ajv/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { Ajv, type ErrorObject } from 'ajv';
import addErrors from 'ajv-errors'

import * as NostrSchemata from '@nostrwatch/schemata'
import { type NostrEvent } from 'nostr-tools'

const ajv = new Ajv();
const ajv = new Ajv({ allErrors: true });
addErrors(ajv);

type NostrSchemataType = typeof NostrSchemata;

Expand Down
4 changes: 4 additions & 0 deletions libraries/schemata-js-ajv/src/types/ajv-errors.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
declare module "ajv-errors" {
const addErrors: (ajvInstance: any, options?: any) => void;
export default addErrors;
}
2 changes: 2 additions & 0 deletions libraries/schemata/@/tag/_K.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
allOf:
- $ref: "nips/nip-22/tag/_K/schema.yaml"
2 changes: 2 additions & 0 deletions libraries/schemata/@/tag/k.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
allOf:
- $ref: "nips/nip-18/tag/k/schema.yaml"
2 changes: 2 additions & 0 deletions libraries/schemata/@/tag/r.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
allOf:
- $ref: "nips/nip-65/tag/r/schema.yaml"
50 changes: 50 additions & 0 deletions libraries/schemata/nips/nip-01/kind-0/schema.content.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
$id: "https://schemata.nostr.watch/note/kind/0/content"
$schema: "https://json-schema.org/draft/2020-12/schema"
title: "Metadata Event Schema"
type: "object"
properties:
name:
type: "string"
minLength: 1
about:
type: "string"
picture:
type: "string"
format: "uri"
display_name:
type: "string"
description: "An alternative, richer name. 'name' must always be set."
website:
type: "string"
format: "uri"
description: "A web URL related to the event author."
banner:
type: "string"
format: "uri"
description: "A URL to a wide picture for background display."
bot:
type: "boolean"
description: "Indicates if content is automated."
lud16:
type: "string"
format: "email"
lud06:
type: "string"

# deprecations
displayName:
deprecated: true
description: "Use 'display_name' instead."
errorMessage: {
deprecated: "Use 'display_name' instead."
}
username:
deprecated: true
description: "Use 'name' instead."
errorMessage: {
deprecated: "Use 'display_name' instead."
}

required:
- "name"
additionalProperties: true
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
$id: 'https://schemata.nostr.watch/note/kind/3'
$id: "https://schemata.nostr.watch/note/kind/0"
$schema: http://json-schema.org/draft-07/schema#
title: kind3
title: kind0
allOf:
- $ref: "@/note.yaml"
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
$id: 'https://schemata.nostr.watch/message/client-close'
$id: 'https://schemata.nostr.watch/message/client/close'
type: "array"
items:
- const: "CLOSE"
- type: "string"
description: "an id of the subscription to close"
errorMessage: "subscription id must be a string and should be unique."
errorMessage: {
type: "subscription id must be a string and should be unique."
}
minItems: 2
maxItems: 2
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
$id: 'https://schemata.nostr.watch/message/client-event'
$id: 'https://schemata.nostr.watch/message/client/event'
type: "array"
title: "Client Event Message"
description: "A message for publishing a NIP-01 note to a relay"
errorMessage: "client-event must be an array of two elements: the string 'EVENT' and a NIP-01 note"
items:
- const: "EVENT"
- allOf:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
$id: 'https://schemata.nostr.watch/message/client-req'
$id: 'https://schemata.nostr.watch/message/client/req'
type: "array"
items:
- const: "REQ"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
$id: 'https://schemata.nostr.watch/message/relay-closed'
$id: 'https://schemata.nostr.watch/message/relay/closed'
type: "array"
items:
- const: "CLOSED"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
$id: 'https://schemata.nostr.watch/message/relay-eose'
$id: 'https://schemata.nostr.watch/message/relay/eose'
type: "array"
description: "End Of Subscription Event"
items:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
$id: 'https://schemata.nostr.watch/message/relay-event'
$id: 'https://schemata.nostr.watch/message/relay/event'
type: "array"
description: "A message sent by relays to clients in response to a client request."
items:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
---
$id: 'https://schemata.nostr.watch/message/relay/notice'
type: "array"
description: "A message sent by relays to clients in response to a client request, usually to inform them of an issue."
items:
Expand Down
31 changes: 31 additions & 0 deletions libraries/schemata/nips/nip-01/note-unsigned/schema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
$id: "https://schemata.nostr.watch/note"
$schema: http://json-schema.org/draft-07/schema
type: object
properties:
content:
type: string
errorMessage: "content must be a string"
description: "The content of the note"
created_at:
type: integer
errorMessage: "created_at must be a timestamp expressed in seconds (not milliseconds)"
description: "The timestamp of the note creation"
kind:
type: integer
pubkey:
allOf:
- $ref: "@/secp256k1.yaml"
errorMessage: "pubkey must be a secp256k1 public key"
description: "The public key of the note's author"
tags:
type: array
errorMessage: "tags must be an array of strings"
description: "The tags of the note"
items:
- $ref: "@/tag.yaml"

required:
- content
- created_at
- kind
- tags
2 changes: 1 addition & 1 deletion libraries/schemata/nips/nip-01/tag/a/schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,4 @@ allOf:
pattern: '^\d+:[a-f0-9]{64}:.+$'
- type: string
pattern: '^(ws://|wss://).+$'
additionalItems: false
additionalItems: true
2 changes: 2 additions & 0 deletions libraries/schemata/nips/nip-01/tag/e/schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ allOf:
items:
- const: "p"
- $ref: "@/secp256k1.yaml"
- type: string
pattern: '^(ws://|wss://).+$'
additionalItems: true
4 changes: 3 additions & 1 deletion libraries/schemata/nips/nip-01/tag/p/schema.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,6 @@ allOf:
items:
- const: "p"
- $ref: "@/secp256k1.yaml"
additionalItems: true
- title: 'petname'
type: string
additionalItems: false
16 changes: 16 additions & 0 deletions libraries/schemata/nips/nip-02/kind-3/schema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
$id: 'https://schemata.nostr.watch/note/kind/3'
$schema: http://json-schema.org/draft-07/schema#
title: kind3
allOf:
- $ref: "@/note.yaml"
- type: object
properties:
tags:
type: array
items:
$ref: "@/tag/p.yaml"
additionalItems: false
minItems: 1
errorMessage:
type: "tags must be an array of p tags and only p tags"
minItems: "tags array must have at least one p tag"
11 changes: 11 additions & 0 deletions libraries/schemata/nips/nip-18/tag/k/schema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
$id: 'https://schemata.nostr.watch/note/tag/k'
$schema: "http://json-schema.org/draft-07/schema#"
allOf:
- $ref: "@/tag.yaml"
- type: array
minItems: 2
items:
- const: "k"
- type: string
pattern: '^\d+$'
additionalItems: false
5 changes: 5 additions & 0 deletions libraries/schemata/nips/nip-22/kind-1111/schema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
$id: "https://schemata.nostr.watch/note/kind/1111"
$schema: http://json-schema.org/draft-07/schema#
title: kind1111
allOf:
- $ref: "@/note.yaml"
4 changes: 4 additions & 0 deletions libraries/schemata/nips/nip-22/tag/_A/schema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
$id: 'https://schemata.nostr.watch/note/tag/A'
$schema: "http://json-schema.org/draft-07/schema#"
allOf:
- $ref: "@/tag/a.yaml"
4 changes: 4 additions & 0 deletions libraries/schemata/nips/nip-22/tag/_E/schema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
$id: 'https://schemata.nostr.watch/note/tag/E'
$schema: "http://json-schema.org/draft-07/schema#"
allOf:
- $ref: "@/tag/e.yaml"
4 changes: 4 additions & 0 deletions libraries/schemata/nips/nip-22/tag/_K/schema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
$id: 'https://schemata.nostr.watch/note/tag/K'
$schema: "http://json-schema.org/draft-07/schema#"
allOf:
- $ref: "@/tag/k.yaml"
4 changes: 4 additions & 0 deletions libraries/schemata/nips/nip-22/tag/_P/schema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
$id: 'https://schemata.nostr.watch/note/tag/P'
$schema: "http://json-schema.org/draft-07/schema#"
allOf:
- $ref: "@/tag/p.yaml"
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
$id: 'https://schemata.nostr.watch/message/client/auth'
type: "array"
items:
- const: "AUTH"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
$id: 'https://schemata.nostr.watch/message/relay/auth'
type: "array"
items:
- const: "AUTH"
Expand Down
18 changes: 18 additions & 0 deletions libraries/schemata/nips/nip-65/kind-10002/schema.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
$id: 'https://schemata.nostr.watch/note/kind/3'
$schema: http://json-schema.org/draft-07/schema#
title: kind10002
allOf:
- $ref: "@/note.yaml"
- type: object
properties:
tags:
type: array
items:
$ref: "@/tag/r.yaml"
additionalItems: false
minItems: 1
errorMessage:
type: "tags must be an array of r tags"
minItems: "tags array must have at least one r tag"
required:
- tags
Loading

0 comments on commit 9932f0b

Please sign in to comment.