Added custom schema validation formats
							parent
							
								
									62dacd3867
								
							
						
					
					
						commit
						fd0bbb83c9
					
				|  | @ -10,10 +10,22 @@ const addSchemas = () => { | ||||||
|         const fileData = JSON.parse(fs.readFileSync(file, 'utf-8')); |         const fileData = JSON.parse(fs.readFileSync(file, 'utf-8')); | ||||||
|         ajv.addSchema(fileData, path.basename(file, '.json')); |         ajv.addSchema(fileData, path.basename(file, '.json')); | ||||||
|     }); |     }); | ||||||
|  | 
 | ||||||
|  |     // Email format
 | ||||||
|  |     const emailRegex = /^([\w-.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([\w-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/; | ||||||
|  |     ajv.addFormat('emailAddress', emailRegex); | ||||||
|  | 
 | ||||||
|  |     // Amount format
 | ||||||
|  |     const amountRegex = /^[1-9]\d*(\.\d+)?$/; | ||||||
|  |     ajv.addFormat('amount', amountRegex); | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| const validateJson = (schema, json) => { | const validateJson = (schema, json) => { | ||||||
|     return ajv.validate(schema, json); |     const result = ajv.validate(schema, json); | ||||||
|  |     return{ | ||||||
|  |         result, | ||||||
|  |         errors: ajv.errors | ||||||
|  |     }; | ||||||
| }; | }; | ||||||
| 
 | 
 | ||||||
| module.exports = { | module.exports = { | ||||||
|  |  | ||||||
|  | @ -1,10 +1,10 @@ | ||||||
| { | { | ||||||
|     "$id": "newCustomer", |     "$id": "customer", | ||||||
|     "type": "object", |     "type": "object", | ||||||
|     "properties": { |     "properties": { | ||||||
|         "email": { |         "email": { | ||||||
|             "type": "string", |             "type": "string", | ||||||
|             "format": "email" |             "format": "emailAddress" | ||||||
|         }, |         }, | ||||||
|         "firstName": { |         "firstName": { | ||||||
|             "type": "string" |             "type": "string" | ||||||
|  | @ -12,7 +12,8 @@ | ||||||
|             "type": "string" |             "type": "string" | ||||||
|         }, |         }, | ||||||
|         "productPrice": { |         "productPrice": { | ||||||
|             "type": "number" |             "type": "number", | ||||||
|  |             "format": "amount" | ||||||
|         }, |         }, | ||||||
|         "productDescription": { |         "productDescription": { | ||||||
|             "type": "string" |             "type": "string" | ||||||
|  |  | ||||||
|  | @ -9,7 +9,8 @@ | ||||||
|             "type": "string" |             "type": "string" | ||||||
|         }, |         }, | ||||||
|         "productPrice": { |         "productPrice": { | ||||||
|             "type": "number" |             "type": "number", | ||||||
|  |             "format": "amount" | ||||||
|         }, |         }, | ||||||
|         "productDescription": { |         "productDescription": { | ||||||
|             "type": "string" |             "type": "string" | ||||||
|  |  | ||||||
		Loading…
	
		Reference in New Issue