forked from Urigo/graphql-scalars
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
(MongoDB) ObjectID Scalar Type/Commit message convention (Urigo#243)
* feat(objectid-scalar): mongodb object id scalar type Adds mongodb object id scalar type. * style(editorconfig): editorconfig to help format code Adds [editorconfig](https://editorconfig.org/) to helps maintain coding style for developers. * style(commitlint): git commit message linting Enforces commit message standards on subsequent contributors to this project. * docs(mongodb-objectid): documents the usage of the mongodb objectid Documents the usage of the mongodb objectid scalar type. * feat(mongodb-objectid): mongodb objectid scalar type Adds mongodb objectid scalar type * feat(mongodb-objectid): mongodb objectid scalar type Adds a mongodb objectid scalar type definition to the already existing list of definitions. * conflict resolution (Urigo#1) package.json rebased * feat(objectid-scalar): mongodb object id scalar type Adds mongodb object id scalar type. * docs(mongodb-objectid): documents the usage of the mongodb objectid Documents the usage of the mongodb objectid scalar type. * git rebase (Urigo#2) conflict resolution -package.json * feat(objectid-scalar): mongodb object id scalar type Adds mongodb object id scalar type. * style(editorconfig): editorconfig to help format code Adds [editorconfig](https://editorconfig.org/) to helps maintain coding style for developers. * style(commitlint): git commit message linting Enforces commit message standards on subsequent contributors to this project. * docs(mongodb-objectid): documents the usage of the mongodb objectid Documents the usage of the mongodb objectid scalar type. * feat(mongodb-objectid): mongodb objectid scalar type Adds a mongodb objectid scalar type definition to the already existing list of definitions. * build: remove package-lock.json
- Loading branch information
Showing
11 changed files
with
197 additions
and
14 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,8 @@ | ||
## General | ||
[*] | ||
indent_style=space | ||
indent_size=2 | ||
end_of_line=lf | ||
charset=utf-8 | ||
trim_trailing_whitespace=true | ||
insert_final_newline=true |
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,9 @@ | ||
Issues and Pull Requests are always welcome. | ||
|
||
Please read OK Grow's global [contribution guidelines](https://github.com/okgrow/guides/blob/master/open-source/contributing.md). | ||
|
||
If you are interested in becoming a maintainer, get in touch with us by sending an email or opening an issue. You should already have code merged into the project. Active contributors are encouraged to get in touch. | ||
|
||
Please note that all interactions in @okgrow's repos should follow our [Code of Conduct](https://github.com/okgrow/guides/blob/master/open-source/CODE_OF_CONDUCT.md). | ||
|
||
All commit messages should follow the [conventional commit](https://www.conventionalcommits.org/en/v1.0.0/) message guidelines. In order to assist with this you can run `npm run-scripts commit` as a command has been added to the scripts section of the `package.json` file which will help guide formulation of a proper commit message. |
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,3 @@ | ||
module.exports = { | ||
extends: ['@commitlint/config-conventional'], | ||
}; |
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,41 @@ | ||
import { ValueNode } from 'graphql/language'; | ||
import { Kind, GraphQLError, GraphQLScalarType } from 'graphql'; | ||
|
||
const MONGODB_OBJECTID_REGEX: RegExp = new RegExp(/^[A-Fa-f0-9]{24}$/); | ||
|
||
export default new GraphQLScalarType({ | ||
name: 'ObjectID', | ||
|
||
description: | ||
'A field whose value conforms with the standard mongodb object ID as described here: https://docs.mongodb.com/manual/reference/method/ObjectId/#ObjectId. Example: 5e5677d71bdc2ae76344968c', | ||
|
||
serialize(value: string) { | ||
if (!MONGODB_OBJECTID_REGEX.test(value)) { | ||
throw new TypeError(`Value is not a valid mongodb object id of form: ${value}`); | ||
} | ||
|
||
return value; | ||
}, | ||
|
||
parseValue(value: string) { | ||
if (!MONGODB_OBJECTID_REGEX.test(value)) { | ||
throw new TypeError(`Value is not a valid mongodb object id of form: ${value}`); | ||
} | ||
|
||
return value; | ||
}, | ||
|
||
parseLiteral(ast: ValueNode) { | ||
if (ast.kind !== Kind.STRING) { | ||
throw new GraphQLError( | ||
`Can only validate strings as mongodb object id but got a: ${ast.kind}`, | ||
); | ||
} | ||
|
||
if (!MONGODB_OBJECTID_REGEX.test(ast.value)) { | ||
throw new TypeError(`Value is not a valid mongodb object id of form: ${ast.value}`); | ||
} | ||
|
||
return ast.value; | ||
}, | ||
}); |
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,94 @@ | ||
/* global describe, test, expect */ | ||
|
||
import { Kind } from 'graphql/language'; | ||
|
||
import ObjectID from '../src/resolvers/ObjectID'; | ||
|
||
describe('ObjectId', () => { | ||
describe('valid', () => { | ||
test('serialize', () => { | ||
expect(ObjectID.serialize('5e5677d71bdc2ae76344968c')).toBe('5e5677d71bdc2ae76344968c'); | ||
}); | ||
|
||
test('parseValue', () => { | ||
expect(ObjectID.parseValue('5e5677d71bdc2ae76344968c')).toBe('5e5677d71bdc2ae76344968c'); | ||
}); | ||
|
||
test('parseLiteral', () => { | ||
expect( | ||
ObjectID.parseLiteral({ value: '5e5677d71bdc2ae76344968c', kind: Kind.STRING }, undefined), // undefined as prescribed by the Maybe<T> type | ||
).toBe('5e5677d71bdc2ae76344968c'); | ||
}); | ||
}); | ||
|
||
describe('invalid', () => { | ||
describe('not a mongodb object id', () => { | ||
test('serialize', () => { | ||
const invalid = '5e5677d71bdc2ae76344968z'; | ||
expect(() => ObjectID.serialize(invalid)).toThrow( | ||
new RegExp(`Value is not a valid mongodb object id of form: ${invalid}`), | ||
); | ||
}); | ||
|
||
test('parseValue', () => { | ||
const invalid = '5e5677d71bdc2ae76344968z'; | ||
expect(() => ObjectID.parseValue(invalid)).toThrow( | ||
new RegExp(`Value is not a valid mongodb object id of form: ${invalid}`), | ||
); | ||
}); | ||
|
||
test('parseLiteral', () => { | ||
const invalid = '5e5677d71bdc2ae76344968z'; | ||
expect(() => | ||
ObjectID.parseLiteral({ value: invalid, kind: Kind.STRING }, undefined), // undefined as prescribed by the Maybe<T> type | ||
).toThrow(new RegExp(`Value is not a valid mongodb object id of form: ${invalid}`)); | ||
}); | ||
}); | ||
|
||
describe('too short', () => { | ||
test('serialize', () => { | ||
const invalid = '5e5677d71bdc2ae'; | ||
expect(() => ObjectID.serialize(invalid)).toThrow( | ||
new RegExp(`Value is not a valid mongodb object id of form: ${invalid}`), | ||
); | ||
}); | ||
|
||
test('parseValue', () => { | ||
const invalid = '5e5677d71bdc2ae'; | ||
expect(() => ObjectID.parseValue(invalid)).toThrow( | ||
new RegExp(`Value is not a valid mongodb object id of form: ${invalid}`), | ||
); | ||
}); | ||
|
||
test('parseLiteral', () => { | ||
const invalid = '5e5677d71bdc2ae'; | ||
expect(() => | ||
ObjectID.parseLiteral({ value: invalid, kind: Kind.STRING }, undefined), // undefined as prescribed by the Maybe<T> type | ||
).toThrow(new RegExp(`Value is not a valid mongodb object id of form: ${invalid}`)); | ||
}); | ||
}); | ||
|
||
describe('too long', () => { | ||
test('serialize', () => { | ||
const invalid = '5e5677d71bdc2ae76344968c5'; | ||
expect(() => ObjectID.serialize(invalid)).toThrow( | ||
new RegExp(`Value is not a valid mongodb object id of form: ${invalid}`), | ||
); | ||
}); | ||
|
||
test('parseValue', () => { | ||
const invalid = '5e5677d71bdc2ae76344968c5'; | ||
expect(() => ObjectID.parseValue(invalid)).toThrow( | ||
new RegExp(`Value is not a valid mongodb object id of form: ${invalid}`), | ||
); | ||
}); | ||
|
||
test('parseLiteral', () => { | ||
const invalid = '5e5677d71bdc2ae76344968c5'; | ||
expect(() => | ||
ObjectID.parseLiteral({ value: invalid, kind: Kind.STRING }, undefined), // undefined as prescribed by the Maybe<T> type | ||
).toThrow(new RegExp(`Value is not a valid mongodb object id of form: ${invalid}`)); | ||
}); | ||
}); | ||
}); | ||
}); |