Skip to content

Commit

Permalink
Replace Joi with Yup, Add ESM Build Target
Browse files Browse the repository at this point in the history
Removed Joi to reduce overall bundle size
  • Loading branch information
Saeris committed Sep 9, 2020
1 parent c87ced1 commit 69a228a
Show file tree
Hide file tree
Showing 49 changed files with 439 additions and 539 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<h1 align="center" style="display: block; text-align: center;">📐 GraphQL Scalars</h1>
<p align="center"><a href="https://www.npmjs.org/package/@saeris/graphql-scalars"><img src="https://img.shields.io/npm/v/@saeris/graphql-scalars.svg?style=flat" alt="npm"></a><a href="https://travis-ci.com/Saeris/graphql-scalars"><img src="https://travis-ci.com/Saeris/graphql-scalars.svg?branch=master" alt="travis"></a><a href="https://codecov.io/gh/Saeris/graphql-scalars"><img src="https://codecov.io/gh/Saeris/graphql-scalars/branch/master/graph/badge.svg" alt="codecov"/></a></p>
<p align="center">A library of custom <a href="http://graphql.org/learn/schema/#scalar-types">GraphQL scalar types</a> for creating precise type-safe GraphQL schemas, with validation powered by <a href="https://github.com/hapijs/joi">Joi</a>.</p>
<p align="center">A library of custom <a href="http://graphql.org/learn/schema/#scalar-types">GraphQL scalar types</a> for creating precise type-safe GraphQL schemas, with validation powered by <a href="https://github.com/jquense/yup">Yup</a>.</p>

---

