File tree Expand file tree Collapse file tree 2 files changed +42
-3
lines changed Expand file tree Collapse file tree 2 files changed +42
-3
lines changed Original file line number Diff line number Diff line change @@ -81,10 +81,18 @@ class Replicate {
81
81
* @returns {Promise<object> } - Resolves with the output of running the model
82
82
*/
83
83
async run ( identifier , options ) {
84
- const pattern =
85
- / ^ (?< owner > [ a - z A - Z 0 - 9 - _ ] + ?) \/ (?< name > [ a - z A - Z 0 - 9 - _ ] + ?) : (?< version > [ 0 - 9 a - f A - F ] + ) $ / ;
86
- const match = identifier . match ( pattern ) ;
84
+ // Define a pattern for owner and model names that allows
85
+ // letters, digits, and certain special characters.
86
+ // Example: "user123", "abc__123", "user.name"
87
+ const namePattern = / [ a - z A - Z 0 - 9 ] + (?: (?: [ . _ ] | _ _ | [ - ] * ) [ a - z A - Z 0 - 9 ] + ) * / ;
88
+
89
+ // Define a pattern for "owner/name:version" format with named capturing groups.
90
+ // Example: "user123/repo_a:1a2b3c"
91
+ const pattern = new RegExp (
92
+ `^(?<owner>${ namePattern . source } )/(?<name>${ namePattern . source } ):(?<version>[0-9a-fA-F]+)$`
93
+ ) ;
87
94
95
+ const match = identifier . match ( pattern ) ;
88
96
if ( ! match || ! match . groups ) {
89
97
throw new Error (
90
98
'Invalid version. It must be in the format "owner/name:version"'
Original file line number Diff line number Diff line change @@ -426,6 +426,37 @@ describe('Replicate client', () => {
426
426
) ;
427
427
expect ( output ) . toBe ( 'foobar' ) ;
428
428
} ) ;
429
+
430
+ test ( 'Does not throw an error for identifier containing hyphen and full stop' , async ( ) => {
431
+ nock ( BASE_URL )
432
+ . post ( '/predictions' )
433
+ . reply ( 200 , {
434
+ id : 'ufawqhfynnddngldkgtslldrkq' ,
435
+ status : 'processing' ,
436
+ } )
437
+ . get ( '/predictions/ufawqhfynnddngldkgtslldrkq' )
438
+ . reply ( 200 , {
439
+ id : 'ufawqhfynnddngldkgtslldrkq' ,
440
+ status : 'succeeded' ,
441
+ output : 'foobar' ,
442
+ } ) ;
443
+
444
+ await expect ( client . run ( 'a/b-1.0:abc123' , { input : { text : 'Hello, world!' } } ) ) . resolves . not . toThrow ( ) ;
445
+ } ) ;
446
+
447
+ test ( 'Throws an error for invalid identifiers' , async ( ) => {
448
+ const options = { input : { text : 'Hello, world!' } }
449
+
450
+ await expect ( client . run ( 'owner/model:invalid' , options ) ) . rejects . toThrow ( ) ;
451
+
452
+ // @ts -expect-error
453
+ await expect ( client . run ( 'owner:abc123' , options ) ) . rejects . toThrow ( ) ;
454
+
455
+ await expect ( client . run ( '/model:abc123' , options ) ) . rejects . toThrow ( ) ;
456
+
457
+ // @ts -expect-error
458
+ await expect ( client . run ( ':abc123' , options ) ) . rejects . toThrow ( ) ;
459
+ } ) ;
429
460
} ) ;
430
461
431
462
// Continue with tests for other methods
You can’t perform that action at this time.
0 commit comments