schema: expose Kind in kind schema struct, NewSchemaFromBytes() function.
This commit is contained in:
+40
-22
@@ -39,20 +39,16 @@ func FetchSchemaFromURL(schemaURL string) (Schema, error) {
|
|||||||
return Schema{}, fmt.Errorf("failed to read schema response: %w", err)
|
return Schema{}, fmt.Errorf("failed to read schema response: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
var schema Schema
|
return NewSchemaFromBytes(body)
|
||||||
if err := yaml.Unmarshal(body, &schema); err != nil {
|
|
||||||
return Schema{}, fmt.Errorf("failed to parse schema: %w", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
return schema, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Schema struct {
|
type Schema struct {
|
||||||
GenericTags map[string]ContentSpec `yaml:"generic_tags" json:"generic_tags,omitempty"`
|
GenericTags map[string]ContentSpec `yaml:"generic_tags" json:"generic_tags,omitempty"`
|
||||||
Kinds map[string]KindSchema `yaml:"kinds" json:"kinds,omitempty"`
|
Kinds map[string]*KindSchema `yaml:"kinds" json:"kinds,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type KindSchema struct {
|
type KindSchema struct {
|
||||||
|
Kind nostr.Kind `yaml:"-" json:"kind"`
|
||||||
Description string `yaml:"description" json:"description,omitempty"`
|
Description string `yaml:"description" json:"description,omitempty"`
|
||||||
InUse bool `yaml:"in_use" json:"in_use,omitempty"`
|
InUse bool `yaml:"in_use" json:"in_use,omitempty"`
|
||||||
Content ContentSpec `yaml:"content" json:"content,omitempty"`
|
Content ContentSpec `yaml:"content" json:"content,omitempty"`
|
||||||
@@ -78,25 +74,39 @@ type ContentSpec struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type Validator struct {
|
type Validator struct {
|
||||||
Schema Schema `json:"schema,omitempty"`
|
Schema Schema `json:"schema,omitempty"`
|
||||||
FailOnUnknownKind bool `json:"fail_on_unknown_kind,omitempty"`
|
FailOnUnknownKind bool `json:"fail_on_unknown_kind,omitempty"`
|
||||||
FailOnUnknownType bool `json:"fail_on_unknown_type,omitempty"`
|
FailOnUnknownType bool `json:"fail_on_unknown_type,omitempty"`
|
||||||
TypeValidators map[string]func(value string, spec *ContentSpec) error `json:"type_validators,omitempty"`
|
TypeValidators map[string]func(value string, spec *ContentSpec) error `json:"type_validators,omitempty"`
|
||||||
UnknownTypes []string `json:"unknown_types,omitempty"`
|
UnknownTypes []string `json:"unknown_types,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewValidatorFromBytes(schemaData []byte) (Validator, error) {
|
func NewValidatorFromBytes(schemaData []byte) (Validator, error) {
|
||||||
schema := Schema{
|
schema, err := NewSchemaFromBytes(schemaData)
|
||||||
GenericTags: make(map[string]ContentSpec),
|
if err != nil {
|
||||||
Kinds: make(map[string]KindSchema),
|
return Validator{}, err
|
||||||
}
|
|
||||||
if err := yaml.Unmarshal(schemaData, &schema); err != nil {
|
|
||||||
return Validator{}, fmt.Errorf("failed to parse schema: %w", err)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return NewValidatorFromSchema(schema), nil
|
return NewValidatorFromSchema(schema), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewSchemaFromBytes(schemaData []byte) (Schema, error) {
|
||||||
|
schema := Schema{
|
||||||
|
GenericTags: make(map[string]ContentSpec),
|
||||||
|
Kinds: make(map[string]*KindSchema),
|
||||||
|
}
|
||||||
|
if err := yaml.Unmarshal(schemaData, &schema); err != nil {
|
||||||
|
return Schema{}, fmt.Errorf("failed to parse schema: %w", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
for k := range schema.Kinds {
|
||||||
|
kn, _ := strconv.ParseUint(k, 10, 16)
|
||||||
|
schema.Kinds[k].Kind = nostr.Kind(kn)
|
||||||
|
}
|
||||||
|
|
||||||
|
return schema, nil
|
||||||
|
}
|
||||||
|
|
||||||
func NewValidatorFromSchema(sch Schema) Validator {
|
func NewValidatorFromSchema(sch Schema) Validator {
|
||||||
validator := Validator{
|
validator := Validator{
|
||||||
Schema: sch,
|
Schema: sch,
|
||||||
@@ -185,11 +195,19 @@ func NewValidatorFromSchema(sch Schema) Validator {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func NewValidatorFromFile(filename string) (Validator, error) {
|
func NewValidatorFromFile(filename string) (Validator, error) {
|
||||||
|
schema, err := NewSchemaFromFile(filename)
|
||||||
|
if err != nil {
|
||||||
|
return Validator{}, err
|
||||||
|
}
|
||||||
|
return NewValidatorFromSchema(schema), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func NewSchemaFromFile(filename string) (Schema, error) {
|
||||||
data, err := os.ReadFile(filename)
|
data, err := os.ReadFile(filename)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return Validator{}, fmt.Errorf("failed to read schema file: %w", err)
|
return Schema{}, fmt.Errorf("failed to read schema file: %w", err)
|
||||||
}
|
}
|
||||||
return NewValidatorFromBytes(data)
|
return NewSchemaFromBytes(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewValidatorFromURL(schemaURL string) (Validator, error) {
|
func NewValidatorFromURL(schemaURL string) (Validator, error) {
|
||||||
@@ -228,9 +246,9 @@ func (ce ContentError) Error() string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
type TagError struct {
|
type TagError struct {
|
||||||
Tag int `json:"tag,omitempty"`
|
Tag int `json:"tag,omitempty"`
|
||||||
Item int `json:"item,omitempty"`
|
Item int `json:"item,omitempty"`
|
||||||
Err error `json:"err,omitempty"`
|
Err error `json:"err,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (te TagError) Error() string {
|
func (te TagError) Error() string {
|
||||||
|
|||||||
Reference in New Issue
Block a user