Skip to content

Commit

Permalink
support require without .default and import from mjs, closes #1381
Browse files Browse the repository at this point in the history
  • Loading branch information
epoberezkin committed Mar 14, 2021
1 parent 0fc85a3 commit b0c5399
Show file tree
Hide file tree
Showing 15 changed files with 128 additions and 117 deletions.
2 changes: 1 addition & 1 deletion .tonic_example.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const Ajv = require("ajv").default
const Ajv = require("ajv")
const ajv = new Ajv({allErrors: true})

const schema = {
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ In JavaScript:
// or ESM/TypeScript import
import Ajv from "ajv"
// Node.js require:
const Ajv = require("ajv").default
const Ajv = require("ajv")

const ajv = new Ajv() // options can be passed, e.g. {allErrors: true}
const validate = ajv.compile(schema)
Expand Down
2 changes: 1 addition & 1 deletion benchmark/jtd.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable no-empty */
/* eslint-disable no-console */
const Ajv = require("ajv/dist/jtd").default
const Ajv = require("ajv/dist/jtd")
const Benchmark = require("benchmark")
const jtdValidationTests = require("../spec/json-typedef-spec/tests/validation.json")

Expand Down
2 changes: 1 addition & 1 deletion docs/guide/combining-schemas.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ const strictTreeSchema = {
}

import Ajv2019 from "ajv/dist/2019"
// const Ajv2019 = require("ajv/dist/2019").default
// const Ajv2019 = require("ajv/dist/2019")
const ajv = new Ajv2019({
schemas: [treeSchema, strictTreeSchema],
})
Expand Down
6 changes: 3 additions & 3 deletions docs/guide/environments.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ In this case you need to load Ajv using the correct bundle, depending on which s
<script src="bundle/ajv7.min.js"></script>
<script>
;(function () {
const Ajv = window.ajv7.default
const Ajv = window.ajv7
const ajv = new Ajv()
})()
</script>
Expand All @@ -49,7 +49,7 @@ In this case you need to load Ajv using the correct bundle, depending on which s
<script src="bundle/ajv2019.min.js"></script>
<script>
;(function () {
const Ajv = window.ajv2019.default
const Ajv = window.ajv2019
const ajv = new Ajv()
})()
</script>
Expand All @@ -61,7 +61,7 @@ In this case you need to load Ajv using the correct bundle, depending on which s
<script src="bundle/ajvJTD.min.js"></script>
<script>
;(function () {
const Ajv = window.ajvJTD.default
const Ajv = window.ajvJTD
const ajv = new Ajv()
})()
</script>
Expand Down
2 changes: 1 addition & 1 deletion docs/guide/formats.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ To add all formats from this plugin:
<code-group>
<code-block title="JavaScript">
```javascript
const Ajv = require("ajv").default
const Ajv = require("ajv")
const addFormats = require("ajv-formats")

const ajv = new Ajv()
Expand Down
6 changes: 3 additions & 3 deletions docs/guide/getting-started.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ For example, to validate an object that has a required property "foo" (an intege
<code-group>
<code-block title="JSON Schema">
```javascript
const Ajv = require("ajv").default
const Ajv = require("ajv")
const ajv = new Ajv() // options can be passed, e.g. {allErrors: true}

const schema = {
Expand All @@ -61,7 +61,7 @@ if (!valid) console.log(validate.errors)

<code-block title="JSON Type Definition">
```javascript
const Ajv = require("ajv/dist/jtd").default
const Ajv = require("ajv/dist/jtd")
const ajv = new Ajv() // options can be passed, e.g. {allErrors: true}

const schema = {
Expand Down Expand Up @@ -114,7 +114,7 @@ For the same data structure, you can compile parser and serializer in this way:
<code-group>
<code-block title="JSON Type Definition">
```javascript
const Ajv = require("ajv/dist/jtd").default
const Ajv = require("ajv/dist/jtd")
const ajv = new Ajv() // options can be passed, e.g. {allErrors: true}

const schema = {
Expand Down
2 changes: 1 addition & 1 deletion docs/json-type-definition.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ This document informally describes JSON Type Definition (JTD) specification to h
To use JTD schemas you need to import a different Ajv class:

```javascript
const AjvJTD = require("ajv/dist/jtd").default
const AjvJTD = require("ajv/dist/jtd")
// or in TypeScript:
// import Ajv from "ajv/dist/jtd"
const ajv = new AjvJTD()
Expand Down
63 changes: 33 additions & 30 deletions lib/2019.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,3 @@
export {
Format,
FormatDefinition,
AsyncFormatDefinition,
KeywordDefinition,
KeywordErrorDefinition,
CodeKeywordDefinition,
MacroKeywordDefinition,
FuncKeywordDefinition,
Vocabulary,
Schema,
SchemaObject,
AnySchemaObject,
AsyncSchema,
AnySchema,
ValidateFunction,
AsyncValidateFunction,
ErrorObject,
ErrorNoParams,
} from "./types"

export {Plugin, Options, CodeOptions, InstanceOptions, Logger, ErrorsTextOptions} from "./core"
export {SchemaCxt, SchemaObjCxt} from "./compile"
import KeywordCxt from "./compile/context"
export {KeywordCxt}
export {DefinedError} from "./vocabularies/errors"
export {JSONType} from "./compile/rules"
export {JSONSchemaType} from "./types/json-schema"
export {_, str, stringify, nil, Name, Code, CodeGen, CodeGenOptions} from "./compile/codegen"

import type {AnySchemaObject} from "./types"
import AjvCore, {Options} from "./core"

Expand Down Expand Up @@ -70,3 +40,36 @@ export default class Ajv2019 extends AjvCore {
super.defaultMeta() || (this.getSchema(META_SCHEMA_ID) ? META_SCHEMA_ID : undefined))
}
}

module.exports = Ajv2019
module.exports.default = Ajv2019

export {
Format,
FormatDefinition,
AsyncFormatDefinition,
KeywordDefinition,
KeywordErrorDefinition,
CodeKeywordDefinition,
MacroKeywordDefinition,
FuncKeywordDefinition,
Vocabulary,
Schema,
SchemaObject,
AnySchemaObject,
AsyncSchema,
AnySchema,
ValidateFunction,
AsyncValidateFunction,
ErrorObject,
ErrorNoParams,
} from "./types"

export {Plugin, Options, CodeOptions, InstanceOptions, Logger, ErrorsTextOptions} from "./core"
export {SchemaCxt, SchemaObjCxt} from "./compile"
import KeywordCxt from "./compile/context"
export {KeywordCxt}
export {DefinedError} from "./vocabularies/errors"
export {JSONType} from "./compile/rules"
export {JSONSchemaType} from "./types/json-schema"
export {_, str, stringify, nil, Name, Code, CodeGen, CodeGenOptions} from "./compile/codegen"
65 changes: 34 additions & 31 deletions lib/ajv.ts
Original file line number Diff line number Diff line change
@@ -1,34 +1,3 @@
export {
Format,
FormatDefinition,
AsyncFormatDefinition,
KeywordDefinition,
KeywordErrorDefinition,
CodeKeywordDefinition,
MacroKeywordDefinition,
FuncKeywordDefinition,
Vocabulary,
Schema,
SchemaObject,
AnySchemaObject,
AsyncSchema,
AnySchema,
ValidateFunction,
AsyncValidateFunction,
SchemaValidateFunction,
ErrorObject,
ErrorNoParams,
} from "./types"

export {Plugin, Options, CodeOptions, InstanceOptions, Logger, ErrorsTextOptions} from "./core"
export {SchemaCxt, SchemaObjCxt} from "./compile"
import KeywordCxt from "./compile/context"
export {KeywordCxt}
export {DefinedError} from "./vocabularies/errors"
export {JSONType} from "./compile/rules"
export {JSONSchemaType} from "./types/json-schema"
export {_, str, stringify, nil, Name, Code, CodeGen, CodeGenOptions} from "./compile/codegen"

import type {AnySchemaObject} from "./types"
import AjvCore from "./core"
import draft7Vocabularies from "./vocabularies/draft7"
Expand Down Expand Up @@ -59,3 +28,37 @@ export default class Ajv extends AjvCore {
super.defaultMeta() || (this.getSchema(META_SCHEMA_ID) ? META_SCHEMA_ID : undefined))
}
}

module.exports = Ajv
module.exports.default = Ajv

export {
Format,
FormatDefinition,
AsyncFormatDefinition,
KeywordDefinition,
KeywordErrorDefinition,
CodeKeywordDefinition,
MacroKeywordDefinition,
FuncKeywordDefinition,
Vocabulary,
Schema,
SchemaObject,
AnySchemaObject,
AsyncSchema,
AnySchema,
ValidateFunction,
AsyncValidateFunction,
SchemaValidateFunction,
ErrorObject,
ErrorNoParams,
} from "./types"

export {Plugin, Options, CodeOptions, InstanceOptions, Logger, ErrorsTextOptions} from "./core"
export {SchemaCxt, SchemaObjCxt} from "./compile"
import KeywordCxt from "./compile/context"
export {KeywordCxt}
export {DefinedError} from "./vocabularies/errors"
export {JSONType} from "./compile/rules"
export {JSONSchemaType} from "./types/json-schema"
export {_, str, stringify, nil, Name, Code, CodeGen, CodeGenOptions} from "./compile/codegen"
69 changes: 36 additions & 33 deletions lib/jtd.ts
Original file line number Diff line number Diff line change
@@ -1,47 +1,15 @@
export {
Format,
FormatDefinition,
AsyncFormatDefinition,
KeywordDefinition,
KeywordErrorDefinition,
CodeKeywordDefinition,
MacroKeywordDefinition,
FuncKeywordDefinition,
Vocabulary,
Schema,
SchemaObject,
AnySchemaObject,
AsyncSchema,
AnySchema,
ValidateFunction,
AsyncValidateFunction,
ErrorObject,
ErrorNoParams,
JTDParser,
} from "./types"

export {Plugin, Options, CodeOptions, InstanceOptions, Logger, ErrorsTextOptions} from "./core"
export {SchemaCxt, SchemaObjCxt} from "./compile"
import KeywordCxt from "./compile/context"
export {KeywordCxt}
export {JTDErrorObject} from "./vocabularies/jtd"
export {_, str, stringify, nil, Name, Code, CodeGen, CodeGenOptions} from "./compile/codegen"

import type {AnySchemaObject, SchemaObject, JTDParser} from "./types"
import type {JTDSchemaType, JTDDataType} from "./types/jtd-schema"
export {JTDSchemaType, JTDDataType}
import AjvCore, {CurrentOptions} from "./core"
import jtdVocabulary from "./vocabularies/jtd"
import jtdMetaSchema from "./refs/jtd-schema"
import compileSerializer from "./compile/jtd/serialize"
import compileParser from "./compile/jtd/parse"
import {SchemaEnv} from "./compile"

// const META_SUPPORT_DATA = ["/properties"]

const META_SCHEMA_ID = "JTD-meta-schema"

export type JTDOptions = CurrentOptions & {
type JTDOptions = CurrentOptions & {
// strict mode options not supported with JTD:
strictTypes?: never
strictTuples?: never
Expand Down Expand Up @@ -124,3 +92,38 @@ export default class Ajv extends AjvCore {
return sch.parse
}
}

module.exports = Ajv
module.exports.default = Ajv

export {
Format,
FormatDefinition,
AsyncFormatDefinition,
KeywordDefinition,
KeywordErrorDefinition,
CodeKeywordDefinition,
MacroKeywordDefinition,
FuncKeywordDefinition,
Vocabulary,
Schema,
SchemaObject,
AnySchemaObject,
AsyncSchema,
AnySchema,
ValidateFunction,
AsyncValidateFunction,
ErrorObject,
ErrorNoParams,
JTDParser,
} from "./types"

export {Plugin, Options, CodeOptions, InstanceOptions, Logger, ErrorsTextOptions} from "./core"
export {SchemaCxt, SchemaObjCxt} from "./compile"
import KeywordCxt from "./compile/context"
export {KeywordCxt}
export {JTDErrorObject} from "./vocabularies/jtd"
export {_, str, stringify, nil, Name, Code, CodeGen, CodeGenOptions} from "./compile/codegen"

export {JTDSchemaType, JTDDataType}
export {JTDOptions}
6 changes: 3 additions & 3 deletions spec/ajv.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type Ajv from "../dist/core"

const m = typeof window == "object" ? (window as any).ajv7 : require("" + "..")
const AjvClass: typeof Ajv = m.default
const AjvClass: typeof Ajv = typeof window == "object" ? (window as any).ajv7 : require("" + "..")

export default AjvClass
module.exports = AjvClass
module.exports.default = AjvClass
7 changes: 4 additions & 3 deletions spec/ajv2019.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type Ajv2019 from "../dist/2019"

const m = typeof window == "object" ? (window as any).ajv2019 : require("" + "../dist/2019")
const AjvClass: typeof Ajv2019 = m.default
const AjvClass: typeof Ajv2019 =
typeof window == "object" ? (window as any).ajv2019 : require("" + "../dist/2019")

export default AjvClass
module.exports = AjvClass
module.exports.default = AjvClass
7 changes: 4 additions & 3 deletions spec/ajv_jtd.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type AjvJTD from "../dist/jtd"

const m = typeof window == "object" ? (window as any).ajvJTD : require("" + "../dist/jtd")
const AjvClass: typeof AjvJTD = m.default
const AjvClass: typeof AjvJTD =
typeof window == "object" ? (window as any).ajvJTD : require("" + "../dist/jtd")

export default AjvClass
module.exports = AjvClass
module.exports.default = AjvClass
4 changes: 2 additions & 2 deletions spec/javacript.spec.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const Ajv = require("./ajv").default
const Ajv2019 = require("./ajv2019").default
const Ajv = require("./ajv")
const Ajv2019 = require("./ajv2019")
const assert = require("assert")

describe("using Ajv with javascript", () => {
Expand Down

0 comments on commit b0c5399

Please sign in to comment.