Skip to content

Commit

Permalink
30% faster startup and faster validation (#159)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina committed Apr 15, 2019
1 parent e5cf898 commit 02c5374
Show file tree
Hide file tree
Showing 4 changed files with 1,799 additions and 32 deletions.
21 changes: 21 additions & 0 deletions build-schema-validator.js
@@ -0,0 +1,21 @@
'use strict'

const Ajv = require('ajv')
const fs = require('fs')
const path = require('path')
const pack = require('ajv-pack')

const ajv = new Ajv({
sourceCode: true // this option is required by ajv-pack
})

const validate = ajv.compile(require('ajv/lib/refs/json-schema-draft-07.json'))

let moduleCode = `// This file is autogenerated by ${path.basename(__filename)}, do not edit
function nop () { return true }
${pack(ajv, validate).replace(/root\.refVal\[0\]/gm, 'nop')}
`

fs.writeFileSync(path.join(__dirname, 'schema-validator.js'), moduleCode)
54 changes: 22 additions & 32 deletions index.js
Expand Up @@ -2,22 +2,7 @@

var Ajv = require('ajv')
var merge = require('deepmerge')

// This Ajv instance is used to validate that the passed schema
// is valid json schema. We reuse the instance to avoid having to
// pay the ajv creation cost more than once.
var ajv = new Ajv({
// Ignore any unknown formats as they aren't used.
unknownFormats: 'ignore',

// Ignoring unknown formats emits warnings, but we don't need to hear about
// them.
logger: {
log: console.log,
warn: function () {},
error: console.error
}
})
var validate = require('./schema-validator')

var uglify = null
var isLong
Expand All @@ -34,9 +19,29 @@ var addComma = `
addComma = true
`

function isValidSchema (schema, name) {
if (!validate(schema)) {
if (name) {
name = `"${name}" `
} else {
name = ''
}
const first = validate.errors[0]
const err = new Error(`${name}schema is invalid: data${first.dataPath} ${first.message}`)
err.errors = isValidSchema.errors
throw err
}
}

function build (schema, options) {
options = options || {}
isValidSchema(schema, options.schema)
isValidSchema(schema)
if (options.schema) {
for (let key of Object.keys(options.schema)) {
isValidSchema(options.schema[key], key)
}
}

/* eslint no-new-func: "off" */
var code = `
'use strict'
Expand Down Expand Up @@ -985,21 +990,6 @@ function loadUglify () {
}
}

function isValidSchema (schema, externalSchema) {
if (externalSchema) {
Object.keys(externalSchema).forEach(key => {
try {
ajv.addSchema(externalSchema[key], key)
} catch (err) {
err.message = '"' + key + '" ' + err.message
throw err
}
})
}
ajv.compile(schema)
ajv.removeSchema()
}

function isEmpty (schema) {
for (var key in schema) {
if (schema.hasOwnProperty(key)) return false
Expand Down
6 changes: 6 additions & 0 deletions package.json
Expand Up @@ -31,6 +31,7 @@
},
"homepage": "https://github.com/fastify/fast-json-stringify#readme",
"devDependencies": {
"ajv-pack": "^0.3.1",
"benchmark": "^2.1.4",
"is-my-json-valid": "^2.19.0",
"long": "^4.0.0",
Expand All @@ -45,5 +46,10 @@
"dependencies": {
"ajv": "^6.8.1",
"deepmerge": "^3.0.0"
},
"standard": {
"ignore": [
"schema-validator.js"
]
}
}

0 comments on commit 02c5374

Please sign in to comment.