Expand Down Expand Up @@ -333,7 +333,7 @@ const { scalar: RegexScalar, resolver: Regex } = new RegularExpressionFactory({

## 📣 Acknowledgements

This library was forked from [@okgrow/graphql-scalars](https://github.com/okgrow/graphql-scalars) and uses [Joi](https://github.com/hapijs/joi) for validation.
This library was forked from [@okgrow/graphql-scalars](https://github.com/okgrow/graphql-scalars) and uses [Yup](https://github.com/jquense/yup) for validation.

## 🥂 License

Expand Down
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@
},
"dependencies": {
"@hapi/joi": "^15.0.0",
"joi-postalcode": "^1.0.1"
"joi-postalcode": "^1.0.1",
"postal-codes-js": "^2.5.1",
"uri-js": "^4.4.0",
"yup": "^0.29.3"
},
"devDependencies": {
"@babel/core": "^7.9.0",
Expand All @@ -45,11 +48,13 @@
"@pika/pack": "^0.5.0",
"@pika/plugin-build-node": "^0.9.2",
"@pika/plugin-build-types": "^0.9.2",
"@pika/plugin-build-web": "^0.9.2",
"@pika/plugin-standard-pkg": "^0.9.2",
"@types/graphql": "^14.5.0",
"@types/hapi__joi": "^17.1.4",
"@types/jest": "^26.0.10",
"@types/node": "^14.6.0",
"@types/yup": "^0.29.6",
"@typescript-eslint/eslint-plugin": "^3.10.1",
"@typescript-eslint/parser": "^3.10.1",
"apollo-server": "^2.11.0",
Expand Down Expand Up @@ -88,6 +93,9 @@
[
"@pika/plugin-build-node"
],
[
"@pika/plugin-build-web"
],
[
"@pika/plugin-build-types"
]
Expand Down
14 changes: 4 additions & 10 deletions src/dateTime.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,8 @@
import { GraphQLScalarType, GraphQLError, Kind } from "graphql"
import * as Joi from "@hapi/joi"

const validate = (value: Date) => {
Joi.assert(
value,
Joi.date().iso(),
new TypeError(`Value is not a valid Date: ${value}`)
)
return value
}
import { date as yupDate } from "yup"

const validate = (value: Date) =>
yupDate().typeError(`Value is not a valid Date: ${value}`).validateSync(value)

export const DateTimeScalar = `scalar DateTime`

Expand Down
23 changes: 8 additions & 15 deletions src/emailAddress.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
import { GraphQLScalarType, GraphQLError, Kind } from "graphql"
import * as Joi from "@hapi/joi"

const validate = (value: string) => {
Joi.assert(
value,
Joi.string(),
new TypeError(`Value is not string: ${value}`)
)
Joi.assert(
value,
Joi.string().email(),
new TypeError(`Value is not a valid email address: ${value}`)
)
return value
}
import { string as yupString } from "yup"

const validate = (value: string) =>
yupString()
.strict(true)
.typeError(`Value is not string: ${value}`)
.email(`Value is not a valid email address: ${value}`)
.validateSync(value)

export const EmailAddressScalar = `scalar EmailAddress`

Expand Down
Empty file removed src/extenions/isBbox.ts
Empty file.
Empty file removed src/extenions/isFeature.ts
Empty file.
Empty file.
Empty file removed src/extenions/isGeoJSON.ts
Empty file.
Empty file.
Empty file removed src/extenions/isGeometryObject.ts
Empty file.
Empty file removed src/extenions/isLineString.ts
Empty file.
Empty file removed src/extenions/isLineStringCoor.ts
Empty file.
Empty file removed src/extenions/isMultiLineString.ts
Empty file.
Empty file.
Empty file removed src/extenions/isMultiPoint.ts
Empty file.
Empty file removed src/extenions/isMultiPointCoor.ts
Empty file.
Empty file removed src/extenions/isMultiPolygon.ts
Empty file.
Empty file.
Empty file removed src/extenions/isPoint.ts
Empty file.
Empty file removed src/extenions/isPolygon.ts
Empty file.
Empty file removed src/extenions/isPolygonCoor.ts
Empty file.
Empty file removed src/extenions/isPosition.ts
Empty file.
23 changes: 8 additions & 15 deletions src/guid.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
import { GraphQLScalarType, GraphQLError, Kind } from "graphql"
import * as Joi from "@hapi/joi"

const validate = (value: string) => {
Joi.assert(
value,
Joi.string(),
new TypeError(`Value is not string: ${value}`)
)
Joi.assert(
value,
Joi.string().guid(),
new TypeError(`Value is not a valid GUID: ${value}`)
)
return value
}
import { string as yupString } from "yup"

const validate = (value: string) =>
yupString()
.strict(true)
.typeError(`Value is not string: ${value}`)
.uuid(`Value is not a valid GUID: ${value}`)
.validateSync(value)

export const GUIDScalar = `scalar GUID`

Expand Down
26 changes: 11 additions & 15 deletions src/hexColorCode.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import { GraphQLScalarType, GraphQLError, Kind } from "graphql"
import * as Joi from "@hapi/joi"

const validate = (value: string) => {
Joi.assert(
value,
Joi.string(),
new TypeError(`Value is not string: ${value}`)
)
Joi.assert(
value,
Joi.string().regex(/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3}|[A-Fa-f0-9]{8})$/),
new TypeError(`Value is not a valid HexColorCode: ${value}`)
)
return value
}
import { string as yupString } from "yup"

const validate = (value: string) =>
yupString()
.strict(true)
.typeError(`Value is not string: ${value}`)
.matches(
/^#([A-Fa-f0-9]{6}|[A-Fa-f0-9]{3}|[A-Fa-f0-9]{8})$/i,
`Value is not a valid HexColorCode: ${value}`
)
.validateSync(value)

export const HexColorCodeScalar = `scalar HexColorCode`

Expand Down
23 changes: 8 additions & 15 deletions src/hexadecimal.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
import { GraphQLScalarType, GraphQLError, Kind } from "graphql"
import * as Joi from "@hapi/joi"

const validate = (value: string) => {
Joi.assert(
value,
Joi.string(),
new TypeError(`Value is not string: ${value}`)
)
Joi.assert(
value,
Joi.string().hex(),
new TypeError(`Value is not a valid hexadecimal value: ${value}`)
)
return value
}
import { string as yupString } from "yup"

const validate = (value: string) =>
yupString()
.strict(true)
.typeError(`Value is not string: ${value}`)
.matches(/^[a-f0-9]+$/i, `Value is not a valid hexadecimal value: ${value}`)
.validateSync(value)

export const HexadecimalScalar = `scalar Hexadecimal`

Expand Down
28 changes: 11 additions & 17 deletions src/hsl.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
import { GraphQLScalarType, GraphQLError, Kind } from "graphql"
import * as Joi from "@hapi/joi"

const validate = (value: string) => {
Joi.assert(
value,
Joi.string(),
new TypeError(`Value is not string: ${value}`)
)
Joi.assert(
value,
Joi.string().regex(
/^hsl\(\s*(-?\d+|-?\d*.\d+)\s*,\s*(-?\d+|-?\d*.\d+)%\s*,\s*(-?\d+|-?\d*.\d+)%\s*\)$/
),
new TypeError(`Value is not a valid HSL color: ${value}`)
)
return value
}
import { string as yupString } from "yup"

const validate = (value: string) =>
yupString()
.strict(true)
.typeError(`Value is not string: ${value}`)
.matches(
/^hsl\(\s*(-?\d+|-?\d*.\d+)\s*,\s*(-?\d+|-?\d*.\d+)%\s*,\s*(-?\d+|-?\d*.\d+)%\s*\)$/i,
`Value is not a valid HSL color: ${value}`
)
.validateSync(value)

export const HSLScalar = `scalar HSL`

Expand Down
28 changes: 11 additions & 17 deletions src/hsla.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,15 @@
import { GraphQLScalarType, GraphQLError, Kind } from "graphql"
import * as Joi from "@hapi/joi"

const validate = (value: string) => {
Joi.assert(
value,
Joi.string(),
new TypeError(`Value is not string: ${value}`)
)
Joi.assert(
value,
Joi.string().regex(
/^hsla\(\s*(-?\d+|-?\d*.\d+)\s*,\s*(-?\d+|-?\d*.\d+)%\s*,\s*(-?\d+|-?\d*.\d+)%\s*,\s*(-?\d+|-?\d*.\d+)\s*\)$/
),
new TypeError(`Value is not a valid HSLA color: ${value}`)
)
return value
}
import { string as yupString } from "yup"

const validate = (value: string) =>
yupString()
.strict(true)
.typeError(`Value is not string: ${value}`)
.matches(
/^hsla\(\s*(-?\d+|-?\d*.\d+)\s*,\s*(-?\d+|-?\d*.\d+)%\s*,\s*(-?\d+|-?\d*.\d+)%\s*,\s*(-?\d+|-?\d*.\d+)\s*\)$/i,
`Value is not a valid HSLA color: ${value}`
)
.validateSync(value)

export const HSLAScalar = `scalar HSLA`

Expand Down
28 changes: 13 additions & 15 deletions src/ipv4.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
import { GraphQLScalarType, GraphQLError, Kind } from "graphql"
import * as Joi from "@hapi/joi"

const validate = (value: string) => {
Joi.assert(
value,
Joi.string(),
new TypeError(`Value is not string: ${value}`)
)
Joi.assert(
value,
Joi.string().ip({ version: `ipv4` }),
new TypeError(`Value is not a valid IPv4 address: ${value}`)
)
return value
}
import { string as yupString } from "yup"

const validate = (value: string) =>
yupString()
.strict(true)
.typeError(`Value is not string: ${value}`)
.matches(
new RegExp(
`^(?:(?:(?:0{0,2}\\d|0?[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])\\.){3}(?:0{0,2}\\d|0?[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])(?:\\/(?:\\d|[1-2]\\d|3[0-2]))?)$`
),
`Value is not a valid IPv4 address: ${value}`
)
.validateSync(value)

export const IPv4Scalar = `scalar IPv4`

Expand Down
45 changes: 30 additions & 15 deletions src/ipv6.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,34 @@
import { GraphQLScalarType, GraphQLError, Kind } from "graphql"
import * as Joi from "@hapi/joi"

const validate = (value: string) => {
Joi.assert(
value,
Joi.string(),
new TypeError(`Value is not string: ${value}`)
)
Joi.assert(
value,
Joi.string().ip({ version: `ipv6` }),
new TypeError(`Value is not a valid IPv6 address: ${value}`)
)
return value
}
import { string as yupString } from "yup"

const decOctect = `(?:0{0,2}\\d|0?[1-9]\\d|1\\d\\d|2[0-4]\\d|25[0-5])`
const ipv4address = `(?:${decOctect}\\.){3}${decOctect}`
const hexDigit = `\\dA-Fa-f`
const hexDigitOnly = `[${hexDigit}]`
const h16 = `${hexDigitOnly}{1,4}`
const ls32 = `(?:${h16}:${h16}|${ipv4address})`
const IPv6SixHex = `(?:${h16}:){6}${ls32}`
const IPv6FiveHex = `::(?:${h16}:){5}${ls32}`
const IPv6FourHex = `(?:${h16})?::(?:${h16}:){4}${ls32}`
const IPv6ThreeHex = `(?:(?:${h16}:){0,1}${h16})?::(?:${h16}:){3}${ls32}`
const IPv6TwoHex = `(?:(?:${h16}:){0,2}${h16})?::(?:${h16}:){2}${ls32}`
const IPv6OneHex = `(?:(?:${h16}:){0,3}${h16})?::${h16}:${ls32}`
const IPv6NoneHex = `(?:(?:${h16}:){0,4}${h16})?::${ls32}`
const IPv6NoneHex2 = `(?:(?:${h16}:){0,5}${h16})?::${h16}`
const IPv6NoneHex3 = `(?:(?:${h16}:){0,6}${h16})?::`
const ipv6address = `(?:${IPv6SixHex}|${IPv6FiveHex}|${IPv6FourHex}|${IPv6ThreeHex}|${IPv6TwoHex}|${IPv6OneHex}|${IPv6NoneHex}|${IPv6NoneHex2}|${IPv6NoneHex3})`

const validate = (value: string) =>
yupString()
.strict(true)
.typeError(`Value is not string: ${value}`)
.matches(
new RegExp(
`^(?:${ipv6address}(?:\\/(?:0{0,2}\\d|0?[1-9]\\d|1[01]\\d|12[0-8]))?)$`
),
`Value is not a valid IPv6 address: ${value}`
)
.validateSync(value)

export const IPv6Scalar = `scalar IPv6`

Expand Down
34 changes: 12 additions & 22 deletions src/isbn.ts
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
import { GraphQLScalarType, GraphQLError, Kind } from "graphql"
import * as Joi from "@hapi/joi"

const validate = (value: string) => {
Joi.assert(
value,
Joi.string(),
new TypeError(`Value is not string: ${value}`)
)
Joi.assert(
value,
[
Joi.string().regex(
/^(?:ISBN(?:-10)?:? *((?=\d{1,5}([ -]?)\d{1,7}\2?\d{1,6}\2?\d)(?:\d\2*){9}[\dX]))$/i
),
Joi.string().regex(
/^(?:ISBN(?:-13)?:? *(97(?:8|9)([ -]?)(?=\d{1,5}\2?\d{1,7}\2?\d{1,6}\2?\d)(?:\d\2*){9}\d))$/i
)
],
new TypeError(`Value is not a valid ISBN number: ${value}`)
)
return value
}
import { string as yupString } from "yup"

const validate = (value: string) =>
yupString()
.strict(true)
.typeError(`Value is not string: ${value}`)
.test(`isbn`, `Value is not a valid ISBN number: ${value}`, val => {
const isbn10 = /^(?:ISBN(?:-10)?:? *((?=\d{1,5}([ -]?)\d{1,7}\2?\d{1,6}\2?\d)(?:\d\2*){9}[\dX]))$/i
const isbn13 = /^(?:ISBN(?:-13)?:? *(97(?:8|9)([ -]?)(?=\d{1,5}\2?\d{1,7}\2?\d{1,6}\2?\d)(?:\d\2*){9}\d))$/i
return !(!isbn10.test(val as string) && !isbn13.test(val as string))
})
.validateSync(value)

export const ISBNScalar = `scalar ISBN`

Expand Down
Loading

0 comments on commit 69a228a

Please sign in to comment.