Skip to content

Commit

Permalink
moved tests from Ajv, default mode is "full"
Browse files Browse the repository at this point in the history
  • Loading branch information
epoberezkin committed Jul 21, 2020
1 parent 9ad612d commit d99e1d8
Show file tree
Hide file tree
Showing 13 changed files with 1,073 additions and 20 deletions.
6 changes: 6 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[submodule "test"]
path = test
url = [email protected]:json-schema-org/JSON-Schema-Test-Suite.git
[submodule "tests/JSON-Schema-Test-Suite"]
path = tests/JSON-Schema-Test-Suite
url = [email protected]:json-schema-org/JSON-Schema-Test-Suite.git
2 changes: 2 additions & 0 deletions .prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
tests/JSON-Schema-Test-Suite
dist
4 changes: 4 additions & 0 deletions jest.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module.exports = {
preset: "ts-jest",
testEnvironment: "node",
}
12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"name": "ajv-formats",
"version": "0.0.1",
"version": "0.1.0",
"description": "Format validation for Ajv v7 (WIP)",
"main": "index.js",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"scripts": {
"build": "tsc && npm run prettier:write",
"build": "tsc",
"prettier:write": "prettier --write './**/*.{md,json,yaml,js,ts}'",
"prettier:check": "prettier --list-different './**/*.{md,json,yaml,js,ts}'",
"eslint": "eslint --ext .ts ./src/**/*",
Expand All @@ -28,13 +29,18 @@
"homepage": "https://github.com/ajv-validator/ajv-formats#readme",
"devDependencies": {
"@ajv-validator/config": "^0.1.0",
"@types/jest": "^26.0.5",
"@typescript-eslint/eslint-plugin": "^3.7.0",
"@typescript-eslint/parser": "^3.7.0",
"ajv": "^6.12.3",
"eslint": "^7.5.0",
"eslint-config-prettier": "^6.11.0",
"husky": "^4.2.5",
"jest": "^26.1.0",
"json-schema-test": "^2.0.0",
"lint-staged": "^10.2.11",
"prettier": "^2.0.5",
"ts-jest": "^26.1.3",
"typescript": "^3.9.7"
},
"prettier": "@ajv-validator/config/prettierrc.json",
Expand Down
35 changes: 19 additions & 16 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,36 +9,39 @@ import AjvPlugin, {Ajv} from "./plugin"

export interface FormatOptions {
mode: FormatMode
formats: Array<FormatName>
formats: FormatName[]
}

export type PluginOptions = FormatMode | Array<FormatName> | FormatOptions
export type PluginOptions = FormatMode | FormatName[] | FormatOptions

const formatsPlugin: AjvPlugin = function (
ajv: Ajv,
opts: PluginOptions = "fast"
opts: PluginOptions = "full"
): Ajv {
if (typeof opts === "string") {
const fs: DefinedFormats = formats[opts]
let f: FormatName
for (f in fs) {
ajv.addFormat(f, fs[f])
}
const fs = formats[opts]
const names = Object.keys(fs) as FormatName[]
addFormats(names, fs, opts)
} else if (Array.isArray(opts)) {
const fs: DefinedFormats = formats.fast
for (const f of opts) {
ajv.addFormat(f, fs[f])
}
addFormats(opts, formats.full, "full")
} else {
const fs: DefinedFormats = formats[opts.mode]
for (const f of opts.formats) {
addFormats(opts.formats, formats[opts.mode], opts.mode)
}
return ajv

function addFormats(
list: FormatName[],
fs: DefinedFormats,
mode: FormatMode
) {
for (const f of list) {
ajv.addFormat(f, fs[f])
}
ajv._opts.format = mode
}
return ajv
}

export default formatsPlugin
module.exports = formatsPlugin

formatsPlugin.get = get

Expand Down
5 changes: 4 additions & 1 deletion src/plugin.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
// TODO move plugin interface either to ajv or a separate package
import {Format} from "./formats"
import {Format, FormatMode} from "./formats"

export interface Ajv {
addFormat: (name: string, f: Format) => Ajv
_opts: {
format: FormatMode | false
}
}

export default interface AjvPlugin {
Expand Down
1 change: 1 addition & 0 deletions tests/JSON-Schema-Test-Suite
Submodule JSON-Schema-Test-Suite added at ea4155
108 changes: 108 additions & 0 deletions tests/extras/$data/format.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
[
{
"description": "one property has format set in another property",
"schema": {
"properties": {
"str": {
"format": {
"$data": "1/strFormat"
}
},
"strFormat": {}
}
},
"tests": [
{
"description": "a valid date-time string",
"data": {
"str": "1963-06-19T08:30:06.283185Z",
"strFormat": "date-time"
},
"valid": true
},
{
"description": "an invalid date-time string",
"data": {
"str": "06/19/1963 08:30:06 PST",
"strFormat": "date-time"
},
"valid": false
},
{
"description": "only RFC3339 not all of ISO 8601 are valid",
"data": {
"str": "2013-350T01:01:01",
"strFormat": "date-time"
},
"valid": false
},
{
"description": "a valid e-mail address",
"data": {
"str": "[email protected]",
"strFormat": "email"
},
"valid": true
},
{
"description": "an invalid e-mail address",
"data": {
"str": "2962",
"strFormat": "email"
},
"valid": false
},
{
"description": "valid if the format is undefined",
"data": {
"str": "any value"
},
"valid": true
},
{
"description": "fails if value of format is not a string",
"data": {
"str": "1963-06-19T08:30:06.283185Z",
"strFormat": 1963
},
"valid": false
}
]
},
{
"description": "property name is the format for the property value",
"schema": {
"additionalProperties": {
"format": {
"$data": "0#"
}
}
},
"tests": [
{
"description": "valid formats",
"data": {
"date-time": "1963-06-19T08:30:06.283185Z",
"email": "[email protected]"
},
"valid": true
},
{
"description": "invalid date-time",
"data": {
"date-time": "06/19/1963 08:30:06 PST",
"email": "[email protected]"
},
"valid": false
},
{
"description": "invalid e-mail",
"data": {
"date-time": "1963-06-19T08:30:06.283185Z",
"email": "2962"
},
"valid": false
}
]
}
]
Loading

0 comments on commit d99e1d8

Please sign in to comment.