@@ -3,7 +3,8 @@ import { expect } from 'chai';
33
44import { date , dateTime } from '../src/framework/base.serdes' ;
55import * as OpenApiValidator from '../src' ;
6- import { Ajv } from 'ajv' ;
6+ import { OpenAPIV3 } from '../src/framework/types' ;
7+ import Ajvs = OpenAPIV3 . Ajvs ;
78
89const apiSpecPath = path . join ( 'test' , 'resources' , 'serdes.yaml' ) ;
910
@@ -20,10 +21,25 @@ class ObjectID {
2021}
2122
2223describe ( 'ajv.return' , ( ) => {
23- let ajv : Ajv = null ;
24+ let ajvs : Ajvs = null ;
25+
26+ class ReqRes {
27+ id ?: string | ObjectID
28+ }
29+
30+ const customSchema = {
31+ type : 'object' ,
32+ properties : {
33+ id : {
34+ $ref : '#/components/schemas/ObjectId' ,
35+ } ,
36+ } ,
37+ required : [ 'id' ] ,
38+ additionalProperties : false ,
39+ } ;
2440
2541 before ( async ( ) => {
26- ajv = await OpenApiValidator . ajv ( {
42+ ajvs = await OpenApiValidator . ajv ( {
2743 apiSpec : apiSpecPath ,
2844 validateRequests : {
2945 coerceTypes : true
@@ -44,30 +60,80 @@ describe('ajv.return', () => {
4460 } ) ;
4561 } ) ;
4662
47- it ( 'should control BAD id format and throw an error' , async ( ) => {
48- class ReqClass {
49- id : string | ObjectID
50- }
51-
52- const req : ReqClass = {
63+ it ( 'should control request and deserialize string to object' , async ( ) => {
64+ const req : ReqRes = {
5365 id : '507f191e810c19729de860ea' ,
5466 }
5567
56- ajv . validate (
57- {
58- type : 'object' ,
59- properties : {
60- id : {
61- $ref : '#/components/schemas/ObjectId' ,
62- } ,
63- } ,
64- required : [ 'token' ] ,
65- additionalProperties : false ,
66- } ,
68+ const isValid = ajvs . req . validate (
69+ customSchema ,
6770 req
6871 ) ;
72+ expect ( isValid ) . to . be . equal ( true ) ;
73+ expect ( ajvs . req . errors ) . to . be . equal ( null ) ;
6974 expect ( req . id instanceof ObjectID ) . to . be . true ;
7075 } ) ;
76+
77+ it ( 'should control request and return error if id is not set' , async ( ) => {
78+ const req : ReqRes = {
79+ // No id but it is required
80+ // id : '507f191e810c19729de860ea',
81+ }
82+
83+ const isValid = ajvs . req . validate (
84+ customSchema ,
85+ req
86+ ) ;
87+ expect ( isValid ) . to . be . equal ( false ) ;
88+ expect ( ajvs . req . errors . length ) . to . be . equal ( 1 ) ;
89+ expect ( ajvs . req . errors [ 0 ] . message ) . to . be . equal ( 'should have required property \'id\'' ) ;
90+ } ) ;
91+
92+ it ( 'should control request and return error if id is in bad format' , async ( ) => {
93+ const req : ReqRes = {
94+ id : 'notAnObjectId' ,
95+ }
96+
97+ const isValid = ajvs . req . validate (
98+ customSchema ,
99+ req
100+ ) ;
101+ expect ( isValid ) . to . be . equal ( false ) ;
102+ expect ( ajvs . req . errors . length ) . to . be . equal ( 1 ) ;
103+ expect ( ajvs . req . errors [ 0 ] . message ) . to . be . equal ( 'should match pattern "^[0-9a-fA-F]{24}$"' ) ;
104+ } ) ;
105+
106+
107+ it ( 'should control response and serialize object to string' , async ( ) => {
108+ const res : ReqRes = {
109+ id : new ObjectID ( '507f191e810c19729de860ea' ) ,
110+ }
111+
112+ const isValid = ajvs . res . validate (
113+ customSchema ,
114+ res
115+ ) ;
116+ expect ( res . id ) . to . be . equal ( '507f191e810c19729de860ea' ) ;
117+ expect ( isValid ) . to . be . equal ( true ) ;
118+ expect ( ajvs . res . errors ) . to . be . equal ( null ) ;
119+ } ) ;
120+
121+ it ( 'should control response and return an error if id is not set' , async ( ) => {
122+
123+ const res : ReqRes = {
124+ // No id but it is required
125+ // id : '507f191e810c19729de860ea',
126+ //id : new ObjectID('507f191e810c19729de860ea'),
127+ }
128+
129+ const isValid = ajvs . res . validate (
130+ customSchema ,
131+ res
132+ ) ;
133+ expect ( isValid ) . to . be . equal ( false ) ;
134+ expect ( ajvs . res . errors . length ) . to . be . equal ( 1 ) ;
135+ expect ( ajvs . res . errors [ 0 ] . message ) . to . be . equal ( 'should have required property \'id\'' ) ;
136+ } ) ;
71137} ) ;
72138
73139
0 commit comments