Coder Social home page Coder Social logo

ajv-draft-04's Introduction

ajv-draft-04

Ajv with JSON Schema draft-04 support

build npm coverage Gitter GitHub Sponsors

Usage

You need to install both ajv and this package (to allow upgrading ajv without upgrading this package):

npm i ajv ajv-draft-04
// ESM/TypeScript import
import Ajv from "ajv-draft-04"
// Node.js require:
const Ajv = require("ajv-draft-04")

const ajv = new Ajv()

See ajv site for documentation.

Tests

npm install
git submodule update --init
npm test

License

MIT

ajv-draft-04's People

Contributors

epoberezkin avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

ajv-draft-04's Issues

ajv-formats for draft-04.

Heya,

I've just been caught out by some format validation on my draft-04 spec.

format: date was introduced in draft-7 - but was not present in draft-4.
format: date-time appears to not have been introduced in draft-6 or draft-7 - but I can't figure out where to find it originally defined.

Reading through the AJV docs; due to the problems inherent with format, it's been moved to ajv-formats as a standalone library.

Appreciate this may be a noddy question, but how would I work out which of those formats were compatible with draft-04 (as date-time appears to be valid, but date and time individually are not)?

Happy to assist if the job is reading through a spec and workout how to leverage a subset of ajv-formats that is draft-04 specific.

Error while using ajv-draft-04: Cannot find module ajv/dist/core

Receiving the following error when using this module:

Error: Cannot find module 'ajv/dist/core'
at Function.Module._resolveFilename (internal/modules/cjs/loader.js:636:15)
at Function.Module._load (internal/modules/cjs/loader.js:562:25)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object. (node_modules/ajv-draft-04/dist/index.js:4:16)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object. (node_modules/openapi-to-postmanv2/lib/ajValidation/ajvValidatorDraft04.js:1:13)
at Module._compile (internal/modules/cjs/loader.js:778:30)
at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
at Module.load (internal/modules/cjs/loader.js:653:32)
at tryModuleLoad (internal/modules/cjs/loader.js:593:12)
at Function.Module._load (internal/modules/cjs/loader.js:585:3)
at Module.require (internal/modules/cjs/loader.js:692:17)
at require (internal/modules/cjs/helpers.js:25:18)
at Object. (node_modules/openapi-to-postmanv2/lib/ajValidation/ajvValidation.js:6:34)
at Module._compile (internal/modules/cjs/loader.js:778:30)

Check the highlighter line in the error.

Error: Schema with key or id already exists

const Ajv = require('ajv-draft-04')
const ajvJson4Schema = require('../schemas/json-schema-draft-04.json')


let _ajvOptions = {
    strict: false,
    strictSchema: true,
    strictTypes: true,
    verbose: true,
    allErrors: true
}

let _draft4Validator = null
   let schema = {
	"$schema": "http://json-schema.org/draft-04/schema#",
	"title": "ActivityLogs",
	"description": "Activity Logs for a specific associate",
	"type": "object",
	"properties": {
		"activityLog": {
                    "type": "string"
                 }
         }
   }

    let results

    try {

        if (_draft4Validator == null) {
            let ajv = new Ajv(_ajvOptions)

            _draft4Validator = ajv.compile(ajvJson4Schema)
        }

        let valid = _draft4Validator(schema)

        results = {
            valid: valid,
            error: _draft4Validator.errors
        }

    } catch (err) {
        results = {
            valid: false,
            error: err
        }
    }
$ node index.js 
{
  valid: false,
  Exception has occurred: Error: schema with key or id "http://json-schema.org/draft-04/schema" already exists
  at Ajv._checkUnique (c:\Users\fieljere\ajvtest\node_modules\ajv\dist\core.js:465:19)
    at Ajv._addSchema (c:\Users\fieljere\ajvtest\node_modules\ajv\dist\core.js:456:22)
    at Ajv.compile (c:\Users\fieljere\ajvtest\node_modules\ajv\dist\core.js:158:26)
    at Object.<anonymous> (c:\Users\fieljere\ajvtest\index.js:34:28)
    at Module._compile (node:internal/modules/cjs/loader:1103:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1155:10)
    at Module.load (node:internal/modules/cjs/loader:981:32)
    at Function.Module._load (node:internal/modules/cjs/loader:822:12)
    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:77:12)
    at node:internal/main/run_main_module:17:47

I'm trying to import this package into the latest version of ajv and when I execute it, i'm receiving an error for schema key or id already exists
I don't have id used in all of my schemas and even when it is present, with different values, the error persists.

Can you please help me with this error?

Thanks so much!

peerDependency hell

I have a relatively complicated enterprise environment where I am writing some reusable code in my own library, which in turn has multiple dependencies, with multiple places that use AJV. One in particular is @seriousme/openapi-schema-validator - which has the following "simple" dependencies: https://github.com/seriousme/openapi-schema-validator/blob/master/package.json#L10

  "dependencies": {
    "ajv": "^8.6.0",
    "ajv-draft-04": "^1.0.0",
    "js-yaml": "^4.1.0"
  }

When launching an application using my library, though, I get the following error:

internal/modules/cjs/loader.js:883
  throw err;
  ^

Error: Cannot find module 'ajv/dist/core'
Require stack:
- /home/rolf/bmix/myapp/node_modules/ajv-draft-04/dist/index.js
- /home/rolf/bmix/myapp/node_modules/@seriousme/openapi-schema-validator/index.js
- /home/rolf/bmix/myapp/node_modules/fastify-openapi-glue/lib/parser.js
- /home/rolf/bmix/myapp/node_modules/fastify-openapi-glue/index.js

It's quite clear, that AJV 8.6.0 has ajv/dist/core, though, BUT, even though both:

  • ajv-draft-04 has a peerDependency of AJV 8.5.0
  • my own library requires AJV 8.6.0 for other reasons.

what happens is that NPM install "trampolines" ajv-draft-04 to the top-level node_modules of the application. Both my library and the @seriousme/openapi-schema-validator libraries have AJV 8.6.0 in their "local" node_modules. The actual version of the top-level node_modules is 6.12.6 - NPM chooses to de-dup that version, instead of 8.6.0... The tail-end of the npm-ls looks like:

├─┬ [email protected]
│ ├─┬ @eslint/[email protected]
│ │ └── [email protected]  deduped
│ ├── [email protected] 
│ └─┬ [email protected]
│   └── [email protected] 
└─┬ [email protected]
  ├─┬ @fastify/[email protected]
  │ └── [email protected]  deduped
  ├─┬ [email protected]
  │ └── [email protected]  deduped
  └─┬ [email protected]
    └── [email protected]  deduped

The end result of this is that ajv-draft-04 and ajv version 6.12.6 are both at the top-level node-modules, and that AJV version is not compatible.

I am not sure what to recommend, but, it appears, at this moment, that I have to force dozens of applications to require AJV 8.6.0 at their top level to prevent the dedup of the wrong version of AJV.

Note, running npm --version version 6.14.11 - I see there's a change in peerDependency handling with npm 7.... as an enterprise, we are slow to move to version, but will try npm 7

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.