UNPKG

@eluvio/elv-js-helpers

Version:

A collection of Javascript helper functions used by several Eluvio libraries.

20 lines (17 loc) 1.23 kB
const TH = require('../../../test-helpers') const defRegexMatchedStrModel = TH.requireSrcFile('ModelFactory/defRegexMatchedStrModel') describe('defRegexMatchedStrModel', () => { it('should work as expected', () => { const UUIDStringModel = defRegexMatchedStrModel('UUIDString', /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/) UUIDStringModel('12345678-90ab-cdef-0123-4567890abcde').should.equal('12345678-90ab-cdef-0123-4567890abcde') TH.expect(() => UUIDStringModel('foo')).to.throw('Value is not in valid format or contains illegal characters (must match regular expression: /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/) (got: "foo")') TH.expect(() => UUIDStringModel(42)).to.throw('expecting String, got Number 42') // supply a nicer error message const UUIDStringModel2 = defRegexMatchedStrModel( 'UUIDString', /^[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12}$/, 'is not in UUID format "xxxxxxxx-xxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"' ) TH.expect(() => UUIDStringModel2('foo')).to.throw('Value is not in UUID format "xxxxxxxx-xxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" (got: "foo")') }) })