Skip to content

Commit

Permalink
fix support in browser (kenspirit#32)
Browse files Browse the repository at this point in the history
  • Loading branch information
HardCoreQual authored Aug 14, 2022
1 parent 60a6107 commit ac819c9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 18 deletions.
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,14 @@ const jsonSchema = parse(joiSchema)
// const openApiSchema = parse(joiSchema, 'open-api')
```

## Browser support
For generating JSON Schema in a browser you should use next import for `joi` library, in order to create your joi schema:
```typescript
import Joi from 'joi/lib/index';
```

This restriction is because in a browser, `joi` don't have the method `describe` for minimize build size.

## Test

>npm run test
Expand Down
37 changes: 19 additions & 18 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
const cmp = require('semver-compare')
const fs = require('fs')
const path = require('path')

const convertorsDir = path.resolve(__dirname, './lib/convertors')
const parsersDir = path.resolve(__dirname, './lib/parsers')
const convertors = []
const parsers = {}
const c17 = require('./lib/convertors/v17')
const c16 = require('./lib/convertors/v16')
const c15 = require('./lib/convertors/v15')
const c14 = require('./lib/convertors/v14')
const c13 = require('./lib/convertors/v13')
const c12 = require('./lib/convertors/v12')

fs.readdirSync(convertorsDir).sort().reverse().forEach(file => {
if (file.endsWith('.js')) {
const convertor = require(`${convertorsDir}/${file}`)
convertors.push(convertor)
}
})
const JoiJsonSchemaParser = require('./lib/parsers/json')
const JoiOpenApiSchemaParser = require('./lib/parsers/open-api')
const JoiJsonDraftSchemaParser19 = require('./lib/parsers/json-draft-2019-09')
const JoiJsonDraftSchemaParser = require('./lib/parsers/json-draft-04')

fs.readdirSync(parsersDir).forEach(file => {
if (file.endsWith('.js')) {
const parser = require(`${parsersDir}/${file}`)
parsers[file.split('.')[0]] = parser
}
})
const convertors = [
c17, c16, c15, c14, c13, c12
]
const parsers = {
'json-draft-2019-09': JoiJsonDraftSchemaParser19,
'json-draft-4': JoiJsonDraftSchemaParser,
json: JoiJsonSchemaParser,
'open-api': JoiOpenApiSchemaParser
}

function parse(joiObj, type = 'json', definitions = {}) {
if (typeof joiObj.describe !== 'function') {
Expand Down

0 comments on commit ac819c9

Please sign in to comment.