Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move validation of names into
GraphQL*
constructors (#3288)
- Loading branch information
1 parent
22b9504
commit 5774173
Showing
12 changed files
with
257 additions
and
162 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
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,71 @@ | ||
import { expect } from 'chai'; | ||
import { describe, it } from 'mocha'; | ||
|
||
import { assertName, assertEnumValueName } from '../assertName'; | ||
import { assertValidName } from '../../utilities/assertValidName'; | ||
|
||
describe('assertValidName()', () => { | ||
assertValidName('test'); | ||
it('passthrough valid name', () => { | ||
expect(assertName('_ValidName123')).to.equal('_ValidName123'); | ||
}); | ||
|
||
it('throws for non-strings', () => { | ||
// @ts-expect-error | ||
expect(() => assertName({})).to.throw('Expected name to be a string.'); | ||
}); | ||
|
||
it('throws on empty strings', () => { | ||
expect(() => assertName('')).to.throw( | ||
'Expected name to be a non-empty string.', | ||
); | ||
}); | ||
|
||
it('throws for names with invalid characters', () => { | ||
expect(() => assertName('>--()-->')).to.throw( | ||
'Names must only contain [_a-zA-Z0-9] but ">--()-->" does not.', | ||
); | ||
}); | ||
|
||
it('throws for names starting with invalid characters', () => { | ||
expect(() => assertName('42MeaningsOfLife')).to.throw( | ||
'Names must start with [_a-zA-Z] but "42MeaningsOfLife" does not.', | ||
); | ||
}); | ||
}); | ||
|
||
describe('assertEnumValueName()', () => { | ||
it('passthrough valid name', () => { | ||
expect(assertEnumValueName('_ValidName123')).to.equal('_ValidName123'); | ||
}); | ||
|
||
it('throws on empty strings', () => { | ||
expect(() => assertEnumValueName('')).to.throw( | ||
'Expected name to be a non-empty string.', | ||
); | ||
}); | ||
|
||
it('throws for names with invalid characters', () => { | ||
expect(() => assertEnumValueName('>--()-->')).to.throw( | ||
'Names must only contain [_a-zA-Z0-9] but ">--()-->" does not.', | ||
); | ||
}); | ||
|
||
it('throws for names starting with invalid characters', () => { | ||
expect(() => assertEnumValueName('42MeaningsOfLife')).to.throw( | ||
'Names must start with [_a-zA-Z] but "42MeaningsOfLife" does not.', | ||
); | ||
}); | ||
|
||
it('throws for restricted names', () => { | ||
expect(() => assertEnumValueName('true')).to.throw( | ||
'Enum values cannot be named: true', | ||
); | ||
expect(() => assertEnumValueName('false')).to.throw( | ||
'Enum values cannot be named: false', | ||
); | ||
expect(() => assertEnumValueName('null')).to.throw( | ||
'Enum values cannot be named: null', | ||
); | ||
}); | ||
}); |
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
Oops, something went wrong.