@@ -10,19 +10,72 @@ npm install openapi-static-validator -g
1010
1111## Usage
1212
13+ #### Compilation of the OpenAPI spec to JavaScript
14+
1315```
1416openapi-static-validator spec.json > validate.js
1517```
1618
19+ #### Use of the validation code
20+
21+ Then the validation can be imported in your code:
22+
23+ ``` ts
24+ import { validateRequest , RequestError } from ' ./validate' ;
25+
26+ const result = validateRequest ({
27+ path: ' say/hello' ,
28+ method: ' post' ,
29+ headers: {},
30+ query: {},
31+ body: {
32+ message: ' Hello world' ,
33+ },
34+ });
35+
36+ if (result instanceof RequestError ) {
37+ // Do something with the error
38+ } else {
39+ console .log (result );
40+ }
41+ ```
42+
43+ #### Custom string format
44+
45+ When using ` format ` for data of ` type: string ` , you need to define validators for them:
46+
47+ ``` ts
48+ import { validateRequest , ValidationError } from ' ./validate' ;
49+
50+ const result = validateRequest (
51+ {
52+ path: ' say/hello' ,
53+ method: ' post' ,
54+ headers: {},
55+ query: {},
56+ body: {
57+ message: ' Hello world' ,
58+ },
59+ },
60+ {
61+ stringFormats: {
62+ uri : (value , path ) =>
63+ value .startsWith (' https://' ) ? null : new ValidationError (path , ' Invalid url' ),
64+ },
65+ },
66+ );
67+ ```
68+
1769## TODOs
1870
1971- [ ] ` in: header ` and ` in: cookie ` parameters
2072- [ ] validation of ` type: integer ` , not just as ` number `
73+ - [ ] validation of response using a ` validateResponse ` function
2174
2275## Development
2376
2477To publish the package:
2578
2679- Change version in ` package.json `
2780- Commit the change and tag it
28- - Run ` npm publish `
81+ - Run ` npm run build ` and ` npm publish`
0 commit comments