Skip to content

Commit

Permalink
add validations
Browse files Browse the repository at this point in the history
  • Loading branch information
blaine-arcjet committed Sep 5, 2024
1 parent a6a60d5 commit b969a4e
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 1 deletion.
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
// }

declare const crawlerUserAgents: {
id: string
pattern: string
addition_date?: string
url?: string
Expand Down
9 changes: 9 additions & 0 deletions validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ var crawlersJson []byte

// Crawler contains information about one crawler.
type Crawler struct {
// An identifier for the crawler.
Id string `json:"id"`

// Regexp of User Agent of the crawler.
Pattern string `json:"pattern"`

Expand All @@ -28,6 +31,7 @@ type Crawler struct {

// Private time needed to convert addition_date from/to the format used in JSON.
type jsonCrawler struct {
Id string `json:"id"`
Pattern string `json:"pattern"`
AdditionDate string `json:"addition_date"`
URL string `json:"url"`
Expand All @@ -52,10 +56,15 @@ func (c *Crawler) UnmarshalJSON(b []byte) error {
return err
}

c.Id = jc.Id
c.Pattern = jc.Pattern
c.URL = jc.URL
c.Instances = jc.Instances

if c.Id == "" {
return fmt.Errorf("empty id in record %s", string(b))
}

if c.Pattern == "" {
return fmt.Errorf("empty pattern in record %s", string(b))
}
Expand Down
3 changes: 2 additions & 1 deletion validate.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,15 @@
"items": {
"type": "object",
"properties": {
"id": {"type": "string"}, # required
"pattern": {"type": "string"}, # required
"instances": {"type": "array"}, # required
"url": {"type": "string"}, # optional
"description": {"type": "string"}, # optional
"addition_date": {"type": "string"}, # optional
"depends_on": {"type": "array"} # allows an instance to match twice
},
"required": ["pattern", "instances"]
"required": ["id", "pattern", "instances"]
}
}

Expand Down

0 comments on commit b969a4e

Please sign in to comment.