@okgrow/graphql-scalars
Advanced tools
Comparing version 0.1.0 to 0.2.0
@@ -6,3 +6,3 @@ 'use strict'; | ||
}); | ||
exports.URL = exports.EmailAddress = exports.NonNegativeFloat = exports.PositiveFloat = exports.NonNegativeInt = exports.PositiveInt = exports.DateTime = undefined; | ||
exports.URL = exports.EmailAddress = exports.NegativeFloat = exports.NonNegativeFloat = exports.PositiveFloat = exports.NonPositiveFloat = exports.NegativeInt = exports.NonNegativeInt = exports.PositiveInt = exports.NonPositiveInt = exports.DateTime = undefined; | ||
@@ -13,2 +13,6 @@ var _DateTime = require('./DateTime'); | ||
var _NonPositiveInt = require('./NonPositiveInt'); | ||
var _NonPositiveInt2 = _interopRequireDefault(_NonPositiveInt); | ||
var _PositiveInt = require('./PositiveInt'); | ||
@@ -22,2 +26,10 @@ | ||
var _NegativeInt = require('./NegativeInt'); | ||
var _NegativeInt2 = _interopRequireDefault(_NegativeInt); | ||
var _NonPositiveFloat = require('./NonPositiveFloat'); | ||
var _NonPositiveFloat2 = _interopRequireDefault(_NonPositiveFloat); | ||
var _PositiveFloat = require('./PositiveFloat'); | ||
@@ -31,2 +43,6 @@ | ||
var _NegativeFloat = require('./NegativeFloat'); | ||
var _NegativeFloat2 = _interopRequireDefault(_NegativeFloat); | ||
var _EmailAddress = require('./EmailAddress'); | ||
@@ -43,6 +59,10 @@ | ||
exports.DateTime = _DateTime2.default; | ||
exports.NonPositiveInt = _NonPositiveInt2.default; | ||
exports.PositiveInt = _PositiveInt2.default; | ||
exports.NonNegativeInt = _NonNegativeInt2.default; | ||
exports.NegativeInt = _NegativeInt2.default; | ||
exports.NonPositiveFloat = _NonPositiveFloat2.default; | ||
exports.PositiveFloat = _PositiveFloat2.default; | ||
exports.NonNegativeFloat = _NonNegativeFloat2.default; | ||
exports.NegativeFloat = _NegativeFloat2.default; | ||
exports.EmailAddress = _EmailAddress2.default; | ||
@@ -54,6 +74,8 @@ exports.URL = _URL2.default; | ||
NonNegativeInt: _NonNegativeInt2.default, | ||
NegativeInt: _NegativeInt2.default, | ||
PositiveFloat: _PositiveFloat2.default, | ||
NonNegativeFloat: _NonNegativeFloat2.default, | ||
NegativeFloat: _NegativeFloat2.default, | ||
EmailAddress: _EmailAddress2.default, | ||
URL: _URL2.default | ||
}; |
@@ -13,14 +13,4 @@ 'use strict'; | ||
function processValue(value) { | ||
if (isNaN(value)) { | ||
throw new TypeError('Value is not a number: ' + value); | ||
} | ||
var _utilities = require('./utilities'); | ||
if (value < 0) { | ||
throw new TypeError('Value is a negative number: ' + value); | ||
} | ||
return parseFloat(value); | ||
} | ||
exports.default = new _graphql.GraphQLScalarType({ | ||
@@ -32,6 +22,6 @@ name: 'NonNegativeFloat', | ||
serialize: function serialize(value) { | ||
return processValue(value); | ||
return (0, _utilities.processValue)(value, _utilities.VALIDATIONS.NonNegativeFloat); | ||
}, | ||
parseValue: function parseValue(value) { | ||
return processValue(value); | ||
return (0, _utilities.processValue)(value, _utilities.VALIDATIONS.NonNegativeFloat); | ||
}, | ||
@@ -43,4 +33,4 @@ parseLiteral: function parseLiteral(ast) { | ||
return processValue(ast.value); | ||
return (0, _utilities.processValue)(ast.value, _utilities.VALIDATIONS.NonNegativeFloat); | ||
} | ||
}); |
@@ -13,14 +13,4 @@ 'use strict'; | ||
function processValue(value) { | ||
if (isNaN(value)) { | ||
throw new TypeError('Value is not a number: ' + value); | ||
} | ||
var _utilities = require('./utilities'); | ||
if (value < 0) { | ||
throw new TypeError('Value is a negative number: ' + value); | ||
} | ||
return parseInt(value, 10); | ||
} | ||
exports.default = new _graphql.GraphQLScalarType({ | ||
@@ -32,6 +22,6 @@ name: 'NonNegativeInt', | ||
serialize: function serialize(value) { | ||
return processValue(value); | ||
return (0, _utilities.processValue)(value, _utilities.VALIDATIONS.NonNegativeInt); | ||
}, | ||
parseValue: function parseValue(value) { | ||
return processValue(value); | ||
return (0, _utilities.processValue)(value, _utilities.VALIDATIONS.NonNegativeInt); | ||
}, | ||
@@ -43,4 +33,4 @@ parseLiteral: function parseLiteral(ast) { | ||
return processValue(ast.value); | ||
return (0, _utilities.processValue)(ast.value, _utilities.VALIDATIONS.NonNegativeInt); | ||
} | ||
}); |
@@ -13,14 +13,4 @@ 'use strict'; | ||
function processValue(value) { | ||
if (isNaN(value)) { | ||
throw new TypeError('Value is not a number: ' + value); | ||
} | ||
var _utilities = require('./utilities'); | ||
if (!(value > 0)) { | ||
throw new TypeError('Value is not a positive number: ' + value); | ||
} | ||
return parseFloat(value); | ||
} | ||
exports.default = new _graphql.GraphQLScalarType({ | ||
@@ -32,6 +22,6 @@ name: 'PositiveFloat', | ||
serialize: function serialize(value) { | ||
return processValue(value); | ||
return (0, _utilities.processValue)(value, _utilities.VALIDATIONS.PositiveFloat); | ||
}, | ||
parseValue: function parseValue(value) { | ||
return processValue(value); | ||
return (0, _utilities.processValue)(value, _utilities.VALIDATIONS.PositiveFloat); | ||
}, | ||
@@ -43,4 +33,4 @@ parseLiteral: function parseLiteral(ast) { | ||
return processValue(ast.value); | ||
return (0, _utilities.processValue)(ast.value, _utilities.VALIDATIONS.PositiveFloat); | ||
} | ||
}); |
@@ -13,14 +13,4 @@ 'use strict'; | ||
function processValue(value) { | ||
if (isNaN(value)) { | ||
throw new TypeError('Value is not a number: ' + value); | ||
} | ||
var _utilities = require('./utilities'); | ||
if (!(value > 0)) { | ||
throw new TypeError('Value is not a positive number: ' + value); | ||
} | ||
return parseInt(value, 10); | ||
} | ||
exports.default = new _graphql.GraphQLScalarType({ | ||
@@ -32,6 +22,6 @@ name: 'PositiveInt', | ||
serialize: function serialize(value) { | ||
return processValue(value); | ||
return (0, _utilities.processValue)(value, _utilities.VALIDATIONS.PositiveInt); | ||
}, | ||
parseValue: function parseValue(value) { | ||
return processValue(value); | ||
return (0, _utilities.processValue)(value, _utilities.VALIDATIONS.PositiveInt); | ||
}, | ||
@@ -43,4 +33,4 @@ parseLiteral: function parseLiteral(ast) { | ||
return processValue(ast.value); | ||
return (0, _utilities.processValue)(ast.value, _utilities.VALIDATIONS.PositiveInt); | ||
} | ||
}); |
{ | ||
"name": "@okgrow/graphql-scalars", | ||
"version": "0.1.0", | ||
"version": "0.2.0", | ||
"description": "A collection of scalar types not included in base GraphQL.", | ||
@@ -26,3 +26,5 @@ "repository": { | ||
"babel-cli": "^6.24.0", | ||
"babel-preset-latest": "^6.24.0", | ||
"babel-eslint": "^8.0.1", | ||
"babel-plugin-transform-es2015-modules-commonjs": "^6.26.0", | ||
"babel-preset-env": "^1.6.0", | ||
"eslint": "^3.18.0", | ||
@@ -33,8 +35,9 @@ "eslint-config-airbnb": "^14.1.0", | ||
"eslint-plugin-react": "^6.10.3", | ||
"graphql": "^0.10.1", | ||
"jest": "^20.0.4", | ||
"nodemon": "1.11.x" | ||
}, | ||
"dependencies": { | ||
"peerDependencies": { | ||
"graphql": "^0.10.1" | ||
} | ||
} |
# @okgrow/graphql-scalars | ||
A library of custom GraphQL scalar types for creating precise type-safe GraphQL schemas. | ||
[![npm version](https://badge.fury.io/js/%40okgrow%2Fgraphql-scalars.svg)](https://badge.fury.io/js/%40okgrow%2Fgraphql-scalars) | ||
[![Build Status](https://semaphoreci.com/api/v1/projects/649ab71f-35fe-440e-8e4b-3f68aaad0f2a/1545482/shields_badge.svg)](https://semaphoreci.com/okgrow/graphql-scalars) | ||
> A library of custom GraphQL [scalar types](http://graphql.org/learn/schema/#scalar-types) for creating precise type-safe GraphQL schemas. | ||
@@ -19,6 +21,11 @@ ## Installation | ||
scalar NonPositiveInt | ||
scalar PositiveInt | ||
scalar NonNegativeInt | ||
scalar NegativeInt | ||
scalar NonPositiveFloat | ||
scalar PositiveFloat | ||
scalar NonNegativeFloat | ||
scalar NegativeFloat | ||
@@ -33,5 +40,13 @@ scalar EmailAddress | ||
DateTime, | ||
NonPositiveInt, | ||
PositiveInt, | ||
NonNegativeInt, | ||
NegativeInt, | ||
NonPositiveFloat, | ||
PositiveFloat, | ||
NonNegativeFloat, | ||
NegativeFloat, | ||
EmailAddress, | ||
@@ -48,6 +63,11 @@ URL, | ||
NonPositiveInt, | ||
PositiveInt, | ||
NonNegativeInt, | ||
NegativeInt, | ||
NonPositiveFloat, | ||
PositiveFloat, | ||
NonNegativeFloat, | ||
NegativeFloat, | ||
@@ -138,11 +158,23 @@ EmailAddress, | ||
### NonPositiveInt | ||
Integers that will have a value of 0 or less. Uses [`parseInt()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt). | ||
### PositiveInt | ||
Integers that will have a value greater than 0. Uses [`parseInt()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt). | ||
### NegativeInt | ||
Integers that will have a value less than 0. Uses [`parseInt()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseInt). | ||
### NonNegativeFloat | ||
Floats that will have a value of 0 or more. Uses [`parseFloat()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat). | ||
### NonPositiveFloat | ||
Floats that will have a value of 0 or less. Uses [`parseFloat()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat). | ||
### PositiveFloat | ||
Floats that will have a value greater than 0. Uses [`parseFloat()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat). | ||
### NegativeFloat | ||
Floats that will have a value less than 0. Uses [`parseFloat()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/parseFloat). | ||
### EmailAddress | ||
@@ -162,11 +194,7 @@ A field whose value conforms to the standard internet email address format as specified in | ||
- NegativeInt | ||
- NegativeFloat | ||
These are easy to add, we just haven't run into cases for them yet. | ||
- PhoneNumber | ||
- PostalCode | ||
- BLOB | ||
These both have challenges in terms of making them globally useful so they need a bit of thought. | ||
These all have challenges in terms of making them globally useful so they need a bit of thought. | ||
@@ -182,2 +210,4 @@ For `PhoneNumber` we can probably just use the [E.164 specification](https://en.wikipedia.org/wiki/E.164) | ||
BLOBs could be a base64-encoded object of some kind. | ||
## What's this all about? | ||
@@ -206,2 +236,3 @@ GraphQL is a wonderful new approach to application data and API layers that's gaining momentum. If | ||
Issues and Pull Requests are always welcome. | ||
Please read our [contribution guidelines](https://github.com/okgrow/guides/blob/master/contributing.md). | ||
Please read our [contribution guidelines](https://okgrow.github.io/guides/docs/open-source-contributing.html). |
@@ -71,14 +71,44 @@ /* global describe, test, expect */ | ||
describe('invalid', () => { | ||
describe('null', () => { | ||
test('serialize', () => { | ||
expect(() => NonNegativeFloat.serialize(null)).toThrow(/Value is not a number/); | ||
}); | ||
// FIXME: Does nothing. No throw. Call doesn't even seem to get to the parseValue() function. | ||
// test('parseValue', () => { | ||
// expect(() => NonNegativeFloat.parseValue(null)).toThrow(/Value is not a number/); | ||
// }); | ||
test('parseLiteral', () => { | ||
expect(() => NonNegativeFloat.parseLiteral({ value: null, kind: Kind.FLOAT })).toThrow(/Value is not a number/); | ||
}); | ||
}); | ||
describe('undefined', () => { | ||
test('serialize', () => { | ||
expect(() => NonNegativeFloat.serialize(undefined)).toThrow(/Value is not a number/); | ||
}); | ||
// FIXME: Does nothing. No throw. Call doesn't even seem to get to the parseValue() function. | ||
// test('parseValue', () => { | ||
// expect(() => NonNegativeFloat.parseValue(undefined)).toThrow(/Value is not a number/); | ||
// }); | ||
test('parseLiteral', () => { | ||
expect(() => NonNegativeFloat.parseLiteral({ value: undefined, kind: Kind.FLOAT })).toThrow(/Value is not a number/); | ||
}); | ||
}); | ||
describe('less than zero', () => { | ||
describe('as float', () => { | ||
test('serialize', () => { | ||
expect(() => NonNegativeFloat.serialize(-1.0)).toThrow(/Value is a negative number/); | ||
expect(() => NonNegativeFloat.serialize(-1.0)).toThrow(/Value is not a non-negative number/); | ||
}); | ||
test('parseValue', () => { | ||
expect(() => NonNegativeFloat.parseValue(-1.0)).toThrow(/Value is a negative number/); | ||
expect(() => NonNegativeFloat.parseValue(-1.0)).toThrow(/Value is not a non-negative number/); | ||
}); | ||
test('parseLiteral', () => { | ||
expect(() => NonNegativeFloat.parseLiteral({ value: -1.0, kind: Kind.FLOAT })).toThrow(/Value is a negative number/); | ||
expect(() => NonNegativeFloat.parseLiteral({ value: -1.0, kind: Kind.FLOAT })).toThrow(/Value is not a non-negative number/); | ||
}); | ||
@@ -89,11 +119,11 @@ }); | ||
test('serialize', () => { | ||
expect(() => NonNegativeFloat.serialize('-1.0')).toThrow(/Value is a negative number/); | ||
expect(() => NonNegativeFloat.serialize('-1.0')).toThrow(/Value is not a non-negative number/); | ||
}); | ||
test('parseValue', () => { | ||
expect(() => NonNegativeFloat.parseValue('-1.0')).toThrow(/Value is a negative number/); | ||
expect(() => NonNegativeFloat.parseValue('-1.0')).toThrow(/Value is not a non-negative number/); | ||
}); | ||
test('parseLiteral', () => { | ||
expect(() => NonNegativeFloat.parseLiteral({ value: '-1.0', kind: Kind.FLOAT })).toThrow(/Value is a negative number/); | ||
expect(() => NonNegativeFloat.parseLiteral({ value: '-1.0', kind: Kind.FLOAT })).toThrow(/Value is not a non-negative number/); | ||
}); | ||
@@ -103,2 +133,16 @@ }); | ||
describe('infinity', () => { | ||
test('serialize', () => { | ||
expect(() => NonNegativeFloat.serialize(Number.POSITIVE_INFINITY)).toThrow(/Value is not a finite number/); | ||
}); | ||
test('parseValue', () => { | ||
expect(() => NonNegativeFloat.parseValue(Number.POSITIVE_INFINITY)).toThrow(/Value is not a finite number/); | ||
}); | ||
test('parseLiteral', () => { | ||
expect(() => NonNegativeFloat.parseLiteral({ value: Number.POSITIVE_INFINITY, kind: Kind.FLOAT })).toThrow(/Value is not a finite number/); | ||
}); | ||
}); | ||
describe('not a number', () => { | ||
@@ -117,3 +161,18 @@ test('serialize', () => { | ||
}); | ||
describe('NaN', () => { | ||
test('serialize', () => { | ||
expect(() => NonNegativeFloat.serialize(Number.NaN)).toThrow(/Value is not a number/); | ||
}); | ||
// FIXME: Does nothing. No throw. Call doesn't even seem to get to the parseValue() function. | ||
// test('parseValue', () => { | ||
// expect(() => NonNegativeFloat.parseValue(Number.NaN)).toThrow(/Value is not a number/); | ||
// }); | ||
test('parseLiteral', () => { | ||
expect(() => NonNegativeFloat.parseLiteral({ value: Number.NaN, kind: Kind.STRING })).toThrow(/Can only validate floating point numbers as non-negative floating point numbers but got a/); | ||
}); | ||
}); | ||
}); | ||
}); |
@@ -71,14 +71,59 @@ /* global describe, test, expect */ | ||
describe('invalid', () => { | ||
describe('null', () => { | ||
test('serialize', () => { | ||
expect(() => NonNegativeInt.serialize(null)).toThrow(/Value is not a number/); | ||
}); | ||
// FIXME: Does nothing. No throw. Call doesn't even seem to get to the parseValue() function. | ||
// test('parseValue', () => { | ||
// expect(() => NonNegativeInt.parseValue(null)).toThrow(/Value is not a number/); | ||
// }); | ||
test('parseLiteral', () => { | ||
expect(() => NonNegativeInt.parseLiteral({ value: null, kind: Kind.INT })).toThrow(/Value is not a number/); | ||
}); | ||
}); | ||
describe('undefined', () => { | ||
test('serialize', () => { | ||
expect(() => NonNegativeInt.serialize(undefined)).toThrow(/Value is not a number/); | ||
}); | ||
// FIXME: Does nothing. No throw. Call doesn't even seem to get to the parseValue() function. | ||
// test('parseValue', () => { | ||
// expect(() => NonNegativeInt.parseValue(undefined)).toThrow(/Value is not a number/); | ||
// }); | ||
test('parseLiteral', () => { | ||
expect(() => NonNegativeInt.parseLiteral({ value: undefined, kind: Kind.INT })).toThrow(/Value is not a number/); | ||
}); | ||
}); | ||
describe('unsafe integer', () => { | ||
test('serialize', () => { | ||
expect(() => NonNegativeInt.serialize(2 ** 53)).toThrow(/Value is not a safe integer/); | ||
}); | ||
// FIXME: Does nothing. No throw. Call doesn't even seem to get to the parseValue() function. | ||
test('parseValue', () => { | ||
expect(() => NonNegativeInt.parseValue(2 ** 53)).toThrow(/Value is not a safe integer/); | ||
}); | ||
test('parseLiteral', () => { | ||
expect(() => NonNegativeInt.parseLiteral({ value: 2 ** 53, kind: Kind.INT })).toThrow(/Value is not a safe integer/); | ||
}); | ||
}); | ||
describe('less than zero', () => { | ||
describe('as int', () => { | ||
test('serialize', () => { | ||
expect(() => NonNegativeInt.serialize(-1)).toThrow(/Value is a negative number/); | ||
expect(() => NonNegativeInt.serialize(-1)).toThrow(/Value is not a non-negative number/); | ||
}); | ||
test('parseValue', () => { | ||
expect(() => NonNegativeInt.parseValue(-1)).toThrow(/Value is a negative number/); | ||
expect(() => NonNegativeInt.parseValue(-1)).toThrow(/Value is not a non-negative number/); | ||
}); | ||
test('parseLiteral', () => { | ||
expect(() => NonNegativeInt.parseLiteral({ value: -1, kind: Kind.INT })).toThrow(/Value is a negative number/); | ||
expect(() => NonNegativeInt.parseLiteral({ value: -1, kind: Kind.INT })).toThrow(/Value is not a non-negative number/); | ||
}); | ||
@@ -89,11 +134,11 @@ }); | ||
test('serialize', () => { | ||
expect(() => NonNegativeInt.serialize('-1')).toThrow(/Value is a negative number/); | ||
expect(() => NonNegativeInt.serialize('-1')).toThrow(/Value is not a non-negative number/); | ||
}); | ||
test('parseValue', () => { | ||
expect(() => NonNegativeInt.parseValue('-1')).toThrow(/Value is a negative number/); | ||
expect(() => NonNegativeInt.parseValue('-1')).toThrow(/Value is not a non-negative number/); | ||
}); | ||
test('parseLiteral', () => { | ||
expect(() => NonNegativeInt.parseLiteral({ value: '-1', kind: Kind.INT })).toThrow(/Value is a negative number/); | ||
expect(() => NonNegativeInt.parseLiteral({ value: '-1', kind: Kind.INT })).toThrow(/Value is not a non-negative number/); | ||
}); | ||
@@ -103,2 +148,16 @@ }); | ||
describe('infinity', () => { | ||
test('serialize', () => { | ||
expect(() => NonNegativeInt.serialize(Number.POSITIVE_INFINITY)).toThrow(/Value is not a finite number/); | ||
}); | ||
test('parseValue', () => { | ||
expect(() => NonNegativeInt.parseValue(Number.POSITIVE_INFINITY)).toThrow(/Value is not a finite number/); | ||
}); | ||
test('parseLiteral', () => { | ||
expect(() => NonNegativeInt.parseLiteral({ value: Number.POSITIVE_INFINITY, kind: Kind.INT })).toThrow(/Value is not a finite number/); | ||
}); | ||
}); | ||
describe('not a number', () => { | ||
@@ -117,3 +176,18 @@ test('serialize', () => { | ||
}); | ||
describe('NaN', () => { | ||
test('serialize', () => { | ||
expect(() => NonNegativeInt.serialize(Number.NaN)).toThrow(/Value is not a number/); | ||
}); | ||
// FIXME: Does nothing. No throw. Call doesn't even seem to get to the parseValue() function. | ||
// test('parseValue', () => { | ||
// expect(() => NonNegativeInt.parseValue(Number.NaN)).toThrow(/Value is not a number/); | ||
// }); | ||
test('parseLiteral', () => { | ||
expect(() => NonNegativeInt.parseLiteral({ value: Number.NaN, kind: Kind.STRING })).toThrow(/Can only validate integers as non-negative integers but got a/); | ||
}); | ||
}); | ||
}); | ||
}); |
@@ -39,2 +39,32 @@ /* global describe, test, expect */ | ||
describe('invalid', () => { | ||
describe('null', () => { | ||
test('serialize', () => { | ||
expect(() => PositiveFloat.serialize(null)).toThrow(/Value is not a number/); | ||
}); | ||
// FIXME: Does nothing. No throw. Call doesn't even seem to get to the parseValue() function. | ||
// test('parseValue', () => { | ||
// expect(() => PositiveFloat.parseValue(null)).toThrow(/Value is not a number/); | ||
// }); | ||
test('parseLiteral', () => { | ||
expect(() => PositiveFloat.parseLiteral({ value: null, kind: Kind.FLOAT })).toThrow(/Value is not a number/); | ||
}); | ||
}); | ||
describe('undefined', () => { | ||
test('serialize', () => { | ||
expect(() => PositiveFloat.serialize(undefined)).toThrow(/Value is not a number/); | ||
}); | ||
// FIXME: Does nothing. No throw. Call doesn't even seem to get to the parseValue() function. | ||
// test('parseValue', () => { | ||
// expect(() => PositiveFloat.parseValue(undefined)).toThrow(/Value is not a number/); | ||
// }); | ||
test('parseLiteral', () => { | ||
expect(() => PositiveFloat.parseLiteral({ value: undefined, kind: Kind.FLOAT })).toThrow(/Value is not a number/); | ||
}); | ||
}); | ||
describe('zero', () => { | ||
@@ -100,2 +130,16 @@ describe('as float', () => { | ||
describe('infinity', () => { | ||
test('serialize', () => { | ||
expect(() => PositiveFloat.serialize(Number.POSITIVE_INFINITY)).toThrow(/Value is not a finite number/); | ||
}); | ||
test('parseValue', () => { | ||
expect(() => PositiveFloat.parseValue(Number.POSITIVE_INFINITY)).toThrow(/Value is not a finite number/); | ||
}); | ||
test('parseLiteral', () => { | ||
expect(() => PositiveFloat.parseLiteral({ value: Number.POSITIVE_INFINITY, kind: Kind.FLOAT })).toThrow(/Value is not a finite number/); | ||
}); | ||
}); | ||
describe('not a number', () => { | ||
@@ -114,3 +158,18 @@ test('serialize', () => { | ||
}); | ||
describe('NaN', () => { | ||
test('serialize', () => { | ||
expect(() => PositiveFloat.serialize(Number.NaN)).toThrow(/Value is not a number/); | ||
}); | ||
// FIXME: Does nothing. No throw. Call doesn't even seem to get to the parseValue() function. | ||
// test('parseValue', () => { | ||
// expect(() => PositiveFloat.parseValue(Number.NaN)).toThrow(/Value is not a number/); | ||
// }); | ||
test('parseLiteral', () => { | ||
expect(() => PositiveFloat.parseLiteral({ value: Number.NaN, kind: Kind.STRING })).toThrow(/Can only validate floating point numbers as positive floating point numbers but got a/); | ||
}); | ||
}); | ||
}); | ||
}); |
@@ -39,2 +39,47 @@ /* global describe, test, expect */ | ||
describe('invalid', () => { | ||
describe('null', () => { | ||
test('serialize', () => { | ||
expect(() => PositiveInt.serialize(null)).toThrow(/Value is not a number: null/); | ||
}); | ||
// FIXME: Does nothing. No throw. Call doesn't even seem to get to the parseValue() function. | ||
// test('parseValue', () => { | ||
// expect(() => PositiveInt.parseValue(null)).toThrow(/Value is not a number/); | ||
// }); | ||
test('parseLiteral', () => { | ||
expect(() => PositiveInt.parseLiteral({ value: null, kind: Kind.INT })).toThrow(/Value is not a number: null/); | ||
}); | ||
}); | ||
describe('undefined', () => { | ||
test('serialize', () => { | ||
expect(() => PositiveInt.serialize(undefined)).toThrow(/Value is not a number: undefined/); | ||
}); | ||
// FIXME: Does nothing. No throw. Call doesn't even seem to get to the parseValue() function. | ||
// test('parseValue', () => { | ||
// expect(() => PositiveInt.parseValue(undefined)).toThrow(/Value is not a number/); | ||
// }); | ||
test('parseLiteral', () => { | ||
expect(() => PositiveInt.parseLiteral({ value: undefined, kind: Kind.INT })).toThrow(/Value is not a number: undefined/); | ||
}); | ||
}); | ||
describe('unsafe integer', () => { | ||
test('serialize', () => { | ||
expect(() => PositiveInt.serialize(2 ** 53)).toThrow(/Value is not a safe integer/); | ||
}); | ||
// FIXME: Does nothing. No throw. Call doesn't even seem to get to the parseValue() function. | ||
test('parseValue', () => { | ||
expect(() => PositiveInt.parseValue(2 ** 53)).toThrow(/Value is not a safe integer/); | ||
}); | ||
test('parseLiteral', () => { | ||
expect(() => PositiveInt.parseLiteral({ value: 2 ** 53, kind: Kind.INT })).toThrow(/Value is not a safe integer/); | ||
}); | ||
}); | ||
describe('zero', () => { | ||
@@ -100,2 +145,16 @@ describe('as int', () => { | ||
describe('infinity', () => { | ||
test('serialize', () => { | ||
expect(() => PositiveInt.serialize(Number.POSITIVE_INFINITY)).toThrow(/Value is not a finite number/); | ||
}); | ||
test('parseValue', () => { | ||
expect(() => PositiveInt.parseValue(Number.POSITIVE_INFINITY)).toThrow(/Value is not a finite number/); | ||
}); | ||
test('parseLiteral', () => { | ||
expect(() => PositiveInt.parseLiteral({ value: Number.POSITIVE_INFINITY, kind: Kind.INT })).toThrow(/Value is not a finite number/); | ||
}); | ||
}); | ||
describe('not a number', () => { | ||
@@ -114,3 +173,18 @@ test('serialize', () => { | ||
}); | ||
describe('NaN', () => { | ||
test('serialize', () => { | ||
expect(() => PositiveInt.serialize(Number.NaN)).toThrow(/Value is not a number/); | ||
}); | ||
// FIXME: Does nothing. No throw. Call doesn't even seem to get to the parseValue() function. | ||
// test('parseValue', () => { | ||
// expect(() => PositiveInt.parseValue(Number.NaN)).toThrow(/Value is not a number/); | ||
// }); | ||
test('parseLiteral', () => { | ||
expect(() => PositiveInt.parseLiteral({ value: Number.NaN, kind: Kind.STRING })).toThrow(/Can only validate integers as positive integers but got a/); | ||
}); | ||
}); | ||
}); | ||
}); |
import DateTime from './DateTime'; | ||
import NonPositiveInt from './NonPositiveInt'; | ||
import PositiveInt from './PositiveInt'; | ||
import NonNegativeInt from './NonNegativeInt'; | ||
import NegativeInt from './NegativeInt'; | ||
import NonPositiveFloat from './NonPositiveFloat'; | ||
import PositiveFloat from './PositiveFloat'; | ||
import NonNegativeFloat from './NonNegativeFloat'; | ||
import NegativeFloat from './NegativeFloat'; | ||
import EmailAddress from './EmailAddress'; | ||
@@ -11,6 +15,10 @@ import URL from './URL'; | ||
DateTime, | ||
NonPositiveInt, | ||
PositiveInt, | ||
NonNegativeInt, | ||
NegativeInt, | ||
NonPositiveFloat, | ||
PositiveFloat, | ||
NonNegativeFloat, | ||
NegativeFloat, | ||
EmailAddress, | ||
@@ -24,6 +32,8 @@ URL, | ||
NonNegativeInt, | ||
NegativeInt, | ||
PositiveFloat, | ||
NonNegativeFloat, | ||
NegativeFloat, | ||
EmailAddress, | ||
URL, | ||
}; |
@@ -5,14 +5,4 @@ import { GraphQLScalarType } from 'graphql'; | ||
function processValue(value) { | ||
if (isNaN(value)) { | ||
throw new TypeError(`Value is not a number: ${value}`); | ||
} | ||
import { processValue, VALIDATIONS } from './utilities'; | ||
if (value < 0) { | ||
throw new TypeError(`Value is a negative number: ${value}`); | ||
} | ||
return parseFloat(value); | ||
} | ||
export default new GraphQLScalarType({ | ||
@@ -24,7 +14,7 @@ name: 'NonNegativeFloat', | ||
serialize(value) { | ||
return processValue(value); | ||
return processValue(value, VALIDATIONS.NonNegativeFloat); | ||
}, | ||
parseValue(value) { | ||
return processValue(value); | ||
return processValue(value, VALIDATIONS.NonNegativeFloat); | ||
}, | ||
@@ -37,4 +27,4 @@ | ||
return processValue(ast.value); | ||
return processValue(ast.value, VALIDATIONS.NonNegativeFloat); | ||
}, | ||
}); |
@@ -5,14 +5,4 @@ import { GraphQLScalarType } from 'graphql'; | ||
function processValue(value) { | ||
if (isNaN(value)) { | ||
throw new TypeError(`Value is not a number: ${value}`); | ||
} | ||
import { processValue, VALIDATIONS } from './utilities'; | ||
if (value < 0) { | ||
throw new TypeError(`Value is a negative number: ${value}`); | ||
} | ||
return parseInt(value, 10); | ||
} | ||
export default new GraphQLScalarType({ | ||
@@ -24,7 +14,7 @@ name: 'NonNegativeInt', | ||
serialize(value) { | ||
return processValue(value); | ||
return processValue(value, VALIDATIONS.NonNegativeInt); | ||
}, | ||
parseValue(value) { | ||
return processValue(value); | ||
return processValue(value, VALIDATIONS.NonNegativeInt); | ||
}, | ||
@@ -37,4 +27,4 @@ | ||
return processValue(ast.value); | ||
return processValue(ast.value, VALIDATIONS.NonNegativeInt); | ||
}, | ||
}); |
@@ -5,14 +5,4 @@ import { GraphQLScalarType } from 'graphql'; | ||
function processValue(value) { | ||
if (isNaN(value)) { | ||
throw new TypeError(`Value is not a number: ${value}`); | ||
} | ||
import { processValue, VALIDATIONS } from './utilities'; | ||
if (!(value > 0)) { | ||
throw new TypeError(`Value is not a positive number: ${value}`); | ||
} | ||
return parseFloat(value); | ||
} | ||
export default new GraphQLScalarType({ | ||
@@ -24,7 +14,7 @@ name: 'PositiveFloat', | ||
serialize(value) { | ||
return processValue(value); | ||
return processValue(value, VALIDATIONS.PositiveFloat); | ||
}, | ||
parseValue(value) { | ||
return processValue(value); | ||
return processValue(value, VALIDATIONS.PositiveFloat); | ||
}, | ||
@@ -37,4 +27,4 @@ | ||
return processValue(ast.value); | ||
return processValue(ast.value, VALIDATIONS.PositiveFloat); | ||
}, | ||
}); |
@@ -5,14 +5,4 @@ import { GraphQLScalarType } from 'graphql'; | ||
function processValue(value) { | ||
if (isNaN(value)) { | ||
throw new TypeError(`Value is not a number: ${value}`); | ||
} | ||
import { processValue, VALIDATIONS } from './utilities'; | ||
if (!(value > 0)) { | ||
throw new TypeError(`Value is not a positive number: ${value}`); | ||
} | ||
return parseInt(value, 10); | ||
} | ||
export default new GraphQLScalarType({ | ||
@@ -24,7 +14,7 @@ name: 'PositiveInt', | ||
serialize(value) { | ||
return processValue(value); | ||
return processValue(value, VALIDATIONS.PositiveInt); | ||
}, | ||
parseValue(value) { | ||
return processValue(value); | ||
return processValue(value, VALIDATIONS.PositiveInt); | ||
}, | ||
@@ -37,4 +27,4 @@ | ||
return processValue(ast.value); | ||
return processValue(ast.value, VALIDATIONS.PositiveInt); | ||
}, | ||
}); |
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
98646
45
2120
232
12
1
- Removedgraphql@^0.10.1