-
Notifications
You must be signed in to change notification settings - Fork 138
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
215 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
'graphql-scalars': minor | ||
--- | ||
|
||
New `LocalDateTimeString` scalar |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,7 @@ export const UtcOffset = () => '+03:00'; | |
export const Duration = () => 'P3Y6M4DT12H30M5S'; | ||
export const LocalDate = () => '2020-07-19'; | ||
export const LocalTime = () => '08:45:59'; | ||
export const LocalDateTime = () => '2020-07-19T08:45:59'; | ||
export const LocalEndTime = () => '24:00:00'; | ||
export const EmailAddress = () => '[email protected]'; | ||
export const NegativeFloat = () => -123.45; | ||
|
@@ -47,7 +48,8 @@ const randomVal = (min: number, max: number) => { | |
}; | ||
// https://codepen.io/meowwwls/pen/jbEJRp | ||
export const HSL = () => `hsl(${randomVal(0, 360)}, ${randomVal(30, 95)}%, ${randomVal(30, 80)}%)`; | ||
export const HSLA = () => `hsla(${randomVal(0, 360)}, ${randomVal(30, 95)}%, ${randomVal(30, 80)}%, ${Math.random()})`; | ||
export const HSLA = () => | ||
`hsla(${randomVal(0, 360)}, ${randomVal(30, 95)}%, ${randomVal(30, 80)}%, ${Math.random()})`; | ||
|
||
export const IP = () => '2001:0db8:85a3:0000:0000:8a2e:0370:7334'; | ||
// https://stackoverflow.com/questions/43464519/creating-fake-ip-address-using-javascript | ||
|
@@ -63,10 +65,13 @@ export const IPv4 = () => | |
export const IPv6 = () => '2001:0db8:85a3:0000:0000:8a2e:0370:7334'; | ||
// http://jsfiddle.net/guest271314/qhbC9/ | ||
export const MAC = () => | ||
'XX:XX:XX:XX:XX:XX'.replace(/X/g, () => '0123456789ABCDEF'.charAt(Math.floor(Math.random() * 16))); | ||
'XX:XX:XX:XX:XX:XX'.replace(/X/g, () => | ||
'0123456789ABCDEF'.charAt(Math.floor(Math.random() * 16)), | ||
); | ||
export const Port = () => randomVal(0, 65535); | ||
export const RGB = () => `rgb(${randomVal(0, 255)}, ${randomVal(0, 255)}, ${randomVal(0, 255)})`; | ||
export const RGBA = () => `rgba(${randomVal(0, 255)}, ${randomVal(0, 255)}, ${randomVal(0, 255)}, ${Math.random()})`; | ||
export const RGBA = () => | ||
`rgba(${randomVal(0, 255)}, ${randomVal(0, 255)}, ${randomVal(0, 255)}, ${Math.random()})`; | ||
export const ISBN = () => `978-3-16-148410-0`; | ||
export const JWT = () => { | ||
// HEADER: { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
import { ASTNode, GraphQLScalarType, GraphQLScalarTypeConfig, Kind } from 'graphql'; | ||
import { createGraphQLError } from '../error.js'; | ||
|
||
const LOCAL_DATE_TIME_REGEX = | ||
/^((?:(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}:\d{2}(?:\.\d+)?))(Z|[\+-]\d{2}:\d{2})?)$/; | ||
|
||
function validateLocalDateTime(value: any, ast?: ASTNode): string { | ||
if (typeof value !== 'string') { | ||
throw createGraphQLError( | ||
`Value is not string: ${value}`, | ||
ast | ||
? { | ||
nodes: [ast], | ||
} | ||
: undefined, | ||
); | ||
} | ||
if (!LOCAL_DATE_TIME_REGEX.test(value.toUpperCase())) { | ||
throw createGraphQLError( | ||
`LocalDateTime cannot represent an invalid local date-time-string ${value}.`, | ||
ast | ||
? { | ||
nodes: [ast], | ||
} | ||
: undefined, | ||
); | ||
} | ||
const valueAsDate = new Date(value); | ||
const isValidDate = !isNaN(valueAsDate.getTime()); | ||
if (!isValidDate) { | ||
throw createGraphQLError( | ||
`Value is not a valid LocalDateTime: ${value}`, | ||
ast | ||
? { | ||
nodes: [ast], | ||
} | ||
: undefined, | ||
); | ||
} | ||
return value; | ||
} | ||
|
||
export const LocalDateTimeConfig: GraphQLScalarTypeConfig<string, string> = /*#__PURE__*/ { | ||
name: 'LocalDateTime', | ||
description: | ||
'A local date-time string (i.e., with no associated timezone) in `YYYY-MM-DDTHH:mm:ss` format, e.g. `2020-01-01T00:00:00`.', | ||
serialize(value) { | ||
return validateLocalDateTime(value); | ||
}, | ||
parseValue(value) { | ||
return validateLocalDateTime(value); | ||
}, | ||
parseLiteral(ast) { | ||
if (ast.kind !== Kind.STRING) { | ||
throw createGraphQLError( | ||
`Can only validate strings as local date-times but got a: ${ast.kind}`, | ||
{ | ||
nodes: [ast], | ||
}, | ||
); | ||
} | ||
return validateLocalDateTime(ast.value, ast); | ||
}, | ||
extensions: { | ||
codegenScalarType: | ||
// eslint-disable-next-line no-template-curly-in-string | ||
'`${number}${number}${number}${number}-${number}${number}-${number}${number}T${number}${number}:${number}${number}:${number}${number}`', | ||
jsonSchema: { | ||
title: 'LocalDateTime', | ||
type: 'string', | ||
format: 'date-time', | ||
}, | ||
}, | ||
}; | ||
|
||
export const GraphQLLocalDateTime = /*#__PURE__*/ new GraphQLScalarType(LocalDateTimeConfig); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
import { Kind } from 'graphql'; | ||
import { GraphQLLocalDateTime } from '../src'; | ||
|
||
const VALID_LOCAL_DATE_TIMES = [ | ||
'2016-02-01T00:00:15Z', | ||
'2016-02-01T00:00:00-11:00', | ||
'2017-01-07T11:25:00+01:00', | ||
'2017-01-07T00:00:00+01:20', | ||
'2016-02-01T00:00:00.1Z', | ||
'2016-02-01T00:00:00.000Z', | ||
'2016-02-01T00:00:00.990Z', | ||
'2016-02-01T00:00:00.23498Z', | ||
'2017-01-07T11:25:00.450+01:00', | ||
'2016-02-01t00:00:00.000z', | ||
]; | ||
|
||
describe(`LocalDateTime`, () => { | ||
describe('valid', () => { | ||
it('serialize', () => { | ||
VALID_LOCAL_DATE_TIMES.forEach(value => { | ||
expect(GraphQLLocalDateTime.serialize(value)).toEqual(value); | ||
}); | ||
}); | ||
|
||
it('parseValue', () => { | ||
VALID_LOCAL_DATE_TIMES.forEach(value => { | ||
expect(GraphQLLocalDateTime.parseValue(value)).toEqual(value); | ||
}); | ||
}); | ||
|
||
it('parseLiteral', () => { | ||
VALID_LOCAL_DATE_TIMES.forEach(value => { | ||
expect( | ||
GraphQLLocalDateTime.parseLiteral( | ||
{ | ||
value, | ||
kind: Kind.STRING, | ||
}, | ||
{}, | ||
), | ||
).toEqual(value); | ||
}); | ||
}); | ||
}); | ||
describe('invalid', () => { | ||
describe('not string', () => { | ||
it('serialize', () => { | ||
expect(() => GraphQLLocalDateTime.serialize(123)).toThrow(/Value is not string/); | ||
expect(() => GraphQLLocalDateTime.serialize(false)).toThrow(/Value is not string/); | ||
}); | ||
it('parseValue', () => { | ||
expect(() => GraphQLLocalDateTime.parseValue(123)).toThrow(/Value is not string/); | ||
expect(() => GraphQLLocalDateTime.parseValue(false)).toThrow(/Value is not string/); | ||
}); | ||
it('parseLiteral', () => { | ||
expect(() => | ||
GraphQLLocalDateTime.parseLiteral({ value: 123 as any, kind: Kind.INT }, {}), | ||
).toThrow(/Can only validate strings as local date-times but got a: IntValue/); | ||
expect(() => | ||
GraphQLLocalDateTime.parseLiteral({ value: false, kind: Kind.BOOLEAN }, {}), | ||
).toThrow(/Can only validate strings as local date-times but got a: BooleanValue/); | ||
}); | ||
}); | ||
describe('not a valid local date time string', () => { | ||
const invalidDateTime = '2015-02-24T00:00:00.000+0100'; | ||
it('serialize', () => { | ||
expect(() => GraphQLLocalDateTime.serialize(invalidDateTime)).toThrow( | ||
/LocalDateTime cannot represent an invalid local date-time-string/, | ||
); | ||
}); | ||
it('parseValue', () => { | ||
expect(() => GraphQLLocalDateTime.parseValue(invalidDateTime)).toThrow( | ||
/LocalDateTime cannot represent an invalid local date-time-string/, | ||
); | ||
}); | ||
it('parseLiteral', () => { | ||
expect(() => | ||
GraphQLLocalDateTime.parseLiteral({ value: invalidDateTime, kind: Kind.STRING }, {}), | ||
).toThrow(/LocalDateTime cannot represent an invalid local date-time-string/); | ||
}); | ||
}); | ||
describe('invalid date', () => { | ||
const invalidDateTime = '2017-13-46T11:25:00.450+01:00'; | ||
it('serialize', () => { | ||
expect(() => GraphQLLocalDateTime.serialize(invalidDateTime)).toThrow( | ||
/Value is not a valid LocalDateTime/, | ||
); | ||
}); | ||
it('parseValue', () => { | ||
expect(() => GraphQLLocalDateTime.parseValue(invalidDateTime)).toThrow( | ||
/Value is not a valid LocalDateTime/, | ||
); | ||
}); | ||
it('parseLiteral', () => { | ||
expect(() => | ||
GraphQLLocalDateTime.parseLiteral({ value: invalidDateTime, kind: Kind.STRING }, {}), | ||
).toThrow(/Value is not a valid LocalDateTime/); | ||
}); | ||
}); | ||
}); | ||
}); |