Add string validator & tests for UUIDv4

pull/10616/head
Jared Scheib 2018-03-14 18:31:09 -07:00
parent a4cf6276f5
commit a81fa3c01e
2 changed files with 30 additions and 0 deletions

View File

@ -0,0 +1,6 @@
// uses Gajus' regex matcher from https://stackoverflow.com/questions/136505/searching-for-uuids-in-text-with-regex
export const isUUIDv4 = str =>
str.length === 36 &&
!!str.match(
/[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}/
)

View File

@ -0,0 +1,24 @@
import uuid from 'uuid'
import {isUUIDv4} from 'src/utils/stringValidators'
describe('isUUIDv4', () => {
it('returns false for non-matches', () => {
const inputWrongLength = 'abcd'
expect(isUUIDv4(inputWrongLength)).toEqual(false)
const inputWrongFormat = 'abcdefghijklmnopqrstuvwxyz1234567890'
expect(isUUIDv4(inputWrongFormat)).toEqual(false)
const inputWrongCharRange = 'z47ac10b-58cc-4372-a567-0e02b2c3d479'
expect(isUUIDv4(inputWrongCharRange)).toEqual(false)
})
it('returns true for matches', () => {
const inputRight = 'a47ac10b-58cc-4372-a567-0e02b2c3d479'
expect(isUUIDv4(inputRight)).toEqual(true)
const inputRightFromLibrary = uuid.v4()
expect(isUUIDv4(inputRightFromLibrary)).toEqual(true)
})
})