diff --git a/fixtures/with_custom_format.json b/fixtures/with_custom_format.json new file mode 100644 index 0000000..52fa9ed --- /dev/null +++ b/fixtures/with_custom_format.json @@ -0,0 +1,31 @@ +{ + "$schema": "https://json-schema.org/draft/2020-12/schema", + "$id": "https://github.com/invopop/jsonschema/with-custom-format", + "$ref": "#/$defs/WithCustomFormat", + "$defs": { + "WithCustomFormat": { + "properties": { + "dates": { + "items": { + "type": "string", + "format": "date" + }, + "type": "array" + }, + "odds": { + "items": { + "type": "string", + "format": "odd" + }, + "type": "array" + } + }, + "additionalProperties": false, + "type": "object", + "required": [ + "dates", + "odds" + ] + } + } +} \ No newline at end of file diff --git a/reflect.go b/reflect.go index 0df85eb..0be6cfe 100644 --- a/reflect.go +++ b/reflect.go @@ -768,10 +768,7 @@ func (t *Schema) stringKeywords(tags []string) { case "pattern": t.Pattern = val case "format": - switch val { - case "date-time", "email", "hostname", "ipv4", "ipv6", "uri", "uuid": - t.Format = val - } + t.Format = val case "readOnly": i, _ := strconv.ParseBool(val) t.ReadOnly = i diff --git a/reflect_test.go b/reflect_test.go index e4a6fa3..37ea18a 100644 --- a/reflect_test.go +++ b/reflect_test.go @@ -629,6 +629,18 @@ func TestUnsignedIntHandling(t *testing.T) { fixtureContains(t, "fixtures/unsigned_int_handling.json", `"maxItems": 0`) } +func TestJSONSchemaFormat(t *testing.T) { + type WithCustomFormat struct { + Dates []string `json:"dates" jsonschema:"format=date"` + Odds []string `json:"odds" jsonschema:"format=odd"` + } + + r := &Reflector{} + compareSchemaOutput(t, "fixtures/with_custom_format.json", r, &WithCustomFormat{}) + fixtureContains(t, "fixtures/with_custom_format.json", `"format": "date"`) + fixtureContains(t, "fixtures/with_custom_format.json", `"format": "odd"`) +} + type AliasObjectA struct { PropA string `json:"prop_a"` }