Skip to content
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

Adds UUID Support #989

Merged
merged 3 commits into from
Sep 29, 2016
Merged

Adds UUID Support #989

merged 3 commits into from
Sep 29, 2016

Conversation

kamronbatman
Copy link
Contributor

@kamronbatman kamronbatman commented Sep 20, 2016

Adds UUID version 1 through 5 support. Based on #970.

@kamronbatman
Copy link
Contributor Author

I added proper bracket support, bracket/separator match support and support for options in the form of { versions: ['uuidv1','uuidv2','uuidv3','uuidv4','uuidv5'] }.
If no options are specified then it will validate exactly like the previous GUID check where brackets and separators are optional.

Help with testing would be appreciated! Thanks!

@eight-molecules
Copy link

In your tests, did you generate those UUIDs or copy a single one and modify its versioning? This comes up because doing so means you may be validating out-of-spec UUIDs (Mostly thinking about v1 and v2 since they rely on semi-fixed seed data).

@DavidTPate
Copy link
Contributor

DavidTPate commented Sep 22, 2016

From what I'm seeing within RFC 4122 it wouldn't matter how these are generated as they all validate to the same style (with the exception of the version number).

Here's the ABNF from page 4:

      UUID                   = time-low "-" time-mid "-"
                               time-high-and-version "-"
                               clock-seq-and-reserved
                               clock-seq-low "-" node
      time-low               = 4hexOctet
      time-mid               = 2hexOctet
      time-high-and-version  = 2hexOctet
      clock-seq-and-reserved = hexOctet
      clock-seq-low          = hexOctet
      node                   = 6hexOctet
      hexOctet               = hexDigit hexDigit
      hexDigit =
            "0" / "1" / "2" / "3" / "4" / "5" / "6" / "7" / "8" / "9" /
            "a" / "b" / "c" / "d" / "e" / "f" /
            "A" / "B" / "C" / "D" / "E" / "F"

Based on that document the validation of the output doesn't change from what I'm seeing but how that output is generated (ie. with MAC address, timestamps, etc.) is what changes. They still come out to the same format (with the exception of the version number).

}

// Matching braces
if (brackets[results[1]] !== results[11]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if they are eluding me or what but I'm not seeing any tests for mismatched braces.

}

return this.createError('string.guid', { value }, state, options);
// Matching separators
if (results[3] !== results[5] || results[3] !== results[7] || results[3] !== results[9]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wasn't able to find any tests for mismatches separators (not sure if they are hiding from me).

@@ -2983,7 +2983,74 @@ describe('string', () => {
['b4b2fb69c6244e5eb0698e0c6ec66618', true],
['{283B67B2-430F-4E6F-97E6-19041992-C1B0}', false, null, '"value" must be a valid GUID'],
['{D1A5279D-B27D-4CD4-A05E-EFDD53D08E8D', false, null, '"value" must be a valid GUID'],
['D1A5279D-B27D-4CD4-A05E-EFDD53D08E8D}', false, null, '"value" must be a valid GUID']
['D1A5279D-B27D-4CD4-A05E-EFDD53D08E8D}', false, null, '"value" must be a valid GUID'],
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are the tests I added for mismatched braces and separators.
The existing test above this (line 2985) is also a test for that.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have tests for hyphenated UUIDs without brackets? These are technically valid also, though they require all hyphens to be included.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, @kamronbatman I was meaning ones like [D1A5279D-B27D-4CD4-A05E-EFDD53D08E8D} where the braces don't match. I definitely saw the ones your referenced above.

Copy link
Contributor Author

@kamronbatman kamronbatman Sep 23, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gelliott181 There is already a test for that: line 2978. I could add them to each of the UUID versions, but it would technically be duplicative because of how the code is written. Let me know what you think.

@DavidTPate I see what you are saying. I can definitely add them in but based on how the code was written it is duplicatable. No brackets mixed with brackets are considered the same as mixing other types of brackets (parenths, braces, etc).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would expect the tests to verify that the functionality is working as expected, not that the code is working the way it was written.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, fair enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@gelliott181 and @DavidTPate I updated/added more tests. Let me know what you two think.

Thanks!

@DavidTPate
Copy link
Contributor

LGTM

@kamronbatman
Copy link
Contributor Author

@DavidTPate I added support for string value of the versions option. Also updated the API reference.

@kamronbatman
Copy link
Contributor Author

@Marsup I think this might be ready to merge! 🎉

@kamronbatman
Copy link
Contributor Author

kamronbatman commented Sep 23, 2016

@DavidTPate @gelliott181 and @Marsup - I updated the options, added assertions, and segregated the tests to follow in the way other Joi features were written. Should still be good in terms of tests/coverage.

versions = Hoek.unique(versions);
}

const regex = /^([\[{\(]?)([0-9A-F]{8})([:-]?)([0-9A-F]{4})([:-]?)([0-9A-F]{4})([:-]?)([0-9A-F]{4})([:-]?)([0-9A-F]{12})([\]}\)]?)$/i;

return this._test('guid', undefined, function (value, state, options) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add the options here instead of leaving undefined.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I fixed it appropriately. Let me know.

'uuidv5': '5'
};

let versions;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd rather have a const here and check for length later.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was following line 186. Not sure what you would suggest otherwise.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh I see, I guess the code can't always be consistent everywhere :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok, so are you thinking to move the checks to inside of the test callback? I am sorry for being confused on your thoughts.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was suggesting a const versions = [], but I guess we won't be able to do the unique then, I don't think it matters much.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I changed it to what you were asking. Let me know if that works and I will resquash.

@Marsup
Copy link
Collaborator

Marsup commented Sep 28, 2016

Are you able to squash all those commits as well ?

@kamronbatman
Copy link
Contributor Author

I just squashed it. And yes, I did not make a const because of the unique check.

@DavidTPate
Copy link
Contributor

@Marsup Not sure if you know about it or just prefer it be done differently but wanted to point out that you can now squash on merge for a PR in GitHub (you can also rebase too as of this week prior to merge). You can see the announcement for more details.

@Marsup
Copy link
Collaborator

Marsup commented Sep 29, 2016

I know about it, they did it wrong. I want squashed commits but I still want the merge commit, none of the options work for me.

@DavidTPate
Copy link
Contributor

Roger that, @Marsup it definitely doesn't add a merge commit like you point out. Just wanted to see if it would work for you.

@Marsup Marsup added the feature New functionality or improvement label Sep 29, 2016
@Marsup Marsup self-assigned this Sep 29, 2016
@Marsup Marsup merged commit 1dd3d94 into hapijs:master Sep 29, 2016
@Marsup
Copy link
Collaborator

Marsup commented Sep 29, 2016

Thanks a lot for the work y'all, I'll release in a bit as I need to add another small thing to it.

@Marsup Marsup added this to the 9.1.0 milestone Sep 29, 2016
@eight-molecules
Copy link

Nice PR @kamronbatman, thanks for taking my minor change so much further!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature New functionality or improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants