1
1
/* eslint-disable max-len, no-control-regex */
2
2
import errors from "./errors" ;
3
3
import validUrl from "valid-url" ;
4
+ import { parse as parseIdnEmail } from "smtp-address-parser" ;
4
5
// referenced
5
6
// https://github.com/cfworker/cfworker/blob/main/packages/json-schema/src/format.ts
6
7
// https://gist.github.com/marcelotmelo/b67f58a08bee6c2468f8
@@ -55,10 +56,10 @@ const formatValidators = {
55
56
return errors . formatDateTimeError ( { value, pointer, schema } ) ;
56
57
} ,
57
58
email : ( draft , schema , value , pointer ) => {
58
- // taken from https://github.com/ExodusMovement/schemasafe/blob/master/src/formats.js
59
59
if ( typeof value !== "string" || value === "" ) {
60
60
return undefined ;
61
61
}
62
+ // taken from https://github.com/ExodusMovement/schemasafe/blob/master/src/formats.js
62
63
if ( value [ 0 ] === '"' ) {
63
64
return errors . formatEmailError ( { value, pointer, schema } ) ;
64
65
}
@@ -77,6 +78,22 @@ const formatValidators = {
77
78
}
78
79
return undefined ;
79
80
} ,
81
+ /**
82
+ * @draft 7
83
+ * [RFC6531] https://json-schema.org/draft-07/json-schema-validation.html#RFC6531
84
+ */
85
+ "idn-email" : ( draft , schema , value , pointer ) => {
86
+ if ( typeof value !== "string" || value === "" ) {
87
+ return undefined ;
88
+ }
89
+ try {
90
+ parseIdnEmail ( value ) ;
91
+ return undefined ;
92
+ }
93
+ catch ( e ) {
94
+ return errors . formatEmailError ( { value, pointer, schema } ) ;
95
+ }
96
+ } ,
80
97
hostname : ( draft , schema , value , pointer ) => {
81
98
if ( typeof value !== "string" ) {
82
99
return undefined ;
0 commit comments