11/* eslint-disable max-len, no-control-regex */
2- import errors from "./errors" ;
32import { JsonError , JsonSchema } from "../types" ;
43import { Draft } from "../draft" ;
54import validUrl from "valid-url" ;
@@ -48,7 +47,7 @@ const formatValidators: Record<
4847 // full-date from http://tools.ietf.org/html/rfc3339#section-5.6
4948 const matches = value . match ( matchDate ) ;
5049 if ( ! matches ) {
51- return errors . formatDateTimeError ( { value, pointer, schema } ) ;
50+ return draft . errors . formatDateTimeError ( { value, pointer, schema } ) ;
5251 }
5352 const year = + matches [ 1 ] ;
5453 const month = + matches [ 2 ] ;
@@ -63,7 +62,7 @@ const formatValidators: Record<
6362 ) {
6463 return undefined ;
6564 }
66- return errors . formatDateError ( { value, pointer, schema } ) ;
65+ return draft . errors . formatDateError ( { value, pointer, schema } ) ;
6766 } ,
6867
6968 "date-time" : ( draft , schema , value , pointer ) => {
@@ -72,11 +71,11 @@ const formatValidators: Record<
7271 }
7372 if ( value === "" || isValidDateTime . test ( value ) ) {
7473 if ( new Date ( value ) . toString ( ) === "Invalid Date" ) {
75- return errors . formatDateTimeError ( { value, pointer, schema } ) ;
74+ return draft . errors . formatDateTimeError ( { value, pointer, schema } ) ;
7675 }
7776 return undefined ;
7877 }
79- return errors . formatDateTimeError ( { value, pointer, schema } ) ;
78+ return draft . errors . formatDateTimeError ( { value, pointer, schema } ) ;
8079 } ,
8180
8281 email : ( draft , schema , value , pointer ) => {
@@ -85,20 +84,20 @@ const formatValidators: Record<
8584 }
8685 // taken from https://github.com/ExodusMovement/schemasafe/blob/master/src/formats.js
8786 if ( value [ 0 ] === '"' ) {
88- return errors . formatEmailError ( { value, pointer, schema } ) ;
87+ return draft . errors . formatEmailError ( { value, pointer, schema } ) ;
8988 }
9089 const [ name , host , ...rest ] = value . split ( "@" ) ;
9190 if ( ! name || ! host || rest . length !== 0 || name . length > 64 || host . length > 253 ) {
92- return errors . formatEmailError ( { value, pointer, schema } ) ;
91+ return draft . errors . formatEmailError ( { value, pointer, schema } ) ;
9392 }
9493 if ( name [ 0 ] === "." || name . endsWith ( "." ) || name . includes ( ".." ) ) {
95- return errors . formatEmailError ( { value, pointer, schema } ) ;
94+ return draft . errors . formatEmailError ( { value, pointer, schema } ) ;
9695 }
9796 if ( ! / ^ [ a - z 0 - 9 . - ] + $ / i. test ( host ) || ! / ^ [ a - z 0 - 9 . ! # $ % & ' * + / = ? ^ _ ` { | } ~ - ] + $ / i. test ( name ) ) {
98- return errors . formatEmailError ( { value, pointer, schema } ) ;
97+ return draft . errors . formatEmailError ( { value, pointer, schema } ) ;
9998 }
10099 if ( ! host . split ( "." ) . every ( ( part ) => / ^ [ a - z 0 - 9 ] ( [ a - z 0 - 9 - ] { 0 , 61 } [ a - z 0 - 9 ] ) ? $ / i. test ( part ) ) ) {
101- return errors . formatEmailError ( { value, pointer, schema } ) ;
100+ return draft . errors . formatEmailError ( { value, pointer, schema } ) ;
102101 }
103102 return undefined ;
104103 } ,
@@ -115,7 +114,7 @@ const formatValidators: Record<
115114 parseIdnEmail ( value ) ;
116115 return undefined ;
117116 } catch ( e ) {
118- return errors . formatEmailError ( { value, pointer, schema } ) ;
117+ return draft . errors . formatEmailError ( { value, pointer, schema } ) ;
119118 }
120119 } ,
121120
@@ -126,7 +125,7 @@ const formatValidators: Record<
126125 if ( value === "" || isValidHostname . test ( value ) ) {
127126 return undefined ;
128127 }
129- return errors . formatHostnameError ( { value, pointer, schema } ) ;
128+ return draft . errors . formatHostnameError ( { value, pointer, schema } ) ;
130129 } ,
131130
132131 ipv4 : ( draft , schema , value , pointer ) => {
@@ -135,12 +134,12 @@ const formatValidators: Record<
135134 }
136135 if ( value && value [ 0 ] === "0" ) {
137136 // leading zeroes should be rejected, as they are treated as octals
138- return errors . formatIPV4LeadingZeroError ( { value, pointer, schema } ) ;
137+ return draft . errors . formatIPV4LeadingZeroError ( { value, pointer, schema } ) ;
139138 }
140139 if ( value . length <= 15 && isValidIPV4 . test ( value ) ) {
141140 return undefined ;
142141 }
143- return errors . formatIPV4Error ( { value, pointer, schema } ) ;
142+ return draft . errors . formatIPV4Error ( { value, pointer, schema } ) ;
144143 } ,
145144
146145 ipv6 : ( draft , schema , value , pointer ) => {
@@ -149,12 +148,12 @@ const formatValidators: Record<
149148 }
150149 if ( value && value [ 0 ] === "0" ) {
151150 // leading zeroes should be rejected, as they are treated as octals
152- return errors . formatIPV6LeadingZeroError ( { value, pointer, schema } ) ;
151+ return draft . errors . formatIPV6LeadingZeroError ( { value, pointer, schema } ) ;
153152 }
154153 if ( value . length <= 45 && isValidIPV6 . test ( value ) ) {
155154 return undefined ;
156155 }
157- return errors . formatIPV6Error ( { value, pointer, schema } ) ;
156+ return draft . errors . formatIPV6Error ( { value, pointer, schema } ) ;
158157 } ,
159158
160159 "json-pointer" : ( draft , schema , value , pointer ) => {
@@ -164,7 +163,7 @@ const formatValidators: Record<
164163 if ( isValidJsonPointer . test ( value ) ) {
165164 return undefined ;
166165 }
167- return errors . formatJsonPointerError ( { value, pointer, schema } ) ;
166+ return draft . errors . formatJsonPointerError ( { value, pointer, schema } ) ;
168167 } ,
169168
170169 "relative-json-pointer" : ( draft , schema , value , pointer ) => {
@@ -174,7 +173,7 @@ const formatValidators: Record<
174173 if ( isValidRelativeJsonPointer . test ( value ) ) {
175174 return undefined ;
176175 }
177- return errors . formatJsonPointerError ( { value, pointer, schema } ) ;
176+ return draft . errors . formatJsonPointerError ( { value, pointer, schema } ) ;
178177 } ,
179178
180179 regex : ( draft , schema , value , pointer ) => {
@@ -184,13 +183,13 @@ const formatValidators: Record<
184183 return undefined ;
185184 } catch ( e ) { } // eslint-disable-line no-empty
186185
187- return errors . formatRegExError ( { value, pointer, schema } ) ;
186+ return draft . errors . formatRegExError ( { value, pointer, schema } ) ;
188187 }
189188 // v7 tests, ignore non-regex values
190189 if ( typeof value === "object" || typeof value === "number" || Array . isArray ( value ) ) {
191190 return undefined ;
192191 }
193- return errors . formatRegExError ( { value, pointer, schema } ) ;
192+ return draft . errors . formatRegExError ( { value, pointer, schema } ) ;
194193 } ,
195194
196195 // hh:mm:ss.sTZD
@@ -202,7 +201,7 @@ const formatValidators: Record<
202201 }
203202 // https://github.com/cfworker/cfworker/blob/main/packages/json-schema/src/format.ts
204203 const matches = value . match ( matchTime ) ;
205- return matches ? undefined : errors . formatDateTimeError ( { value, pointer, schema } ) ;
204+ return matches ? undefined : draft . errors . formatDateTimeError ( { value, pointer, schema } ) ;
206205 // if (!matches) {
207206 // return errors.formatDateTimeError({ value, pointer, schema });
208207 // }
@@ -227,7 +226,7 @@ const formatValidators: Record<
227226 if ( validUrl . isUri ( value ) ) {
228227 return undefined ;
229228 }
230- return errors . formatURIError ( { value, pointer, schema } ) ;
229+ return draft . errors . formatURIError ( { value, pointer, schema } ) ;
231230 } ,
232231
233232 "uri-reference" : ( draft , schema , value , pointer ) => {
@@ -237,7 +236,7 @@ const formatValidators: Record<
237236 if ( isValidURIRef . test ( value ) ) {
238237 return undefined ;
239238 }
240- return errors . formatURIReferenceError ( { value, pointer, schema } ) ;
239+ return draft . errors . formatURIReferenceError ( { value, pointer, schema } ) ;
241240 } ,
242241
243242 "uri-template" : ( draft , schema , value , pointer ) => {
@@ -247,14 +246,14 @@ const formatValidators: Record<
247246 if ( isValidURITemplate . test ( value ) ) {
248247 return undefined ;
249248 }
250- return errors . formatURITemplateError ( { value, pointer, schema } ) ;
249+ return draft . errors . formatURITemplateError ( { value, pointer, schema } ) ;
251250 } ,
252251
253252 url : ( draft , schema , value : string , pointer ) => {
254253 if ( value === "" || validUrl . isWebUri ( value ) ) {
255254 return undefined ;
256255 }
257- return errors . formatURLError ( { value, pointer, schema } ) ;
256+ return draft . errors . formatURLError ( { value, pointer, schema } ) ;
258257 }
259258} ;
260259
0 commit comments