UNPKG

@saeris/graphql-scalars

Version:

A collection of scalar types using Joi for validation

846 lines (645 loc) 29.6 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); function _interopDefault (ex) { return (ex && (typeof ex === 'object') && 'default' in ex) ? ex['default'] : ex; } var graphql = require('graphql'); var yup = require('yup'); var postalCodes = _interopDefault(require('postal-codes-js')); var uriJs = require('uri-js'); const validate = value => yup.date().typeError(`Value is not a valid Date: ${value}`).validateSync(value); const DateTimeScalar = `scalar DateTime`; const DateTime = new graphql.GraphQLScalarType({ name: `DateTime`, description: `Use JavaScript Date object for date/time fields.`, serialize(value) { const date = typeof value === `string` ? new Date(Date.parse(value)) : value; validate(date); return date.toJSON(); }, parseValue(value) { const date = new Date(value); return validate(date); }, parseLiteral(ast) { if (ast.kind !== graphql.Kind.STRING) { throw new graphql.GraphQLError(`Can only parse strings to dates but got a: ${ast.kind}`); } const date = new Date(ast.value); if (ast.value !== date.toJSON()) { throw new graphql.GraphQLError(`Value is not a valid Date format (YYYY-MM-DDTHH:MM:SS.SSSZ): ${ast.value}`); } return validate(date); } }); const validate$1 = value => yup.string().strict(true).typeError(`Value is not string: ${value}`).email(`Value is not a valid email address: ${value}`).validateSync(value); const EmailAddressScalar = `scalar EmailAddress`; const EmailAddress = new graphql.GraphQLScalarType({ name: `EmailAddress`, description: `A field whose value conforms to the standard internet email address format as specified in RFC822: https://www.w3.org/Protocols/rfc822/.`, serialize(value) { return validate$1(value); }, parseValue(value) { return validate$1(value); }, parseLiteral(ast) { if (ast.kind !== graphql.Kind.STRING) { throw new graphql.GraphQLError(`Can only validate strings as email addresses but got a: ${ast.kind}`); } return validate$1(ast.value); } }); const validate$2 = value => yup.string().strict(true).typeError(`Value is not string: ${value}`).uuid(`Value is not a valid GUID: ${value}`).validateSync(value); const GUIDScalar = `scalar GUID`; const GUID = new graphql.GraphQLScalarType({ name: `GUID`, description: `A field whose value is a generic Globally Unique Identifier: https://en.wikipedia.org/wiki/Universally_unique_identifier.`, serialize(value) { return validate$2(value); }, parseValue(value) { return validate$2(value); }, parseLiteral(ast) { if (ast.kind !== graphql.Kind.STRING) { throw new graphql.GraphQLError(`Can only validate strings as GUIDs but got a: ${ast.kind}`); } return validate$2(ast.value); } }); const validate$3 = value => yup.string().strict(true).typeError(`Value is not string: ${value}`).matches(/^[a-f0-9]+$/i, `Value is not a valid hexadecimal value: ${value}`).validateSync(value); const HexadecimalScalar = `scalar Hexadecimal`; const Hexadecimal = new graphql.GraphQLScalarType({ name: `Hexadecimal`, description: `A field whose value is a hexadecimal: https://en.wikipedia.org/wiki/Hexadecimal.`, serialize(value) { return validate$3(value); }, parseValue(value) { return validate$3(value); }, parseLiteral(ast) { if (ast.kind !== graphql.Kind.STRING) { throw new graphql.GraphQLError(`Can only validate strings as a hexadecimal but got a: ${ast.kind}`); } return validate$3(ast.value); } }); const validate$4 = value => yup.string().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); const HexColorCodeScalar = `scalar HexColorCode`; const HexColorCode = new graphql.GraphQLScalarType({ name: `HexColorCode`, description: `A field whose value is a hex color code: https://en.wikipedia.org/wiki/Web_colors.`, serialize(value) { return validate$4(value); }, parseValue(value) { return validate$4(value); }, parseLiteral(ast) { if (ast.kind !== graphql.Kind.STRING) { throw new graphql.GraphQLError(`Can only validate strings as hex color codes but got a: ${ast.kind}`); } return validate$4(ast.value); } }); const validate$5 = value => yup.string().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); const HSLScalar = `scalar HSL`; const HSL = new graphql.GraphQLScalarType({ name: `HSL`, description: `A field whose value is a CSS HSL color: https://developer.mozilla.org/en-US/docs/Web/CSS/color_value#hsl()_and_hsla().`, serialize(value) { return validate$5(value); }, parseValue(value) { return validate$5(value); }, parseLiteral(ast) { if (ast.kind !== graphql.Kind.STRING) { throw new graphql.GraphQLError(`Can only validate strings as HSL colors but got a: ${ast.kind}`); } return validate$5(ast.value); } }); const validate$6 = value => yup.string().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); const HSLAScalar = `scalar HSLA`; const HSLA = new graphql.GraphQLScalarType({ name: `HSLA`, description: `A field whose value is a CSS HSLA color: https://developer.mozilla.org/en-US/docs/Web/CSS/color_value#hsl()_and_hsla().`, serialize(value) { return validate$6(value); }, parseValue(value) { return validate$6(value); }, parseLiteral(ast) { if (ast.kind !== graphql.Kind.STRING) { throw new graphql.GraphQLError(`Can only validate strings as HSLA colors but got a: ${ast.kind}`); } return validate$6(ast.value); } }); const validate$7 = value => yup.string().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); const IPv4Scalar = `scalar IPv4`; const IPv4 = new graphql.GraphQLScalarType({ name: `IPv4`, description: `A field whose value is a IPv4 address: https://en.wikipedia.org/wiki/IPv4.`, serialize(value) { return validate$7(value); }, parseValue(value) { return validate$7(value); }, parseLiteral(ast) { if (ast.kind !== graphql.Kind.STRING) { throw new graphql.GraphQLError(`Can only validate strings as IPv4 addresses but got a: ${ast.kind}`); } return validate$7(ast.value); } }); 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$8 = value => yup.string().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); const IPv6Scalar = `scalar IPv6`; const IPv6 = new graphql.GraphQLScalarType({ name: `IPv6`, description: `A field whose value is a IPv6 address: https://en.wikipedia.org/wiki/IPv6.`, serialize(value) { return validate$8(value); }, parseValue(value) { return validate$8(value); }, parseLiteral(ast) { if (ast.kind !== graphql.Kind.STRING) { throw new graphql.GraphQLError(`Can only validate strings as IPv6 addresses but got a: ${ast.kind}`); } return validate$8(ast.value); } }); const validate$9 = value => yup.string().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) && !isbn13.test(val)); }).validateSync(value); const ISBNScalar = `scalar ISBN`; const ISBN = new graphql.GraphQLScalarType({ name: `ISBN`, description: `A field whose value is a ISBN-10 or ISBN-13 number: https://en.wikipedia.org/wiki/International_Standard_Book_Number.`, serialize(value) { return validate$9(value); }, parseValue(value) { return validate$9(value); }, parseLiteral(ast) { if (ast.kind !== graphql.Kind.STRING) { throw new graphql.GraphQLError(`Can only validate strings as ISBN numbers but got a: ${ast.kind}`); } return validate$9(ast.value); } }); const validate$a = value => yup.string().strict(true).typeError(`Value is not string: ${value}`).matches(/^(?:[0-9A-Fa-f]{2}([:-]?)[0-9A-Fa-f]{2})(?:(?:\1|\.)(?:[0-9A-Fa-f]{2}([:-]?)[0-9A-Fa-f]{2})){2}$/, `Value is not a valid MAC address: ${value}`).validateSync(value); const MACScalar = `scalar MAC`; const MAC = new graphql.GraphQLScalarType({ name: `MAC`, description: `A field whose value is a IEEE 802 48-bit MAC address: https://en.wikipedia.org/wiki/MAC_address.`, serialize(value) { return validate$a(value); }, parseValue(value) { return validate$a(value); }, parseLiteral(ast) { if (ast.kind !== graphql.Kind.STRING) { throw new graphql.GraphQLError(`Can only validate strings as MAC addresses but got a: ${ast.kind}`); } return validate$a(ast.value); } }); const validate$b = value => { yup.number().typeError(`Value is not a number: ${value}`).notOneOf([Infinity, -Infinity], `Value is not a finite number: ${value}`).required(`Value is not a number: ${value}`).negative(`Value is not a negative number: ${value}`).validateSync(value); const parsed = parseFloat(value); yup.number().strict(true).lessThan(0, `Value is not less than 0: ${parsed}`).validateSync(parsed); return parsed; }; const NegativeFloatScalar = `scalar NegativeFloat`; const NegativeFloat = new graphql.GraphQLScalarType({ name: `NegativeFloat`, description: `Floats that will have a value less than 0.`, serialize(value) { return validate$b(value); }, parseValue(value) { return validate$b(value); }, parseLiteral(ast) { if (ast.kind !== graphql.Kind.FLOAT) { throw new graphql.GraphQLError(`Can only validate floating point numbers as negative floating point numbers but got a: ${ast.kind}`); } return validate$b(ast.value); } }); const validate$c = value => { yup.number().typeError(`Value is not a number: ${value}`).notOneOf([Infinity, -Infinity], `Value is not a finite number: ${value}`).required(`Value is not a number: ${value}`).test(`unsafeInt`, `Value is not a number: ${value}`, val => Number.isSafeInteger(val)).negative(`Value is not a negative number: ${value}`).validateSync(value); const parsed = parseInt(value, 10); yup.number().strict(true).integer(`Value is not an integer: ${value}`).lessThan(0, `Value is not less than 0: ${parsed}`).validateSync(parsed); return parsed; }; const NegativeIntScalar = `scalar NegativeInt`; const NegativeInt = new graphql.GraphQLScalarType({ name: `NegativeInt`, description: `Integers that will have a value less than 0.`, serialize(value) { return validate$c(value); }, parseValue(value) { return validate$c(value); }, parseLiteral(ast) { if (ast.kind !== graphql.Kind.INT) { throw new graphql.GraphQLError(`Can only validate integers as negative integers but got a: ${ast.kind}`); } return validate$c(ast.value); } }); const validate$d = value => { yup.number().typeError(`Value is not a number: ${value}`).notOneOf([Infinity, -Infinity], `Value is not a finite number: ${value}`).required(`Value is not a number: ${value}`).validateSync(value); const parsed = parseFloat(value); yup.number().strict(true).negative(`Value is not a negative number: ${parsed}`).max(0, `Value is not a non-positive number: ${parsed}`).validateSync(parsed); return parsed; }; const NonPositiveFloatScalar = `scalar NonPositiveFloat`; const NonPositiveFloat = new graphql.GraphQLScalarType({ name: `NonPositiveFloat`, description: `Floats that will have a value of 0 or less.`, serialize(value) { return validate$d(value); }, parseValue(value) { return validate$d(value); }, parseLiteral(ast) { if (ast.kind !== graphql.Kind.FLOAT) { throw new graphql.GraphQLError(`Can only validate floating point numbers as non-positive floating point numbers but got a: ${ast.kind}`); } return validate$d(ast.value); } }); const validate$e = value => { yup.number().typeError(`Value is not a number: ${value}`).notOneOf([Infinity, -Infinity], `Value is not a finite number: ${value}`).required(`Value is not a number: ${value}`).test(`unsafeInt`, `Value is not a number: ${value}`, val => Number.isSafeInteger(val)).validateSync(value); const parsed = parseInt(value, 10); yup.number().strict(true).integer(`Value is not an integer: ${parsed}`).negative(`Value is not a non-positive number: ${parsed}`).max(0, `Value is not a non-positive number: ${parsed}`).validateSync(parsed); return parsed; }; const NonPositiveIntScalar = `scalar NonPositiveInt`; const NonPositiveInt = new graphql.GraphQLScalarType({ name: `NonPositiveInt`, description: `Integers that will have a value of 0 or less.`, serialize(value) { return validate$e(value); }, parseValue(value) { return validate$e(value); }, parseLiteral(ast) { if (ast.kind !== graphql.Kind.INT) { throw new graphql.GraphQLError(`Can only validate integers as non-positive integers but got a: ${ast.kind}`); } return validate$e(ast.value); } }); const validate$f = value => yup.string().strict(true).typeError(`Value is not string: ${value}`).matches(/^\+\d{11,15}$/i, `Value is not a valid phone number of the form +17895551234 (10-15 digits): ${value}`).validateSync(value); const PhoneNumberScalar = `scalar PhoneNumber`; const PhoneNumber = new graphql.GraphQLScalarType({ name: `PhoneNumber`, description: `A field whose value conforms to the standard E.164 format as specified in: https://en.wikipedia.org/wiki/E.164. Basically this is +17895551234.`, serialize(value) { return validate$f(value); }, parseValue(value) { return validate$f(value); }, parseLiteral(ast) { if (ast.kind !== graphql.Kind.STRING) { throw new graphql.GraphQLError(`Can only validate strings as phone numbers but got a: ${ast.kind}`); } return validate$f(ast.value); } }); const validate$g = value => { yup.number().typeError(`Value is not a number: ${value}`).notOneOf([Infinity, -Infinity], `Value is not a finite number: ${value}`).test(`integer`, `Value is not a number: ${value}`, val => typeof val === `number` && !isNaN(val)).validateSync(value); const parsed = parseInt(value, 10); yup.number().strict(true).required(`Value is not a number: ${value}`).test(`port`, `Value is not a valid TCP port: ${parsed}`, val => !!val && Number.isSafeInteger(val) && val >= 0 && val <= 65535).validateSync(parsed); return parsed; }; const PortScalar = `scalar Port`; const Port = new graphql.GraphQLScalarType({ name: `Port`, description: `A field whose value is a valid TCP port within the range of 0 to 65535: https://en.wikipedia.org/wiki/Transmission_Control_Protocol#TCP_ports`, serialize(value) { return validate$g(value); }, parseValue(value) { return validate$g(value); }, parseLiteral(ast) { if (ast.kind !== graphql.Kind.INT) { throw new graphql.GraphQLError(`Can only validate integers as TCP ports but got a: ${ast.kind}`); } return validate$g(ast.value); } }); const validate$h = value => { yup.number().typeError(`Value is not a number: ${value}`).notOneOf([Infinity, -Infinity], `Value is not a finite number: ${value}`).required(`Value is not a number: ${value}`).positive(`Value is not a positive number: ${value}`).validateSync(value); const parsed = parseFloat(value); yup.number().strict(true).moreThan(0, `Value is not greater than 0: ${value}`).validateSync(parsed); return parsed; }; const PositiveFloatScalar = `scalar PositiveFloat`; const PositiveFloat = new graphql.GraphQLScalarType({ name: `PositiveFloat`, description: `Floats that will have a value greater than 0.`, serialize(value) { return validate$h(value); }, parseValue(value) { return validate$h(value); }, parseLiteral(ast) { if (ast.kind !== graphql.Kind.FLOAT) { throw new graphql.GraphQLError(`Can only validate floating point numbers as positive floating point numbers but got a: ${ast.kind}`); } return validate$h(ast.value); } }); const validate$i = value => { yup.number().typeError(`Value is not a number: ${value}`).notOneOf([Infinity, -Infinity], `Value is not a finite number: ${value}`).required(`Value is not a number: ${value}`).positive(`Value is not a positive number: ${value}`).test(`unsafeInt`, `Value is not a number: ${value}`, val => Number.isSafeInteger(val)).validateSync(value); const parsed = parseInt(value, 10); yup.number().strict(true).integer(`Value is not an integer: ${parsed}`).moreThan(0, `Value is not greater than 0: ${parsed}`).validateSync(parsed); return parsed; }; const PositiveIntScalar = `scalar PositiveInt`; const PositiveInt = new graphql.GraphQLScalarType({ name: `PositiveInt`, description: `Integers that will have a value greater than 0.`, serialize(value) { return validate$i(value); }, parseValue(value) { return validate$i(value); }, parseLiteral(ast) { if (ast.kind !== graphql.Kind.INT) { throw new graphql.GraphQLError(`Can only validate integers as positive integers but got a: ${ast.kind}`); } return validate$i(ast.value); } }); const countries = [`US`, `GB`, `DE`, `CA`, `FR`, `IT`, `AU`, `NL`, `ES`, `DK`, `SE`, `BE`, `IN`]; const validate$j = value => yup.string().strict(true).typeError(`Value is not string: ${value}`).test(`postal code`, `Value is not a valid postal code: ${value}`, val => countries.some(country => postalCodes.validate(country, val) === true)).validateSync(value); const PostalCodeScalar = `scalar PostalCode`; const PostalCode = new graphql.GraphQLScalarType({ name: `PostalCode`, description: `A field whose value conforms to the standard postal code formats for United States, United Kingdom, Germany, Canada, France, Italy, Australia, Netherlands, Spain, Denmark, Sweden, Belgium or India.`, serialize(value) { return validate$j(value); }, parseValue(value) { return validate$j(value); }, parseLiteral(ast) { if (ast.kind !== graphql.Kind.STRING) { throw new graphql.GraphQLError(`Can only validate strings as phone numbers but got a: ${ast.kind}`); } return validate$j(ast.value); } }); const validate$k = value => yup.string().strict(true).typeError(`Value is not string: ${value}`).matches(/^rgb\(\s*(-?\d+|-?\d*\.\d+(?=%))(%?)\s*,\s*(-?\d+|-?\d*\.\d+(?=%))(\2)\s*,\s*(-?\d+|-?\d*\.\d+(?=%))(\2)\s*\)$/, `Value is not a valid RGB color: ${value}`).validateSync(value); const RGBScalar = `scalar RGB`; const RGB = new graphql.GraphQLScalarType({ name: `RGB`, description: `A field whose value is a CSS RGB color: https://developer.mozilla.org/en-US/docs/Web/CSS/color_value#rgb()_and_rgba().`, serialize(value) { return validate$k(value); }, parseValue(value) { return validate$k(value); }, parseLiteral(ast) { if (ast.kind !== graphql.Kind.STRING) { throw new graphql.GraphQLError(`Can only validate strings as RGB colors but got a: ${ast.kind}`); } return validate$k(ast.value); } }); const validate$l = value => yup.string().strict(true).typeError(`Value is not string: ${value}`).matches(/^rgba\(\s*(-?\d+|-?\d*\.\d+(?=%))(%?)\s*,\s*(-?\d+|-?\d*\.\d+(?=%))(\2)\s*,\s*(-?\d+|-?\d*\.\d+(?=%))(\2)\s*,\s*(-?\d+|-?\d*.\d+)\s*\)$/, `Value is not a valid RGBA color: ${value}`).validateSync(value); const RGBAScalar = `scalar RGBA`; const RGBA = new graphql.GraphQLScalarType({ name: `RGBA`, description: `A field whose value is a CSS RGBA color: https://developer.mozilla.org/en-US/docs/Web/CSS/color_value#rgb()_and_rgba().`, serialize(value) { return validate$l(value); }, parseValue(value) { return validate$l(value); }, parseLiteral(ast) { if (ast.kind !== graphql.Kind.STRING) { throw new graphql.GraphQLError(`Can only validate strings as RGBA colors but got a: ${ast.kind}`); } return validate$l(ast.value); } }); const validate$m = value => { yup.number().typeError(`Value is not a number: ${value}`).notOneOf([Infinity, -Infinity], `Value is not a finite number: ${value}`).required(`Value is not a number: ${value}`).validateSync(value); const parsed = parseFloat(value); yup.number().strict(true).positive(`Value is not a non-negative number: ${parsed}`).min(0, `Value is not a non-negative number: ${parsed}`).validateSync(parsed); return parsed; }; const UnsignedFloatScalar = `scalar UnsignedFloat`; const UnsignedFloat = new graphql.GraphQLScalarType({ name: `UnsignedFloat`, description: `Floats that will have a value of 0 or more.`, serialize(value) { return validate$m(value); }, parseValue(value) { return validate$m(value); }, parseLiteral(ast) { if (ast.kind !== graphql.Kind.FLOAT) { throw new graphql.GraphQLError(`Can only validate floating point numbers as non-negative floating point numbers but got a: ${ast.kind}`); } return validate$m(ast.value); } }); const validate$n = value => { yup.number().typeError(`Value is not a number: ${value}`).notOneOf([Infinity, -Infinity], `Value is not a finite number: ${value}`).required(`Value is not a number: ${value}`).test(`unsafeInt`, `Value is not a number: ${value}`, val => Number.isSafeInteger(val)).validateSync(value); const parsed = parseInt(value, 10); yup.number().strict(true).integer(`Value is not an integer: ${parsed}`).positive(`Value is not a positive number: ${parsed}`).min(0, `Value is not a non-negative number: ${parsed}`).validateSync(parsed); return parsed; }; const UnsignedIntScalar = `scalar UnsignedInt`; const UnsignedInt = new graphql.GraphQLScalarType({ name: `UnsignedInt`, description: `Integers that will have a value of 0 or more.`, serialize(value) { return validate$n(value); }, parseValue(value) { return validate$n(value); }, parseLiteral(ast) { if (ast.kind !== graphql.Kind.INT) { throw new graphql.GraphQLError(`Can only validate integers as non-negative integers but got a: ${ast.kind}`); } return validate$n(ast.value); } }); const validate$o = value => yup.string().strict(true).typeError(`Value is not string: ${value}`).test(`uri`, `Value is not a valid URL: ${value}`, val => !!uriJs.parse(val).scheme).validateSync(value); const URLScalar = `scalar URL`; const URL = new graphql.GraphQLScalarType({ name: `URL`, description: `A field whose value conforms to the standard URL format as specified in RFC3986: https://www.ietf.org/rfc/rfc3986.txt.`, serialize(value) { return validate$o(value); }, parseValue(value) { return validate$o(value); }, parseLiteral(ast) { if (ast.kind !== graphql.Kind.STRING) { throw new graphql.GraphQLError(`Can only validate strings as URLs but got a: ${ast.kind}`); } return validate$o(ast.value); } }); const regularExpressionFactory = ({ name, regex }) => { const validate = value => yup.string().strict(true).typeError(`Value is not string: ${value}`).matches(new RegExp(regex), `Value does not match the regular expression ${regex}: ${value}`).validateSync(value); return { scalar: `scalar ${name}`, resolver: new graphql.GraphQLScalarType({ name, description: `A field whose value matches the provided regular expression ${regex}.`, serialize(value) { return validate(value); }, parseValue(value) { return validate(value); }, parseLiteral(ast) { if (ast.kind !== graphql.Kind.STRING) { throw new graphql.GraphQLError(`Can only validate strings as regular expressions but got a: ${ast.kind}`); } return validate(ast.value); } }) }; }; const rangeFactory = ({ name, start, end, float = false }) => { const validate = value => { yup.number().typeError(`Value is not a number: ${value}`).notOneOf([Infinity, -Infinity], `Value is not a finite number: ${value}`).required(`Value is not a number: ${value}`).min(0, `Value is not a positive number: ${value}`).validateSync(value); if (!float) { yup.number().integer(`Value is not an integer: ${value}`).test(`unsafeInt`, `Value is not a number: ${value}`, val => Number.isSafeInteger(val)).validateSync(value); } const parsed = float ? parseFloat(value) : parseInt(value, 10); yup.number().strict(true).min(start, `Value is less than limit: ${start}: ${parsed}`).max(end, `Value is greater than limit: ${end}: ${parsed}`).validateSync(parsed); return parsed; }; return { scalar: `scalar ${name}`, resolver: new graphql.GraphQLScalarType({ name, description: `A${float ? ` Float` : `n Integer`} with a value between ${start} and ${end} (inclusive).`, serialize(value) { return validate(value); }, parseValue(value) { return validate(value); }, parseLiteral(ast) { if (ast.kind !== (float ? graphql.Kind.FLOAT : graphql.Kind.INT)) { throw new graphql.GraphQLError(`Can only validate ${float ? `Floats` : `Integers`} but got a: ${ast.kind}`); } return validate(ast.value); } }) }; }; var index = new Map([[DateTimeScalar, DateTime], [EmailAddressScalar, EmailAddress], [GUIDScalar, GUID], [HexadecimalScalar, Hexadecimal], [HexColorCodeScalar, HexColorCode], [HSLScalar, HSL], [HSLAScalar, HSLA], [IPv4Scalar, IPv4], [IPv6Scalar, IPv6], [ISBNScalar, ISBN], [MACScalar, MAC], [NegativeFloatScalar, NegativeFloat], [NegativeIntScalar, NegativeInt], [NonPositiveFloatScalar, NonPositiveFloat], [NonPositiveIntScalar, NonPositiveInt], [PhoneNumberScalar, PhoneNumber], [PortScalar, Port], [PositiveFloatScalar, PositiveFloat], [PositiveIntScalar, PositiveInt], [PostalCodeScalar, PostalCode], [RGBScalar, RGB], [RGBAScalar, RGBA], [UnsignedFloatScalar, UnsignedFloat], [UnsignedIntScalar, UnsignedInt], [URLScalar, URL]]); exports.DateTime = DateTime; exports.DateTimeScalar = DateTimeScalar; exports.EmailAddress = EmailAddress; exports.EmailAddressScalar = EmailAddressScalar; exports.GUID = GUID; exports.GUIDScalar = GUIDScalar; exports.HSL = HSL; exports.HSLA = HSLA; exports.HSLAScalar = HSLAScalar; exports.HSLScalar = HSLScalar; exports.HexColorCode = HexColorCode; exports.HexColorCodeScalar = HexColorCodeScalar; exports.Hexadecimal = Hexadecimal; exports.HexadecimalScalar = HexadecimalScalar; exports.IPv4 = IPv4; exports.IPv4Scalar = IPv4Scalar; exports.IPv6 = IPv6; exports.IPv6Scalar = IPv6Scalar; exports.ISBN = ISBN; exports.ISBNScalar = ISBNScalar; exports.MAC = MAC; exports.MACScalar = MACScalar; exports.NegativeFloat = NegativeFloat; exports.NegativeFloatScalar = NegativeFloatScalar; exports.NegativeInt = NegativeInt; exports.NegativeIntScalar = NegativeIntScalar; exports.NonPositiveFloat = NonPositiveFloat; exports.NonPositiveFloatScalar = NonPositiveFloatScalar; exports.NonPositiveInt = NonPositiveInt; exports.NonPositiveIntScalar = NonPositiveIntScalar; exports.PhoneNumber = PhoneNumber; exports.PhoneNumberScalar = PhoneNumberScalar; exports.Port = Port; exports.PortScalar = PortScalar; exports.PositiveFloat = PositiveFloat; exports.PositiveFloatScalar = PositiveFloatScalar; exports.PositiveInt = PositiveInt; exports.PositiveIntScalar = PositiveIntScalar; exports.PostalCode = PostalCode; exports.PostalCodeScalar = PostalCodeScalar; exports.RGB = RGB; exports.RGBA = RGBA; exports.RGBAScalar = RGBAScalar; exports.RGBScalar = RGBScalar; exports.URL = URL; exports.URLScalar = URLScalar; exports.UnsignedFloat = UnsignedFloat; exports.UnsignedFloatScalar = UnsignedFloatScalar; exports.UnsignedInt = UnsignedInt; exports.UnsignedIntScalar = UnsignedIntScalar; exports.default = index; exports.rangeFactory = rangeFactory; exports.regularExpressionFactory = regularExpressionFactory; //# sourceMappingURL=index.js.map