Skip to content

Commit

Permalink
Updates based on the comments
Browse files Browse the repository at this point in the history
  • Loading branch information
dalcib committed Apr 12, 2021
1 parent 0463761 commit 84b1fdd
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 11 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,11 @@ By default, this plugin only compiles ["secure" schemas][secure]. This can be di

By default, only files with a `.schema` extension are compiled. If you have different requirements you can pass a Regexp to `filter` for the plugin to use.

### `formats`
### `addFormats`

Includes [ajv-formats](https://github.com/ajv-validator/ajv-formats). Default `true`.

### `options`
### `ajvOptions`

Custom [options](https://ajv.js.org/options.html) to be passed to Ajv constructor.

Expand Down
13 changes: 9 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,19 @@
const Ajv = require('ajv')
const standaloneCode = require('ajv/dist/standalone').default
const fs = require('fs')
const addFormats = require('ajv-formats')
const addAjvFormats = require('ajv-formats')

const ajvSecure = new Ajv({ strictTypes: false })
const isSchemaSecure = ajvSecure.compile(require('ajv/lib/refs/json-schema-secure.json'))

module.exports = ({ filter = /\.schema$/, secure = true, formats = true, options = {} } = {}) => {
const ajv = new Ajv({ code: { source: true }, ...options })
if (formats) addFormats(ajv)
module.exports = ({
filter = /\.schema$/,
secure = true,
addFormats = true,
ajvOptions = {}
} = {}) => {
const ajv = new Ajv({ code: { source: true }, ...ajvOptions })
if (addFormats) addAjvFormats(ajv)
return {
name: 'jsonschema',
setup (build) {
Expand Down
4 changes: 4 additions & 0 deletions test/avjoption.schema
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"type": "string",
"format": "xix"
}
54 changes: 49 additions & 5 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const Module = require('module')
const path = require('path')
const tape = require('tape')
const vm = require('vm')
Expand All @@ -6,7 +7,7 @@ const esbuild = require('esbuild')
const jsonschemaPlugin = require('..')

tape.test('passes schema', function (t) {
bundle('ok.js', undefined, function (err, src) {
bundle('ok.js', {}, function (err, src) {
if (err) {
t.fail(err)
}
Expand All @@ -23,7 +24,7 @@ tape.test('passes schema', function (t) {
})

tape.test('does not pass schema', function (t) {
bundle('fail.js', undefined, function (err, src) {
bundle('fail.js', {}, function (err, src) {
if (err) {
t.fail(err)
}
Expand Down Expand Up @@ -57,32 +58,69 @@ tape.test('using custom matcher', function (t) {
})

tape.test('secure schema by default', function (t) {
bundle('insecure.schema', { formats: true }, function (err, src) {
bundle('insecure.schema', {}, function (err, src) {
t.ok(err, 'does not compile insecure schema')
t.match(err[0].text, /is not secure/, 'has correct error message')
t.end()
})
})

tape.test('secure schema disabled via option', function (t) {
bundle('insecure.schema', { secure: false, formats: true }, function (err, src) {
bundle('insecure.schema', { secure: false }, function (err, src) {
t.notOk(err, 'does compile insecure schema when option is passed')
t.end()
})
})

tape.test('invalid schema', function (t) {
bundle('invalid.schema', undefined, function (err, src) {
bundle('invalid.schema', {}, function (err, src) {
t.ok(err, 'does not compile invalid schema')
t.match(err[0].text, /Error compiling and packing/, 'has correct error message')
t.end()
})
})

tape.test('addFormats by default', function (t) {
bundle('insecure.schema', { secure: false }, function (err, src) {
t.notOk(err, 'does compile schema with date-time format when no addFormats is passed')
t.end()
})
})

tape.test('addFormats disabled via option', function (t) {
bundle('insecure.schema', { secure: false, addFormats: false }, function (err, src) {
t.ok(err, 'does not compile schema with formats')
t.end()
})
})

tape.test('use custom ajvOptions', function (t) {
bundle(
'avjoption.schema',
{
secure: false,
ajvOptions: {
formats: { xix: /^(?:[a-z][a-z0-9+\-.]*:)(?:\/?\/)?[^\s]*$/i }
}
},
function (err, src) {
t.notOk(err, 'does compile schema with custom ajvOptions')
const validate = requireFromString(src)
validate('http://github.com')
t.notOk(validate.errors, 'does validate schema with custom format with a valid value')
validate('xxxxx')
t.ok(validate.errors, 'does validate schema with custom format with a not valid value')
t.match(validate.errors[0].message, /must match format "xix"/, 'has correct error message')
t.end()
}
)
})

function bundle (file, opts, callback) {
esbuild.build({
entryPoints: [path.join(__dirname, file)],
bundle: true,
format: 'cjs',
plugins: [jsonschemaPlugin(opts)],
write: false
})
Expand All @@ -92,3 +130,9 @@ function bundle (file, opts, callback) {
callback(errResult.errors)
})
}

function requireFromString (str) {
const m = new Module('', module.parent)
m._compile(str, '')
return m.exports
}

0 comments on commit 84b1fdd

Please sign in to comment.