Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: ajv-validator/ajv
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v8.1.0
Choose a base ref
...
head repository: ajv-validator/ajv
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v8.2.0
Choose a head ref

Commits on Apr 12, 2021

  1. Add JTDDataType to compile signature

    To get this to work, a few changes had to be made:
    1. The semantics of `ref` for JTDDataType had to be changed. I don't
       entirely understand why, but my guess is that infer steps allow the
       compiler to "take a break" and so this helps with the recursion
       checking.
    2. Added `SomeJTDSchemaType`. This is necessary to prevent typescript
       from inferring a JTDDataType when actually it's a different schema.
       This is especially a problem for simple types, e.g. the empty schema
       is valid JTD so it can confuse things.
    
    Three other notes about the current implementation. Historically
    specifying a type for compile indicated that that was the return type.
    To keep that working, the overload signatures for the JTDDataType
    returns need to have their first parameter extend never, so that it's
    only inferred (or it could be specified manually with `compile<never,
    ActualType>(...)`.
    
    In addition, SomeJTDSchemaType needs to use the empty type `{}`, there's
    a note linking to the issue that discusses how this is the one instance
    when this is actually what you want, and it's tested.
    
    Finally, this works with typescript 4.2.3. However, even with the infer
    trick, it's very close to the maximum complexity that typescript wants
    to deal with. Small changes in the overload signature or the way that
    typescript descides to handle this could result in compile erroring in
    typescript saying the type is too complex. In that event, this overload
    could always be removed, but I wanted to raise this potential risk now.
    
    fixes #1489
    erikbrinkman committed Apr 12, 2021
    Copy the full SHA
    59cdbdf View commit details
  2. Update typescript getting started documentation

    Raised this after discussion here: 
    #1331 (comment)
    alazyzombie authored Apr 12, 2021
    Copy the full SHA
    599fa28 View commit details
  3. Merge pull request #1548 from alazyzombie/patch-1

    docs: Update typescript getting started documentation
    epoberezkin authored Apr 12, 2021
    Copy the full SHA
    8762e6f View commit details

Commits on Apr 14, 2021

  1. Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature.
    Copy the full SHA
    0d5c18c View commit details

Commits on Apr 15, 2021

  1. Merge pull request #1547 from erikbrinkman/jtd-type

    Add JTDDataType to compile signature
    epoberezkin authored Apr 15, 2021
    Copy the full SHA
    baf1475 View commit details

Commits on Apr 19, 2021

  1. site quote

    epoberezkin committed Apr 19, 2021
    Copy the full SHA
    364cc00 View commit details
  2. update index

    epoberezkin committed Apr 19, 2021
    Copy the full SHA
    4470a8b View commit details

Commits on Apr 22, 2021

  1. Copy the full SHA
    b4e7749 View commit details

Commits on Apr 23, 2021

  1. Some improvements to JSONSchemaType for records

    Additional properties doesn't required `type: "string"` since it's
    implied. `required` is no long required for objects without any required
    members.
    
    fixes #1491
    erikbrinkman committed Apr 23, 2021
    Copy the full SHA
    d88ba06 View commit details
  2. Fix typescript sample bug

    ktrueda committed Apr 23, 2021
    Copy the full SHA
    fa6594f View commit details
  3. ajv webinar

    epoberezkin committed Apr 23, 2021
    Copy the full SHA
    f45b963 View commit details

Commits on Apr 24, 2021

  1. Merge pull request #1564 from erikbrinkman/jsonschematype-improvements

    Some improvements to JSONSchemaType for records
    epoberezkin authored Apr 24, 2021
    Copy the full SHA
    2edc54c View commit details
  2. Merge pull request #1561 from Delapouite/doc

    doc: fix draft version typo (2012 → 2020)
    epoberezkin authored Apr 24, 2021
    Copy the full SHA
    62e2992 View commit details
  3. webinar

    epoberezkin committed Apr 24, 2021
    Copy the full SHA
    6497f14 View commit details
  4. update readme

    epoberezkin authored Apr 24, 2021
    Copy the full SHA
    c9e0676 View commit details

Commits on Apr 25, 2021

  1. Revert chaning type of MyData

    ktrueda committed Apr 25, 2021
    Copy the full SHA
    55706db View commit details
  2. Updated Readme

    seriousme committed Apr 25, 2021
    Copy the full SHA
    7e52716 View commit details
  3. Copy the full SHA
    b780944 View commit details

Commits on Apr 26, 2021

  1. update webinar

    epoberezkin committed Apr 26, 2021
    Copy the full SHA
    b203181 View commit details
  2. Copy the full SHA
    c672310 View commit details
  3. fix link

    epoberezkin committed Apr 26, 2021
    Copy the full SHA
    ff67954 View commit details
  4. Copy the full SHA
    e695db1 View commit details
  5. Merge pull request #1562 from ktrueda/fix/typescript_doc

    doc: Fix JTD typescript sample error
    epoberezkin authored Apr 26, 2021
    Copy the full SHA
    d3bef59 View commit details
  6. Copy the full SHA
    30a8561 View commit details
  7. Merge pull request #1533 from realityking/rollup-bundle

    Rollup bundle
    epoberezkin authored Apr 26, 2021
    Copy the full SHA
    00b0e24 View commit details

Commits on Apr 27, 2021

  1. update event

    epoberezkin committed Apr 27, 2021
    Copy the full SHA
    aa4f64f View commit details
  2. update event

    epoberezkin committed Apr 27, 2021
    Copy the full SHA
    6ad97e8 View commit details
  3. 8.2.0

    epoberezkin committed Apr 27, 2021
    Copy the full SHA
    2123bf2 View commit details
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -19,6 +19,24 @@ Supports JSON Schema draft-06/07/2019-09/2020-12 (draft-04 is supported in [vers

[<img src="https://ajv.js.org/img/mozilla.svg" width="45%">](https://www.mozilla.org)<img src="https://ajv.js.org/img/gap.svg" width="8%">[<img src="https://ajv.js.org/img/reserved.svg" width="45%">](https://opencollective.com/ajv)

## Ajv online event - May 20, 10am PT / 6pm UK

We will talk about:
- new features of Ajv version 8.
- the improvements sponsored by Mozilla's MOSS grant.
- how Ajv is used in JavaScript applications.

Speakers:
- [Evgeny Poberezkin](https://github.com/epoberezkin), the creator of Ajv.
- [Mehan Jayasuriya](https://github.com/mehan), Program Officer at Mozilla Foundation, leading the [MOSS](https://www.mozilla.org/en-US/moss/) and other programs investing in the open source and community ecosystems.
- [Matteo Collina](https://github.com/mcollina), Technical Director at NearForm and Node.js Technical Steering Committee member, creator of Fastify web framework.
- [Kin Lane](https://github.com/kinlane), Chief Evangelist at Postman. Studying the tech, business & politics of APIs since 2010. Presidential Innovation Fellow during the Obama administration.
- [Ulysse Carion](https://github.com/ucarion), the creator of JSON Type Definition specification.

[Gajus Kuizinas](https://github.com/gajus) will host the event.

Please [register here](https://us02web.zoom.us/webinar/register/2716192553618/WN_erJ_t4ICTHOnGC1SOybNnw).

## Contributing

More than 100 people contributed to Ajv, and we would love to have you join the development. We welcome implementing new features that will benefit many users and ideas to improve our documentation.
2 changes: 1 addition & 1 deletion docs/.vuepress/components/NewsHome.vue
Original file line number Diff line number Diff line change
@@ -9,7 +9,7 @@

<Column side="right">
<div v-html="post.excerpt"></div>
<Button :link="post.path" cssClass="read-more">Read more</Button>
<Button :link="post.path" cssClass="read-more" v-if="post.frontmatter.more !== false">Read more</Button>
</Column>
</Columns>
</div>
4 changes: 3 additions & 1 deletion docs/.vuepress/config.js
Original file line number Diff line number Diff line change
@@ -85,9 +85,10 @@ module.exports = {
text: "Information",
items: [
{link: "/news/", text: "News"},
{link: "/faq", text: "FAQ"},
{link: "/security", text: "Security"},
{link: "/v6-to-v8-migration", text: "Migrate from v6"},
{link: "/faq", text: "FAQ"},
{link: "/testimonials", text: "What users say"},
{link: "/license", text: "License"},
],
},
@@ -151,6 +152,7 @@ module.exports = {
"/faq",
"/security",
["/v6-to-v8-migration", "Migrate from v6 to v8"],
"/testimonials",
["/license", "License"],
],
},
10 changes: 5 additions & 5 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -117,9 +117,9 @@ if (!valid) console.log(ajv.errors)
<Testimonials>
<Testimonial color="green">

Built-in data validation is a key feature of any web framework. We decided to leverage JSON Schema in [Fastify](https://www.fastify.io), and Ajv fits our needs wonderfully: it’s fast, stable and well maintained.
Ajv stands out as the implementation of choice - it provides a rich API which many thousands of people use in production... Ajv is partly responsible for the success of JSON Schema.

[Matteo Collina](https://github.com/mcollina), tech director [@nearform](https://github.com/nearform) and TSC member
[Ben Hutton](https://github.com/relequestual), JSON Schema Specification Lead

</Testimonial>

@@ -132,7 +132,7 @@ Built-in data validation is a key feature of any web framework. We decided to le
</Testimonial>
</Testimonials>

<span style="float:right;">[All testimonials](./testimonials.md)</span>
<span style="float:right;">[All quotes](./testimonials.md)</span>

</HomeSection>

@@ -211,7 +211,7 @@ Ajv is free to use and open-source that many developers contributed to. Join us!

<FooterColumn type="links">

[Learn Ajv](./docs/getting-started.md)
[Learn Ajv](./guide/getting-started.md)

[Reference](./api.md)

@@ -225,7 +225,7 @@ Ajv is free to use and open-source that many developers contributed to. Join us!

[JSON Type Definition](./json-type-definition.md)

[Contributing](./contributing.html)
[Contributing](./contributing.md)

</FooterColumn>

2 changes: 1 addition & 1 deletion docs/api.md
Original file line number Diff line number Diff line change
@@ -64,7 +64,7 @@ If you use JTD with typescript, the type for the schema can be derived from the
import Ajv, {JTDSchemaType} from "ajv/dist/jtd"
const ajv = new Ajv()

interface MyData = {
interface MyData {
foo: number
bar?: string
}
2 changes: 1 addition & 1 deletion docs/guide/schema-language.md
Original file line number Diff line number Diff line change
@@ -47,7 +47,7 @@ const draft6MetaSchema = require("ajv/dist/refs/json-schema-draft-06.json")
ajv.addMetaSchema(draft6MetaSchema)
```

### draft 2019-09 (and draft-2012-12)
### draft 2019-09 (and draft-2020-12)

The main advantage of this JSON Schema version over draft-07 is the ability to spread the definition of records that do not allow additional properties across multiple schemas. If you do not need it, you might be better off with draft-07.

3 changes: 2 additions & 1 deletion docs/guide/typescript.md
Original file line number Diff line number Diff line change
@@ -15,6 +15,7 @@ Ajv takes advantage of TypeScript type system to provide additional functionalit
## Utility types for schemas

For the same example as in [Getting started](./getting-started):
- ensure strictNullChecks is true

<code-group>
<code-block title="JSON Schema">
@@ -31,7 +32,7 @@ const schema: JSONSchemaType<MyData> = {
type: "object",
properties: {
foo: {type: "integer"},
bar: {type: "string"}
bar: {type: "string", nullable: "true"}
},
required: ["foo"],
additionalProperties: false
24 changes: 24 additions & 0 deletions docs/news/2021-04-24-ajv-online-event.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
---
news: true
title: "Ajv online event - May 20, 10am PT / 6pm UK"
date: 2021-04-24
more: false
---

We will talk about:
- new features of Ajv version 8.
- the improvements sponsored by Mozilla's MOSS grant.
- how Ajv is used in JavaScript applications.

Speakers:
- [Evgeny Poberezkin](https://github.com/epoberezkin), the creator of Ajv.
- [Mehan Jayasuriya](https://github.com/mehan), Program Officer at Mozilla Foundation, leading the [MOSS](https://www.mozilla.org/en-US/moss/) and other programs investing in the open source and community ecosystems.
- [Matteo Collina](https://github.com/mcollina), Technical Director at NearForm and Node.js Technical Steering Committee member, creator of Fastify web framework.
- [Kin Lane](https://github.com/kinlane), Chief Evangelist at Postman. Studying the tech, business & politics of APIs since 2010. Presidential Innovation Fellow during the Obama administration.
- [Ulysse Carion](https://github.com/ucarion), the creator of JSON Type Definition specification.

[Gajus Kuizinas](https://github.com/gajus) will host the event.

Please [register here](https://us02web.zoom.us/webinar/register/4216192074976/WN_erJ_t4ICTHOnGC1SOybNnw).

<!-- more -->
12 changes: 11 additions & 1 deletion docs/testimonials.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@
# Testimonials
# What users say

In the past 6 years of working on the JSON Schema Specification itself, Ajv stands out as the implementation of choice. It is very well documented and provides a rich API for extending JSON Schema which many thousands of people use in production today. A huge effort was put into testing, with many tests now forming part of the official test suite.

I've personally used Ajv in production to validate requests for a federated undiagnosed genetic disease program that has lead to new scientific discoveries and literally changed lives.

Ajv development can inform the future tooling and specification changes. There's no doubt that Ajv is partly responsible for the ubiquity and success of JSON Schema.

[Ben Hutton](https://github.com/relequestual), JSON Schema Specification Lead

<br>

[ESLint](https://eslint.org/) has used Ajv for validating our complex configurations. Ajv has proven to be reliable over the years we’ve been using it and ESLint is proud to sponsor Ajv’s continued development.

16 changes: 14 additions & 2 deletions lib/core.ts
Original file line number Diff line number Diff line change
@@ -30,7 +30,7 @@ export {KeywordCxt} from "./compile/validate"
export {DefinedError} from "./vocabularies/errors"
export {JSONType} from "./compile/rules"
export {JSONSchemaType} from "./types/json-schema"
export {JTDSchemaType} from "./types/jtd-schema"
export {JTDSchemaType, SomeJTDSchemaType, JTDDataType} from "./types/jtd-schema"
export {_, str, stringify, nil, Name, Code, CodeGen, CodeGenOptions} from "./compile/codegen"

import type {
@@ -50,7 +50,7 @@ import type {
AddedFormat,
} from "./types"
import type {JSONSchemaType} from "./types/json-schema"
import type {JTDSchemaType} from "./types/jtd-schema"
import type {JTDSchemaType, SomeJTDSchemaType, JTDDataType} from "./types/jtd-schema"
import ValidationError from "./runtime/validation_error"
import MissingRefError from "./compile/ref_error"
import {getRules, ValidationRules, Rule, RuleGroup, JSONType} from "./compile/rules"
@@ -313,6 +313,12 @@ export default class Ajv {
// Separated for type inference to work
// eslint-disable-next-line @typescript-eslint/unified-signatures
validate<T>(schema: JTDSchemaType<T>, data: unknown): data is T
// This overload is only intended for typescript inference, the first
// argument prevents manual type annotation from matching this overload
validate<N extends never, T extends SomeJTDSchemaType>(
schema: T,
data: unknown
): data is JTDDataType<T>
validate<T>(schema: AsyncSchema, data: unknown | T): Promise<T>
validate<T>(schemaKeyRef: AnySchema | string, data: unknown): data is T | Promise<T>
validate<T>(
@@ -338,6 +344,12 @@ export default class Ajv {
// Separated for type inference to work
// eslint-disable-next-line @typescript-eslint/unified-signatures
compile<T = unknown>(schema: JTDSchemaType<T>, _meta?: boolean): ValidateFunction<T>
// This overload is only intended for typescript inference, the first
// argument prevents manual type annotation from matching this overload
compile<N extends never, T extends SomeJTDSchemaType>(
schema: T,
_meta?: boolean
): ValidateFunction<JTDDataType<T>>
compile<T = unknown>(schema: AsyncSchema, _meta?: boolean): AsyncValidateFunction<T>
compile<T = unknown>(schema: AnySchema, _meta?: boolean): AnyValidateFunction<T>
compile<T = unknown>(schema: AnySchema, _meta?: boolean): AnyValidateFunction<T> {
4 changes: 2 additions & 2 deletions lib/jtd.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type {AnySchemaObject, SchemaObject, JTDParser} from "./types"
import type {JTDSchemaType, JTDDataType} from "./types/jtd-schema"
import type {JTDSchemaType, SomeJTDSchemaType, JTDDataType} from "./types/jtd-schema"
import AjvCore, {CurrentOptions} from "./core"
import jtdVocabulary from "./vocabularies/jtd"
import jtdMetaSchema from "./refs/jtd-schema"
@@ -125,5 +125,5 @@ export {KeywordCxt} from "./compile/validate"
export {JTDErrorObject} from "./vocabularies/jtd"
export {_, str, stringify, nil, Name, Code, CodeGen, CodeGenOptions} from "./compile/codegen"

export {JTDSchemaType, JTDDataType}
export {JTDSchemaType, SomeJTDSchemaType, JTDDataType}
export {JTDOptions}
14 changes: 9 additions & 5 deletions lib/types/json-schema.ts
Original file line number Diff line number Diff line change
@@ -88,20 +88,24 @@ export type JSONSchemaType<T, _partial extends boolean = false> = (
// "properties" are optional for more concise dictionary schemas
// "patternProperties" and can be only used with interfaces that have string index
type: JSONType<"object", _partial>
// "required" type does not guarantee that all required properties are listed
// it only asserts that optional cannot be listed
required: _partial extends true ? Readonly<(keyof T)[]> : Readonly<RequiredMembers<T>[]>
additionalProperties?: boolean | JSONSchemaType<T[string]>
unevaluatedProperties?: boolean | JSONSchemaType<T[string]>
properties?: _partial extends true ? Partial<PropertiesSchema<T>> : PropertiesSchema<T>
patternProperties?: {[Pattern in string]?: JSONSchemaType<T[string]>}
propertyNames?: JSONSchemaType<string>
propertyNames?: Omit<JSONSchemaType<string>, "type"> & {type?: "string"}
dependencies?: {[K in keyof T]?: Readonly<(keyof T)[]> | PartialSchema<T>}
dependentRequired?: {[K in keyof T]?: Readonly<(keyof T)[]>}
dependentSchemas?: {[K in keyof T]?: PartialSchema<T>}
minProperties?: number
maxProperties?: number
}
} & (// "required" type does not guarantee that all required properties
// are listed it only asserts that optional cannot be listed.
// "required" is not necessary if it's a non-partial type with no required keys
_partial extends true
? {required: Readonly<(keyof T)[]>}
: [RequiredMembers<T>] extends [never]
? {required?: Readonly<RequiredMembers<T>[]>}
: {required: Readonly<RequiredMembers<T>[]>})
: T extends null
? {
nullable: true
52 changes: 45 additions & 7 deletions lib/types/jtd-schema.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,45 @@
/** numeric strings */
type NumberType = "float32" | "float64" | "int8" | "uint8" | "int16" | "uint16" | "int32" | "uint32"

/** string strings */
type StringType = "string" | "timestamp"

/** Generic JTD Schema without inference of the represented type */
export type SomeJTDSchemaType = (
| // ref
{ref: string}
// primitives
| {type: NumberType | StringType | "boolean"}
// enum
| {enum: string[]}
// elements
| {elements: SomeJTDSchemaType}
// values
| {values: SomeJTDSchemaType}
// properties
| {
properties: Record<string, SomeJTDSchemaType>
optionalProperties?: Record<string, SomeJTDSchemaType>
additionalProperties?: boolean
}
| {
properties?: Record<string, SomeJTDSchemaType>
optionalProperties: Record<string, SomeJTDSchemaType>
additionalProperties?: boolean
}
// discriminator
| {discriminator: string; mapping: Record<string, SomeJTDSchemaType>}
// empty
// NOTE see the end of
// https://github.com/typescript-eslint/typescript-eslint/issues/2063#issuecomment-675156492
// eslint-disable-next-line @typescript-eslint/ban-types
| {}
) & {
nullable?: boolean
metadata?: Record<string, unknown>
definitions?: Record<string, SomeJTDSchemaType>
}

/** required keys of an object, not undefined */
type RequiredKeys<T> = {
[K in keyof T]-?: undefined extends T[K] ? never : K
@@ -57,12 +99,6 @@ type IsRecord<T, Union extends boolean> = Union extends IsUnion<Exclude<T, null>
: true
: false

/** numeric strings */
type NumberType = "float32" | "float64" | "int8" | "uint8" | "int16" | "uint16" | "int32" | "uint32"

/** string strings */
type StringType = "string" | "timestamp"

/** actual schema */
export type JTDSchemaType<T, D extends Record<string, unknown> = Record<string, never>> = (
| // refs - where null wasn't specified, must match exactly
@@ -164,7 +200,9 @@ export type JTDSchemaType<T, D extends Record<string, unknown> = Record<string,
type JTDDataDef<S, D extends Record<string, unknown>> =
| (// ref
S extends {ref: string}
? JTDDataDef<D[S["ref"]], D>
? D extends {[K in S["ref"]]: infer V}
? JTDDataDef<V, D>
: never
: // type
S extends {type: NumberType}
? number
12 changes: 8 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ajv",
"version": "8.1.0",
"version": "8.2.0",
"description": "Another JSON Schema Validator",
"main": "dist/ajv.js",
"types": "dist/ajv.d.ts",
@@ -17,7 +17,7 @@
"test-codegen": "nyc cross-env TS_NODE_PROJECT=spec/tsconfig.json mocha -r ts-node/register 'spec/codegen.spec.ts' -R spec",
"test-debug": "npm run test-spec -- --inspect-brk",
"test-cov": "nyc npm run test-spec",
"bundle": "rm -rf bundle && node ./scripts/bundle.js ajv ajv7 ajv7 && node ./scripts/bundle.js 2019 ajv2019 ajv2019 && node ./scripts/bundle.js jtd ajvJTD ajvJTD",
"bundle": "rm -rf bundle && rollup -c",
"build": "rm -rf dist && tsc && cp -r lib/refs dist && rm dist/refs/json-schema-2019-09/index.ts && rm dist/refs/json-schema-2020-12/index.ts && rm dist/refs/jtd-schema.ts",
"json-tests": "rm -rf spec/_json/*.js && node scripts/jsontests",
"test-karma": "karma start",
@@ -64,6 +64,10 @@
},
"devDependencies": {
"@ajv-validator/config": "^0.3.0",
"@rollup/plugin-commonjs": "^18.0.0",
"@rollup/plugin-json": "^4.1.0",
"@rollup/plugin-node-resolve": "^11.2.1",
"@rollup/plugin-typescript": "^8.2.1",
"@types/chai": "^4.2.12",
"@types/mocha": "^8.0.3",
"@types/node": "^14.0.27",
@@ -72,7 +76,6 @@
"@typescript-eslint/parser": "^3.8.0",
"@vuepress/shared-utils": "^1.8.2",
"ajv-formats": "^2.0.0",
"browserify": "^17.0.0",
"chai": "^4.0.1",
"cross-env": "^7.0.2",
"dayjs": "^1.10.4",
@@ -93,7 +96,8 @@
"node-fetch": "^2.6.1",
"nyc": "^15.0.0",
"prettier": "^2.0.5",
"terser": "^5.2.1",
"rollup": "^2.44.0",
"rollup-plugin-terser": "^7.0.2",
"ts-node": "^9.0.0",
"tsify": "^5.0.2",
"typescript": "^4.2.0",
32 changes: 32 additions & 0 deletions rollup.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import commonjs from "@rollup/plugin-commonjs"
import {nodeResolve} from "@rollup/plugin-node-resolve"
import json from "@rollup/plugin-json"
import typescript from "@rollup/plugin-typescript"
import {terser} from "rollup-plugin-terser"

function createBundleConfig(sourceFile, outFile, globalName) {
return {
input: `./lib/${sourceFile}.ts`,
output: [
{
file: `./bundle/${outFile}.bundle.js`,
format: "umd",
name: globalName,
},
{
file: `./bundle/${outFile}.min.js`,
format: "umd",
name: globalName,
sourcemap: true,
plugins: [terser()],
},
],
plugins: [commonjs(), nodeResolve(), json(), typescript()],
}
}

export default [
createBundleConfig("ajv", "ajv7", "av7"),
createBundleConfig("2019", "ajv2019", "ajv2019"),
createBundleConfig("jtd", "ajvJTD", "ajvJTD"),
]
48 changes: 0 additions & 48 deletions scripts/bundle.js

This file was deleted.

31 changes: 31 additions & 0 deletions spec/types/json-schema.spec.ts
Original file line number Diff line number Diff line change
@@ -145,6 +145,8 @@ const invalidSchema: JSONSchemaType<MyData> = {
type: [],
}

type MyEnumRecord = Record<"a" | "b" | "c" | "d", number | undefined>

describe("JSONSchemaType type and validation as a type guard", () => {
const ajv = new _Ajv({allowUnionTypes: true})

@@ -259,6 +261,35 @@ describe("JSONSchemaType type and validation as a type guard", () => {
should.not.exist(ajv.errors)
})
})

describe("schema should be simple for record types", () => {
it("typechecks a valid type without required", () => {
const myEnumRecordSchema: JSONSchemaType<MyEnumRecord> = {
type: "object",
propertyNames: {enum: ["a", "b", "c", "d"]},
additionalProperty: {type: "number"},
}
// eslint-disable-next-line no-void
void myEnumRecordSchema
})

it("requires required for non-optional types", () => {
// @ts-expect-error missing required
const requiredSchema: JSONSchemaType<{a: number}> = {
type: "object",
}
// eslint-disable-next-line no-void
void requiredSchema
})

it("doesn't require required for optional types", () => {
const optionalSchema: JSONSchemaType<{a?: number}> = {
type: "object",
}
// eslint-disable-next-line no-void
void optionalSchema
})
})
})

// eslint-disable-next-line no-void
18 changes: 13 additions & 5 deletions spec/types/jtd-schema.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/no-empty-interface,no-void */
import _Ajv from "../ajv_jtd"
import type {JTDSchemaType, JTDDataType} from "../../dist/jtd"
import type {JTDSchemaType, SomeJTDSchemaType, JTDDataType} from "../../dist/jtd"
import chai from "../chai"
const should = chai.should()

@@ -353,16 +353,14 @@ describe("JTDDataType", () => {
},
} as const

type MyData1 = JTDDataType<typeof mySchema1>

const validate = ajv.compile<MyData1>(mySchema1)
const validate = ajv.compile(mySchema1)
const validData: unknown = {type: "a", a: 1}
if (validate(validData) && validData.type === "a") {
validData.a.should.equal(1)
}
should.not.exist(validate.errors)

if (ajv.validate<MyData1>(mySchema1, validData) && validData.type === "a") {
if (ajv.validate(mySchema1, validData) && validData.type === "a") {
validData.a.should.equal(1)
}
should.not.exist(ajv.errors)
@@ -492,3 +490,13 @@ describe("JTDDataType", () => {
void [empty]
})
})

describe("SomeJTDSchemaType", () => {
it("should allow setting unknowns", () => {
// This test is basically here to assert that we should be using `{}` in
// SomeJTDSchemaType
const schema: SomeJTDSchemaType = {}

void [schema]
})
})