schema: use the same validators on content.
This commit is contained in:
@@ -3,6 +3,7 @@ package schema
|
|||||||
import (
|
import (
|
||||||
_ "embed"
|
_ "embed"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
"net/http"
|
"net/http"
|
||||||
@@ -14,7 +15,6 @@ import (
|
|||||||
"unsafe"
|
"unsafe"
|
||||||
|
|
||||||
"fiatjaf.com/nostr"
|
"fiatjaf.com/nostr"
|
||||||
"github.com/segmentio/encoding/json"
|
|
||||||
"gopkg.in/yaml.v3"
|
"gopkg.in/yaml.v3"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -48,7 +48,7 @@ type Schema struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type KindSchema struct {
|
type KindSchema struct {
|
||||||
Content string `yaml:"content"`
|
Content nextSpec `yaml:"content"`
|
||||||
Tags []tagSpec `yaml:"tags"`
|
Tags []tagSpec `yaml:"tags"`
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -119,6 +119,12 @@ func NewValidatorFromSchema(sch Schema) Validator {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
|
"json": func(value string, spec *nextSpec) error {
|
||||||
|
if !json.Valid(unsafe.Slice(unsafe.StringData(value), len(value))) {
|
||||||
|
return ContentError{ErrInvalidJson}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
},
|
||||||
"constrained": func(value string, spec *nextSpec) error {
|
"constrained": func(value string, spec *nextSpec) error {
|
||||||
if !slices.Contains(spec.Either, value) {
|
if !slices.Contains(spec.Either, value) {
|
||||||
return fmt.Errorf("not in allowed list")
|
return fmt.Errorf("not in allowed list")
|
||||||
@@ -141,11 +147,14 @@ func NewValidatorFromSchema(sch Schema) Validator {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
},
|
},
|
||||||
|
"numeric": func(value string, spec *nextSpec) error {
|
||||||
|
_, err := strconv.ParseUint(value, 10, 64)
|
||||||
|
return err
|
||||||
|
},
|
||||||
"imeta": func(value string, spec *nextSpec) error {
|
"imeta": func(value string, spec *nextSpec) error {
|
||||||
if len(strings.SplitN(value, " ", 2)) == 2 {
|
if len(strings.SplitN(value, " ", 2)) == 2 {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return fmt.Errorf("not a space-separated keyval")
|
return fmt.Errorf("not a space-separated keyval")
|
||||||
},
|
},
|
||||||
"free": func(value string, spec *nextSpec) error {
|
"free": func(value string, spec *nextSpec) error {
|
||||||
@@ -217,16 +226,11 @@ func (v *Validator) ValidateEvent(evt nostr.Event) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if sch, ok := v.Schema.Kinds[strconv.FormatUint(uint64(evt.Kind), 10)]; ok {
|
if sch, ok := v.Schema.Kinds[strconv.FormatUint(uint64(evt.Kind), 10)]; ok {
|
||||||
switch sch.Content {
|
if validator, ok := v.TypeValidators[sch.Content.Type]; ok {
|
||||||
case "json":
|
if err := validator(evt.Content, &sch.Content); err != nil {
|
||||||
if !json.Valid(unsafe.Slice(unsafe.StringData(evt.Content), len(evt.Content))) {
|
return ContentError{err}
|
||||||
return ContentError{ErrInvalidJson}
|
|
||||||
}
|
}
|
||||||
case "free":
|
} else {
|
||||||
if evt.Content == "" {
|
|
||||||
return ContentError{ErrEmptyValue}
|
|
||||||
}
|
|
||||||
default:
|
|
||||||
if v.FailOnUnknown {
|
if v.FailOnUnknown {
|
||||||
return ContentError{ErrUnknownContent}
|
return ContentError{ErrUnknownContent}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user