-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add uuid type with primary key coercion #32
Changes from 6 commits
1fc1809
103c41d
775a6f4
886a263
8372610
8a2a167
e856169
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -50,6 +50,25 @@ var normalize = module.exports = { | |
else if (context.attributes[pk].type == 'string') { | ||
coercePK = function(pk) {return String(pk).toString();}; | ||
} | ||
|
||
// If the the data type is uuid (both according to the model definition | ||
// as well as the database schema) attempt to coerce the value to a | ||
// properly formatted uuid, stripping any extreneous characters from it. | ||
else if (context.attributes[pk].type == 'uuid' && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we use triple equals here and below? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That’d be my normal preference, though I was deferring to the existing style. I could go either way, but since the RHS is a string literal, I’m not super worried about it. |
||
context.schema && | ||
context.schema[pk] && | ||
context.schema[pk].type == 'uuid') { | ||
coercePK = function(pk) { | ||
var pattern = /[a-f0-9]{8}-[a-f0-9]{4}-4[a-f0-9]{3}-[89aAbB][a-f0-9]{3}-[a-f0-9]{12}/i | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Might be better to pull this to the top of the file so we don't need to redeclare the regex every time we match. |
||
var matches = pattern.exec(pk); | ||
var uuid = matches && matches[0]; | ||
if (uuid) { | ||
return uuid; | ||
} else { | ||
throw new TypeError("value '" + pk + "' cannot be coerced to UUID"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. String addition always works right? Like its impossible for this to throw a second error because In Python for example if you try to do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, it'll always work. If |
||
} | ||
}; | ||
} | ||
// If the data type is unspecified, return the key as-is | ||
else { | ||
coercePK = function(pk) {return pk;}; | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,4 +33,128 @@ describe("Normalize utility", function() { | |
|
||
}); | ||
|
||
}); | ||
describe(".expandPK()", function() { | ||
it("casts integers", function() { | ||
var context = { | ||
attributes: { | ||
id: { | ||
type: 'integer', | ||
primaryKey: true | ||
} | ||
} | ||
}; | ||
|
||
var options = { | ||
id: '123' | ||
} | ||
|
||
var result = normalize.expandPK(context, options); | ||
|
||
assert(result.id === 123); | ||
}); | ||
|
||
it("casts uuids", function() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can we add a test here if you try to pass like There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, and I'll check that an error gets thrown. |
||
var context = { | ||
attributes: { | ||
id: { | ||
type: 'uuid', | ||
primaryKey: true | ||
} | ||
}, | ||
schema: { | ||
id: { | ||
type: 'uuid' | ||
} | ||
} | ||
}; | ||
|
||
var options = { | ||
id: 'prefix_0b6c28e0-a117-4a9e-9a0d-60f0992edbee' | ||
} | ||
|
||
var result = normalize.expandPK(context, options); | ||
|
||
assert(result.id === '0b6c28e0-a117-4a9e-9a0d-60f0992edbee'); | ||
}); | ||
|
||
it("casts uuids with capitals", function() { | ||
var context = { | ||
attributes: { | ||
id: { | ||
type: 'uuid', | ||
primaryKey: true | ||
} | ||
}, | ||
schema: { | ||
id: { | ||
type: 'uuid' | ||
} | ||
} | ||
}; | ||
|
||
var options = { | ||
id: '0B6C28E0-A117-4A9E-9A0D-60F0992EDBEE' | ||
} | ||
|
||
var result = normalize.expandPK(context, options); | ||
|
||
assert(result.id === '0B6C28E0-A117-4A9E-9A0D-60F0992EDBEE'); | ||
}); | ||
|
||
it("does not cast uuids with an underlying non-uuid type", function() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh this is neat |
||
var context = { | ||
attributes: { | ||
id: { | ||
type: 'uuid', | ||
primaryKey: true | ||
} | ||
}, | ||
schema: { | ||
id: { | ||
type: 'text' | ||
} | ||
} | ||
}; | ||
|
||
var options = { | ||
id: 'prefix_0b6c28e0-a117-4a9e-9a0d-60f0992edbee' | ||
} | ||
|
||
var result = normalize.expandPK(context, options); | ||
|
||
assert(result.id === 'prefix_0b6c28e0-a117-4a9e-9a0d-60f0992edbee'); | ||
|
||
}); | ||
|
||
it("throws a TypeError when attempting to cast a non-uuid", function() { | ||
var context = { | ||
attributes: { | ||
id: { | ||
type: 'uuid', | ||
primaryKey: true | ||
} | ||
}, | ||
schema: { | ||
id: { | ||
type: 'uuid' | ||
} | ||
} | ||
}; | ||
|
||
var options = { | ||
id: 'hello' | ||
} | ||
|
||
try { | ||
normalize.expandPK(context, options); | ||
throw new Error('wrong error'); | ||
} catch (e) { | ||
assert(e); | ||
assert(e instanceof TypeError); | ||
assert(e.message === "value 'hello' cannot be coerced to UUID"); | ||
} | ||
|
||
}); | ||
}); | ||
|
||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
*extraneous