Who Am I

Hi I am Ryan a community focused engineer based in Birmingham

Hi I am Ryan a community focused engineer based in Birmingham

JSON SCHEMAS
wooo 🎉

Who here has had the pleasure
of having to write a json schema before

For the uninitiated

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "My Cool JSON Schema",
  "type": "object",
  "properties": {
    "what": {
      "type": "string"
    },
    "is": {
      "type": "number"
    },
    "a": {
      "enum": ["json", "schema"]
    },
   },
  "additionalProperties": false,
  "required": [
    "version"
  ]
}

For the uninitiated

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "My Cool JSON Schema",
  "type": "object",
  "properties": {
    "what": {
      "type": "string"
    },
    "is": {
      "type": "number"
    },
    "a": {
      "enum": ["json", "schema"]
    },
   },
  "additionalProperties": false,
  "required": [
    "version"
  ]
}
{
  "what": "foo",
  "is": 5,
  "a": "json"
}

The Mission

Use this

{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "title": "My Cool JSON Schema",
  "type": "object",
  "properties": {
    "what": {
      "type": "string"
    },
    "is": {
      "type": "number"
    },
    "a": {
      "enum": ["json", "schema"]
    },
   },
  "additionalProperties": false,
  "required": [
    "version"
  ]
}
{
  "what": "foo",
  "is": 5,
  "a": "json"
}

To Generate this

Why?

:(

How bad could it be?

Numbers

{
  "type": "integer"
}

Easy!

{
  "type": "integer"
}
4

Easy!

{
  "type": "number"
}
4.5
{
   "type": "string"
}

Seems easy enough just return a random sting!

"Helooo!"

Huh?

{
  "type": "string",
  "format": "time"
}
func generateFormat(format stringFormat, opts *GeneratorOptions) string {
	switch format {
	case formatDateTime:
		return time.Unix(faker.UnixTime(), 0).Format(time.RFC3339)
	case formatTime:
		return fmt.Sprintf("%s+00:00", time.Unix(faker.UnixTime(), 0).Format(time.TimeOnly))
	case formatDate:
		return time.Unix(faker.UnixTime(), 0).Format(time.DateOnly)
	case formatDuration:
		return fmt.Sprintf("P%dD", opts.Rand.RandomInt(0, 90))
	case formatEmail, formatIdnEmail:
		return faker.Email()
	case formatHostname, formatIdnHostname:
		return faker.DomainName()
        [...]
{
  "type": "string",
  "format": "time"
}
"123456789"
{
  "type": "string",
  "pattern": "\w+"
}

oh no...

{
  "type": "string",
  "pattern": "\w+"
}

oh no...

{
  "type": "string",
  "pattern": "^(?=.{1,256})(?=.{1,64}@.{1,255}$)(?:(?![_.-])[a-zA-Z0-9._%+-]{1,64}(?:(?:(?<!\\)[.,;:])?(?=.{0,64})[a-zA-Z0-9._%+-]{0,64})?@(?:(?!-)[a-zA-Z0-9-]{1,63}(?:(?:(?<!\\)[.,;:])?(?=.{0,255})[a-zA-Z0-9-]{0,63})?\.?)+(?:(?!-)[a-zA-Z]{2,})?|(?:[a-zA-Z0-9-]{1,63}\.[a-zA-Z]{2,}))(?=.{1,256})(?=.{1,64}@.{1,255}$)(?:(?![_.-])[a-zA-Z0-9._%+-]{1,64}(?:(?:(?<!\\)[.,;:])?(?=.{0,64})[a-zA-Z0-9._%+-]{0,64})?@(?:(?!-)[a-zA-Z0-9-]{1,63}(?:(?:(?<!\\)[.,;:])?(?=.{0,255})[a-zA-Z0-9-]{0,63})?\.?)+(?:(?!-)[a-zA-Z]{2,})?|(?:[a-zA-Z0-9-]{1,63}\.[a-zA-Z]{2,}))(?=.{1,256})(?=.{1,64}@.{1,255}$)(?:(?![_.-])[a-zA-Z0-9._%+-]{1,64}(?:(?:(?<!\\)[.,;:])?(?=.{0,64})[a-zA-Z0-9._%+-]{0,64})?@(?:(?!-)[a-zA-Z0-9-]{1,63}(?:(?:(?<!\\)[.,;:])?(?=.{0,255})[a-zA-Z0-9-]{0,63})?\.?)+(?:(?!-)[a-zA-Z]{2,})?|(?:[a-zA-Z0-9-]{1,63}\.[a-zA-Z]{2,}))(?=.{1,256})(?=.{1,64}@.{1,255}$)(?:(?![_.-])[a-zA-Z0-9._%+-]{1,64}(?:(?:(?<!\\)[.,;:])?(?=.{0,64})[a-zA-Z0-9._%+-]{0,64})?@(?:(?!-)[a-zA-Z0-9-]{1,63}(?:(?:(?<!\\)[.,;:])?(?=.{0,255})[a-zA-Z0-9-]{0,63})?\.?)+(?:(?!-)[a-zA-Z]{2,})?|(?:[a-zA-Z0-9-]{1,63}\.[a-zA-Z]{2,}))$"
}

oh no...

Go Chaff

By Rizza

Go Chaff

  • 76