Golang support for string formats defined by JSON Schema and OpenAPI.
import _ "github.com/go-openapi/strfmt/enable/mongodb". This will switch the behavior to the actual driver, which remains regularly updated as an independent module.API is stable.
go get github.com/go-openapi/strfmt
This package exposes a registry of data types to support string formats in the go-openapi toolkit.
strfmt represents a well known string format such as hostname or email.
This package provides a few extra formats such as credit card (US), color, etc.
Format types can serialize and deserialize JSON or from a SQL database.
BSON is also supported (MongoDB).
go-openapi/strfmt follows the swagger 2.0 specification with the following formats defined here.
It also provides convenient extensions to go-openapi users.
NOTE: as the name stands for, this package is intended to support string formatting only. It does not provide validation for numerical values with swagger format extension for JSON types “number” or “integer” (e.g. float, double, int32...).
All types defined here are stringers and may be converted to strings with .String(). Note that most types defined by this package may be converted directly to string like string(Email{}).
Date and DateTime may be converted directly to time.Time like time.Time(Time{}). Similarly, you can convert Duration to time.Duration as in time.Duration(Duration{})
The conv subpackage provides helpers to convert the types to and from pointers, just like go-openapi/swag does with primitive types.
Types defined in strfmt expose marshaling and validation capabilities.
List of defined types:
All format types implement the database/sql interfaces sql.Scanner and driver.Valuer, so they work out of the box with Go's standard database/sql package and any SQL driver.
All format types also implement BSON marshaling/unmarshaling for use with MongoDB. By default, a built-in minimal codec is used (compatible with mongo-driver v2.5.0). For full driver support, add import _ "github.com/go-openapi/strfmt/enable/mongodb".
MySQL / MariaDB caveat for
DateTime: Thego-sql-driver/mysqldriver has hard-coded handling fortime.Timebut does not intercept type redefinitions likestrfmt.DateTime. As a result,DateTime.Value()sends an RFC 3339 string (e.g."2024-06-15T12:30:45.123Z") that MySQL/MariaDB rejects forDATETIMEcolumns.Workaround: set
strfmt.MarshalFormatto a MySQL-compatible format such asstrfmt.ISO8601LocalTimeand normalize to UTC before marshaling:strfmt.MarshalFormat = strfmt.ISO8601LocalTime strfmt.NormalizeTimeForMarshal = func(t time.Time) time.Time { return t.UTC() }See #174 for details.
Integration tests for MongoDB, MariaDB, and PostgreSQL run in CI to verify database roundtrip compatibility for all format types. See internal/testintegration/.
See https://github.com/go-openapi/strfmt/releases
https://github.com/OAI/OpenAPI-Specification/blob/main/versions/2.0.md
This library ships under the SPDX-License-Identifier: Apache-2.0.
Maintainers can cut a new release by either: