Skip to content

Commit

Permalink
cleanup: make jsonCheck an wrapper
Browse files Browse the repository at this point in the history
  • Loading branch information
ChALkeR committed Oct 9, 2022
1 parent 85297aa commit e62bfa6
Showing 1 changed file with 19 additions and 22 deletions.
41 changes: 19 additions & 22 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,33 @@
const genfun = require('./generate-function')
const { buildSchemas } = require('./pointer')
const { compile } = require('./compile')
const functions = require('./scope-functions')
const { hasOwn, deepEqual } = require('./scope-functions')

function writeJsonCheck(fun, validate, includeErrors) {
if (includeErrors) {
fun.write('if (!deepEqual(data, JSON.parse(JSON.stringify(data)))) {')
fun.write('validateIsJSON.errors = [{instanceLocation:"#",error:"not JSON compatible"}]')
fun.write('return false')
fun.write('}')
fun.write('const res = %s(data)', validate)
fun.write('validateIsJSON.errors = %s.errors', validate)
fun.write('return res')
} else {
fun.write('return deepEqual(data, JSON.parse(JSON.stringify(data))) && %s(data)', validate)
const jsonCheckWithErrors = (validate) =>
function validateIsJSON(data) {
if (!deepEqual(data, JSON.parse(JSON.stringify(data)))) {
validateIsJSON.errors = [{ instanceLocation: '#', error: 'not JSON compatible' }]
return false
}
const res = validate(data)
validateIsJSON.errors = validate.errors
return res
}
}

const jsonCheckWithoutErrors = (validate) => (data) =>
deepEqual(data, JSON.parse(JSON.stringify(data))) && validate(data)

const validator = (schema, { jsonCheck = false, isJSON = false, schemas = [], ...opts } = {}) => {
if (jsonCheck && isJSON) throw new Error('Can not specify both isJSON and jsonCheck options')
const options = { ...opts, schemas: buildSchemas(schemas, [schema]), isJSON: isJSON || jsonCheck }
const { scope, refs } = compile([schema], options) // only a single ref
if (opts.dryRun) return
const fun = genfun()
if (!jsonCheck) {
fun.write('%s', refs[0])
} else {
scope.deepEqual = functions.deepEqual
fun.write('function validateIsJSON(data) {')
writeJsonCheck(fun, refs[0], opts.includeErrors)
fun.write('}')
}
if (jsonCheck) {
scope.deepEqual = deepEqual
scope.jsonCheckWrap = opts.includeErrors ? jsonCheckWithErrors : jsonCheckWithoutErrors
fun.write('jsonCheckWrap(%s)', refs[0])
} else fun.write('%s', refs[0])
const validate = fun.makeFunction(scope)
validate.toModule = ({ semi = true } = {}) => fun.makeModule(scope) + (semi ? ';' : '')
validate.toJSON = () => schema
Expand Down Expand Up @@ -68,7 +65,7 @@ const parseWithoutErrors = (validate) => (src) => {

const parser = function(schema, opts = {}) {
// strong mode is default in parser
if (functions.hasOwn(opts, 'jsonCheck') || functions.hasOwn(opts, 'isJSON'))
if (hasOwn(opts, 'jsonCheck') || hasOwn(opts, 'isJSON'))
throw new Error('jsonCheck and isJSON options are not applicable in parser mode')
const validate = validator(schema, { mode: 'strong', ...opts, jsonCheck: false, isJSON: true })
const parseWith = opts.includeErrors ? parseWithErrors : parseWithoutErrors
Expand Down

0 comments on commit e62bfa6

Please sign in to comment.