All needed mongoose types in one library
MIT License
This mongoose types library is a port of the awesome graphql-scalars libraries:
Currency
EmailAddress
GUID
HSL
HSLA
HexColorCode
Hexadecimal
IBAN
Int32
IPv4
IPv6
ISBN
MAC
PhoneNumber
Port
PostalCode
RGB
RGBA
Url
UtcOffset
npm install --save mongoose-schema-types
or
yarn add mongoose-schema-types
import mongoose from 'mongoose';
import 'mongoose-schema-types';
const schema = new mongoose.Schema({
currency: { type: mongoose.Schema.Types.Currency },
email: { type: mongoose.Schema.Types.EmailAddress },
guid: { type: mongoose.Schema.Types.GUID },
hsl: { type: mongoose.Schema.Types.HSL },
hsla: { type: mongoose.Schema.Types.HSLA },
hexcolorcode: { type: mongoose.Schema.Types.HexColorCode },
hexadecimal: { type: mongoose.Schema.Types.Hexadecimal },
iban: { type: mongoose.Schema.Types.IBAN },
int32: { type: mongoose.Schema.Types.Int32 },
ipv4: { type: mongoose.Schema.Types.IPv4 },
ipv6: { type: mongoose.Schema.Types.IPv6 },
isbn: { type: mongoose.Schema.Types.ISBN },
mac: { type: mongoose.Schema.Types.MAC },
phonenumber: { type: mongoose.Schema.Types.PhoneNumber },
port: { type: mongoose.Schema.Types.Port },
postalcode: { type: mongoose.Schema.Types.PostalCode },
rgb: { type: mongoose.Schema.Types.RGB },
rgba: { type: mongoose.Schema.Types.RGBA },
url: { type: mongoose.Schema.Types.Url },
utcoffset: { type: mongoose.Schema.Types.UtcOffset }
});
Many mongoose type plugins exist on npm, but most of them only handle one type. And some of them are not linked to any git repo.
A field whose value is a ISO-4217 currency (case insensitive).
String that will have a value of format ±hh:mm. List of tz database time zones
.
A field whose value conforms to the standard internet email address format as specified in RFC822.
A field whose value conforms to the standard URL format as specified in
RFC3986, and it uses real JavaScript URL
objects.
A field whose value conforms to the standard E.164 format as specified in
E.164 specification. Basically this is +17895551234
.
The very powerful
libphonenumber
library is available to take
that format, parse and display it in whatever display format you want. It can also be used to
parse user input and get the E.164 format to pass into a schema.
We're going to start with a limited set as suggested here and here.
Which gives us the following countries:
This is really a practical decision of weight (of the package) vs. completeness.
In the future we might expand this list and use the more comprehensive list found here.
A field whose value is a generic Globally Unique Identifier.
A field whose value is a hexadecimal.
A field whose value is a int32. See also wikipedia.
A field whose value is a hex color code.
A field whose value is a CSS HSL color.
A field whose value is a IPv4 address.
A field whose value is a IPv6 address.
A field whose value is a ISBN-10 or ISBN-13 number.
A field whose value is a IEEE 802 48-bit MAC address.
A field whose value is a valid TCP port within the range of 0 to 65535.
A field whose value is a CSS RGB color.
A field whose value is a CSS RGBA color.
Includes IBAN specifications for the following countries:
Released under the MIT license.
Issues and Pull Requests are always welcome.
Thanks to the awesome graphql-scalars library for all the hard work done. All validators and associated documentation come from this lib.