Skip to content

Commit 5930437

Browse files
committed
nak validate
1 parent 2193491 commit 5930437

2 files changed

Lines changed: 80 additions & 0 deletions

File tree

main.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ var app = &cli.Command{
5555
syncCmd,
5656
spell,
5757
profile,
58+
validateCmd,
5859
},
5960
Version: version,
6061
Flags: []cli.Flag{

validate.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"strings"
7+
8+
"fiatjaf.com/nostr"
9+
"fiatjaf.com/nostr/schema"
10+
"github.com/urfave/cli/v3"
11+
)
12+
13+
var validateCmd = &cli.Command{
14+
Name: "validate",
15+
Usage: "validates events against the provided RoK YAML schema",
16+
Description: `takes a URL to a YAML schema in the same format as that of https://github.com/nostr-protocol/registry-of-kinds (defaults to that one) and validates the event tags and content against it, according to its kind.
17+
18+
example:
19+
nak event -k 1 -p not_a_pubkey | nak validate
20+
>> schema validation failed: tag[0][1]: invalid pubkey value 'not_a_pubkey' at tag 'p', index 1: pubkey should be 64-char hex, got 'not_a_pubkey'
21+
`,
22+
DisableSliceFlagSeparator: true,
23+
Flags: []cli.Flag{
24+
&cli.StringFlag{
25+
Name: "schema",
26+
Usage: "url to download the YAML schema from, or path to the file",
27+
Value: "https://raw.githubusercontent.com/nostr-protocol/registry-of-kinds/refs/heads/master/schema.yaml",
28+
TakesFile: true,
29+
},
30+
},
31+
Action: func(ctx context.Context, c *cli.Command) error {
32+
var validator schema.Validator
33+
34+
if schemaURL := c.String("schema"); strings.HasPrefix(schemaURL, "http") {
35+
var err error
36+
validator, err = schema.NewValidatorFromURL(schemaURL)
37+
if err != nil {
38+
return fmt.Errorf("failed to instantiate validator from '%s': %w", schemaURL, err)
39+
}
40+
} else {
41+
var err error
42+
validator, err = schema.NewValidatorFromFile(schemaURL)
43+
if err != nil {
44+
return fmt.Errorf("failed to instantiate validator from %s: %w", schemaURL, err)
45+
}
46+
}
47+
48+
handleEvent := func(stdinEvent string) error {
49+
evt := nostr.Event{}
50+
if err := json.Unmarshal([]byte(stdinEvent), &evt); err != nil {
51+
return fmt.Errorf("invalid event JSON: %w", err)
52+
}
53+
54+
if err := validator.ValidateEvent(evt); err != nil {
55+
return fmt.Errorf("schema validation failed: %w", err)
56+
}
57+
58+
return nil
59+
}
60+
61+
for stdinEvent := range getJsonsOrBlank() {
62+
if stdinEvent == "" {
63+
for _, arg := range c.Args().Slice() {
64+
if err := handleEvent(arg); err != nil {
65+
ctx = lineProcessingError(ctx, "%s", err)
66+
}
67+
}
68+
continue
69+
}
70+
71+
if err := handleEvent(stdinEvent); err != nil {
72+
ctx = lineProcessingError(ctx, "%s", err)
73+
}
74+
}
75+
76+
exitIfLineProcessingError(ctx)
77+
return nil
78+
},
79+
}

0 commit comments

Comments
 (0)