Skip to content

Commit dfee5b3

Browse files
committed
Removed UUID v4.
We only want deterministic functions.
1 parent 8aab8a5 commit dfee5b3

3 files changed

Lines changed: 16 additions & 8 deletions

File tree

README.md

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Additionally, it adds the following functions:
2323
* [json_parse](#json_parse) - Parse a JSON string into a JSON object
2424
* [sha256](#sha256) - Calculate the SHA-256 hash of a string
2525
* [sha512](#sha512) - Calculate the SHA-512 hash of a string
26-
* [uuid](#uuid) - Generate a UUID
26+
* [uuid](#uuid) - Generate a UUID v5
2727
* [regex_test](#regex_test) - Test if a string matches a regular expression
2828
* [regex_match](#regex_match) - Return the first match of a regular expression in a string
2929
* [regex_match_all](#regex_match_all) - Return all matches of a regular expression in a string
@@ -310,16 +310,26 @@ uuid(name?, namespace?)
310310
```
311311
312312
**Description**:
313-
Generates a UUID. If `name` and (optionally) `namespace` are provided, generates a version 5 UUID; otherwise, generates a version 4 UUID.
313+
Generates a version 5 UUID.
314+
315+
UUID v5 is consistent. It creates a UUID based on the SHA hash of the input. This means that any given combination
316+
of input and namespace will result in the same UUID, every time.
314317
315318
**Example**:
316319
```jmespath
317-
uuid() // Random v4 UUID
318320
uuid('example') // v5 UUID
319321
uuid('example', '6ba7b810-9dad-11d1-80b4-00c04fd430c8') // v5 UUID with namespace
320322
```
321323
322324
`name` must be a string. Use `json_serialize()` to convert a JSON object to a string.
325+
`namespace` must be a UUID string. By default, it uses the NIL UUID.
326+
327+
The UUID RFC pre-defines four namespaces
328+
329+
* NameSpace_DNS: `6ba7b810-9dad-11d1-80b4-00c04fd430c8`
330+
* NameSpace_URL: `6ba7b811-9dad-11d1-80b4-00c04fd430c8`
331+
* NameSpace_OID: `6ba7b812-9dad-11d1-80b4-00c04fd430c8`
332+
* NameSpace_X500: `6ba7b814-9dad-11d1-80b4-00c04fd430c8`
323333
324334
### `regex_test`
325335
**Syntax**:

src/Runtime.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import type { TreeInterpreter } from './TreeInterpreter';
2828
import { sha256 } from '@noble/hashes/sha256';
2929
import { sha512 } from '@noble/hashes/sha512';
3030
import { bytesToHex } from '@noble/hashes/utils';
31-
import { NIL, v4 as uuidv4, v5 as uuidv5 } from 'uuid';
31+
import { NIL, v5 as uuidv5 } from 'uuid';
3232
import jsonStringify from './utils/json-serialize';
3333

3434
export enum InputArgument {
@@ -670,8 +670,8 @@ export class Runtime {
670670
return bytesToHex(sha512(inputValue));
671671
};
672672

673-
private functionUuid: RuntimeFunction<[string?, string?], string> = ([name, ns]) => {
674-
return name !== undefined ? uuidv5(name, ns ?? NIL) : uuidv4();
673+
private functionUuid: RuntimeFunction<[string, string?], string> = ([name, ns]) => {
674+
return uuidv5(name, ns ?? NIL);
675675
};
676676

677677
private parseRegexString(regexString: string): RegExp {
@@ -1192,7 +1192,6 @@ export class Runtime {
11921192
_signature: [
11931193
{
11941194
types: [InputArgument.TYPE_STRING],
1195-
optional: true,
11961195
},
11971196
{
11981197
types: [InputArgument.TYPE_STRING],

test/jmespath-functions.spec.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,6 @@ describe('Added functions', () => {
172172
});
173173

174174
it('uuid', () => {
175-
expect(search({}, 'uuid()')).toMatch(/^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/);
176175
expect(search({}, "uuid('example')")).toEqual('feb54431-301b-52bb-a6dd-e1e93e81bb9e');
177176
expect(search({}, "uuid('example', '6ba7b810-9dad-11d1-80b4-00c04fd430c8')")).toEqual(
178177
'7cb48787-6d91-5b9f-bc60-f30298ea5736',

0 commit comments

Comments
 (0)