Skip to content

Commit a3c7eba

Browse files
committed
Merge branch 'refactor-opt-object-type' of github.com:b4h0-c4t/ajv into refactor-opt-object-type
2 parents 9963331 + 03e14cc commit a3c7eba

File tree

7 files changed

+63
-54
lines changed

7 files changed

+63
-54
lines changed

Diff for: README.md

+16-17
Original file line numberDiff line numberDiff line change
@@ -12,29 +12,30 @@ The fastest JSON Schema validator for Node.js and browser. Supports draft-04/06/
1212
[![GitHub Sponsors](https://img.shields.io/badge/$-sponsors-brightgreen)](https://github.com/sponsors/epoberezkin)
1313

1414

15-
## Please [sponsor Ajv development](https://github.com/sponsors/epoberezkin)
15+
## Mozilla MOSS grant and OpenJS Foundation
1616

17-
I will get straight to the point - I need your support to ensure that the development of Ajv continues.
17+
[<img src="https://www.poberezkin.com/images/mozilla.png" width="240" height="68">](https://www.mozilla.org/en-US/moss/) &nbsp;&nbsp;&nbsp; [<img src="https://www.poberezkin.com/images/openjs.png" width="220" height="68">](https://openjsf.org/blog/2020/08/14/ajv-joins-openjs-foundation-as-an-incubation-project/)
1818

19-
I have developed Ajv for 5 years in my free time, but it is not sustainable. I'd appreciate if you consider supporting its further development with donations:
20-
- [GitHub sponsors page](https://github.com/sponsors/epoberezkin) (GitHub will match it)
21-
- [Ajv Open Collective️](https://opencollective.com/ajv)
19+
Ajv has been awarded a grant from Mozilla’s [Open Source Support (MOSS) program](https://www.mozilla.org/en-US/moss/) in the “Foundational Technology” track! It will sponsor the development of Ajv support of [JSON Schema version 2019-09](https://tools.ietf.org/html/draft-handrews-json-schema-02) and of [JSON Type Definition](https://tools.ietf.org/html/draft-ucarion-json-type-definition-04).
20+
21+
Ajv also joined [OpenJS Foundation](https://openjsf.org/) – having this support will help ensure the longevity and stability of Ajv for all its users.
22+
23+
This [blog post](https://www.poberezkin.com/posts/2020-08-14-ajv-json-validator-mozilla-open-source-grant-openjs-foundation.html) has more details.
2224

23-
There are many small and large improvements that are long due, including the support of the next versions of JSON Schema specification, improving website and documentation, and making Ajv more modular and maintainable to address its limitations - what Ajv needs to evolve is much more than what I can contribute in my free time.
25+
I am looking for the long term maintainers of Ajv – working with [ReadySet](https://www.thereadyset.co/), also sponsored by Mozilla, to establish clear guidelines for the role of a "maintainer" and the contribution standards, and to encourage a wider, more inclusive, contribution from the community.
2426

25-
I would also really appreciate any advice you could give on how to raise funds for Ajv development - whether some suitable open-source fund I could apply to or some sponsor I should approach.
2627

27-
Since 2015 Ajv has become widely used, thanks to your help and contributions:
28+
## Please [sponsor Ajv development](https://github.com/sponsors/epoberezkin)
2829

29-
- **90** contributors 🏗
30-
- **5,000** dependent npm packages ⚙️
31-
- **7,000** github stars, from GitHub users [all over the world](https://www.google.com/maps/d/u/0/viewer?mid=1MGRV8ciFUGIbO1l0EKFWNJGYE7iSkDxP&ll=-3.81666561775622e-14%2C4.821737100000007&z=2) ⭐️
32-
- **5,000,000** dependent repositories on GitHub 🚀
33-
- **120,000,000** npm downloads per month! 💯
30+
Since I asked to support Ajv development 40 people and 6 organizations contributed via GitHub and OpenCollective - this support helped receiving the MOSS grant!
3431

35-
I believe it would benefit all Ajv users to help put together the fund that will be used for its further development - it would allow to bring some additional maintainers to the project.
32+
Your continuing support is very important - the funds will be used to develop and maintain Ajv once the next major version is released.
3633

37-
Thank you
34+
Please sponsor Ajv via:
35+
- [GitHub sponsors page](https://github.com/sponsors/epoberezkin) (GitHub will match it)
36+
- [Ajv Open Collective️](https://opencollective.com/ajv)
37+
38+
Thank you.
3839

3940

4041
#### Open Collective sponsors
@@ -156,8 +157,6 @@ Performance of different validators by [json-schema-benchmark](https://github.co
156157
- [$data reference](#data-reference) to use values from the validated data as values for the schema keywords
157158
- [asynchronous validation](#asynchronous-validation) of custom formats and keywords
158159

159-
Currently Ajv is the only validator that passes all the tests from [JSON Schema Test Suite](https://github.com/json-schema/JSON-Schema-Test-Suite) (according to [json-schema-benchmark](https://github.com/ebdrup/json-schema-benchmark), apart from the test that requires that `1.0` is not an integer that is impossible to satisfy in JavaScript).
160-
161160

162161
## Install
163162

Diff for: lib/dot/coerce.def

+18-28
Original file line numberDiff line numberDiff line change
@@ -4,55 +4,45 @@
44
, $coerced = 'coerced' + $lvl;
55
}}
66
var {{=$dataType}} = typeof {{=$data}};
7-
{{? it.opts.coerceTypes == 'array'}}
8-
if ({{=$dataType}} == 'object' && Array.isArray({{=$data}})) {{=$dataType}} = 'array';
9-
{{?}}
10-
117
var {{=$coerced}} = undefined;
128

13-
{{ var $bracesCoercion = ''; }}
14-
{{~ $coerceToTypes:$type:$i }}
15-
{{? $i }}
16-
if ({{=$coerced}} === undefined) {
17-
{{ $bracesCoercion += '}'; }}
18-
{{?}}
19-
20-
{{? it.opts.coerceTypes == 'array' && $type != 'array' }}
21-
if ({{=$dataType}} == 'array' && {{=$data}}.length == 1) {
22-
{{=$coerced}} = {{=$data}} = {{=$data}}[0];
23-
{{=$dataType}} = typeof {{=$data}};
24-
/*if ({{=$dataType}} == 'object' && Array.isArray({{=$data}})) {{=$dataType}} = 'array';*/
25-
}
26-
{{?}}
9+
{{? it.opts.coerceTypes == 'array' }}
10+
if ({{=$dataType}} == 'object' && Array.isArray({{=$data}}) && {{=$data}}.length == 1) {
11+
{{=$data}} = {{=$data}}[0];
12+
{{=$dataType}} = typeof {{=$data}};
13+
if ({{=it.util.checkDataType(it.schema.type, $data, it.opts.strictNumbers)}}) {{=$coerced}} = {{=$data}};
14+
}
15+
{{?}}
2716

17+
if ({{=$coerced}} !== undefined) ;
18+
{{~ $coerceToTypes:$type:$i }}
2819
{{? $type == 'string' }}
29-
if ({{=$dataType}} == 'number' || {{=$dataType}} == 'boolean')
20+
else if ({{=$dataType}} == 'number' || {{=$dataType}} == 'boolean')
3021
{{=$coerced}} = '' + {{=$data}};
3122
else if ({{=$data}} === null) {{=$coerced}} = '';
3223
{{?? $type == 'number' || $type == 'integer' }}
33-
if ({{=$dataType}} == 'boolean' || {{=$data}} === null
24+
else if ({{=$dataType}} == 'boolean' || {{=$data}} === null
3425
|| ({{=$dataType}} == 'string' && {{=$data}} && {{=$data}} == +{{=$data}}
3526
{{? $type == 'integer' }} && !({{=$data}} % 1){{?}}))
3627
{{=$coerced}} = +{{=$data}};
3728
{{?? $type == 'boolean' }}
38-
if ({{=$data}} === 'false' || {{=$data}} === 0 || {{=$data}} === null)
29+
else if ({{=$data}} === 'false' || {{=$data}} === 0 || {{=$data}} === null)
3930
{{=$coerced}} = false;
4031
else if ({{=$data}} === 'true' || {{=$data}} === 1)
4132
{{=$coerced}} = true;
4233
{{?? $type == 'null' }}
43-
if ({{=$data}} === '' || {{=$data}} === 0 || {{=$data}} === false)
34+
else if ({{=$data}} === '' || {{=$data}} === 0 || {{=$data}} === false)
4435
{{=$coerced}} = null;
4536
{{?? it.opts.coerceTypes == 'array' && $type == 'array' }}
46-
if ({{=$dataType}} == 'string' || {{=$dataType}} == 'number' || {{=$dataType}} == 'boolean' || {{=$data}} == null)
37+
else if ({{=$dataType}} == 'string' || {{=$dataType}} == 'number' || {{=$dataType}} == 'boolean' || {{=$data}} == null)
4738
{{=$coerced}} = [{{=$data}}];
4839
{{?}}
4940
{{~}}
50-
51-
{{= $bracesCoercion }}
52-
53-
if ({{=$coerced}} === undefined) {
41+
else {
5442
{{# def.error:'type' }}
55-
} else {
43+
}
44+
45+
if ({{=$coerced}} !== undefined) {
5646
{{# def.setParentData }}
5747
{{=$data}} = {{=$coerced}};
5848
{{? !$dataLvl }}if ({{=$parentData}} !== undefined){{?}}

Diff for: lib/dot/definitions.def

+2-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@
6464

6565
{{## def.nonEmptySchema:_schema:
6666
(it.opts.strictKeywords
67-
? typeof _schema == 'object' && Object.keys(_schema).length > 0
67+
? (typeof _schema == 'object' && Object.keys(_schema).length > 0)
68+
|| _schema === false
6869
: it.util.schemaHasRules(_schema, it.RULES.all))
6970
#}}
7071

Diff for: lib/dot/validate.jst

+1-1
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
it.baseId = it.baseId || it.rootId;
8282
delete it.isTop;
8383

84-
it.dataPathArr = [undefined];
84+
it.dataPathArr = [""];
8585

8686
if (it.schema.default !== undefined && it.opts.useDefaults && it.opts.strictDefaults) {
8787
var $defaultMsg = 'default is ignored in the schema root';

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ajv",
3-
"version": "6.12.3",
3+
"version": "6.12.4",
44
"description": "Another JSON Schema Validator",
55
"main": "lib/ajv.js",
66
"typings": "lib/ajv.d.ts",

Diff for: spec/boolean.spec.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ describe('boolean schemas', function() {
1111
ajvs = [
1212
new Ajv,
1313
new Ajv({allErrors: true}),
14-
new Ajv({inlineRefs: false})
14+
new Ajv({inlineRefs: false}),
15+
new Ajv({strictKeywords: true}),
1516
];
1617
});
1718

Diff for: spec/coercion.spec.js

+23-5
Original file line numberDiff line numberDiff line change
@@ -144,32 +144,50 @@ var coercionArrayRules = JSON.parse(JSON.stringify(coercionRules));
144144
coercionArrayRules.string.array = [
145145
{ from: ['abc'], to: 'abc' },
146146
{ from: [123], to: '123' },
147+
{ from: [true], to: 'true'},
148+
{ from: [null], to: ''},
149+
{ from: [{}], to: undefined },
147150
{ from: ['abc', 'def'], to: undefined },
148151
{ from: [], to: undefined }
149152
];
150153
coercionArrayRules.number.array = [
151154
{ from: [1.5], to: 1.5 },
152-
{ from: ['1.5'], to: 1.5 }
155+
{ from: ['1.5'], to: 1.5 },
156+
{ from: [true], to: 1 },
157+
{ from: [null], to: 0 },
158+
{ from: ['abc'], to: undefined },
159+
{ from: [{}], to: undefined },
153160
];
154161
coercionArrayRules.integer.array = [
155162
{ from: [1], to: 1 },
156163
{ from: ['1'], to: 1 },
157164
{ from: [true], to: 1 },
158-
{ from: [null], to: 0 }
165+
{ from: [null], to: 0 },
166+
{ from: [1.5], to: undefined },
167+
{ from: ['abc'], to: undefined },
168+
{ from: [{}], to: undefined },
159169
];
160170
coercionArrayRules.boolean.array = [
161171
{ from: [true], to: true },
162172
{ from: ['true'], to: true },
163-
{ from: [1], to: true }
173+
{ from: [1], to: true },
174+
{ from: [null], to: false },
175+
{ from: ['abc'], to: undefined },
176+
{ from: [2], to: undefined },
177+
{ from: [{}], to: undefined },
164178
];
165179
coercionArrayRules.null.array = [
166180
{ from: [null], to: null },
167181
{ from: [''], to: null },
168182
{ from: [0], to: null },
169-
{ from: [false], to: null }
183+
{ from: [false], to: null },
184+
{ from: ['abc'], to: undefined },
185+
{ from: [1], to: undefined },
186+
{ from: [true], to: undefined },
187+
{ from: [{}], to: undefined },
170188
];
171189
coercionArrayRules.object.array = [
172-
{ from: [{}], to: undefined }
190+
{ from: [{}], to: undefined }
173191
];
174192

175193
coercionArrayRules.array = {

0 commit comments

Comments
 (0)