File tree Expand file tree Collapse file tree 2 files changed +15
-4
lines changed Expand file tree Collapse file tree 2 files changed +15
-4
lines changed Original file line number Diff line number Diff line change @@ -25,6 +25,10 @@ describe('SafeUrlSchema', () => {
25
25
expect ( ( ) => SafeUrlSchema . parse ( 'not-a-url' ) ) . toThrow ( ) ;
26
26
expect ( ( ) => SafeUrlSchema . parse ( '' ) ) . toThrow ( ) ;
27
27
} ) ;
28
+
29
+ it ( 'works with safeParse' , ( ) => {
30
+ expect ( ( ) => SafeUrlSchema . safeParse ( 'not-a-url' ) ) . not . toThrow ( ) ;
31
+ } ) ;
28
32
} ) ;
29
33
30
34
describe ( 'OAuthMetadataSchema' , ( ) => {
Original file line number Diff line number Diff line change @@ -4,10 +4,17 @@ import { z } from "zod";
4
4
* Reusable URL validation that disallows javascript: scheme
5
5
*/
6
6
export const SafeUrlSchema = z . string ( ) . url ( )
7
- . refine (
8
- ( url ) => URL . canParse ( url ) ,
9
- { message : "URL must be parseable" }
10
- ) . refine (
7
+ . superRefine ( ( val , ctx ) => {
8
+ if ( ! URL . canParse ( val ) ) {
9
+ ctx . addIssue ( {
10
+ code : z . ZodIssueCode . custom ,
11
+ message : "URL must be parseable" ,
12
+ fatal : true ,
13
+ } ) ;
14
+
15
+ return z . NEVER ;
16
+ }
17
+ } ) . refine (
11
18
( url ) => {
12
19
const u = new URL ( url ) ;
13
20
return u . protocol !== 'javascript:' && u . protocol !== 'data:' && u . protocol !== 'vbscript:' ;
You can’t perform that action at this time.
0 commit comments