Skip to content

Commit

Permalink
Merge branch 'refactor-opt-object-type' of github.com:b4h0-c4t/ajv in…
Browse files Browse the repository at this point in the history
…to refactor-opt-object-type
  • Loading branch information
b4h0-c4t committed Sep 7, 2020
2 parents 9963331 + 03e14cc commit a3c7eba
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 54 deletions.
33 changes: 16 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,29 +12,30 @@ The fastest JSON Schema validator for Node.js and browser. Supports draft-04/06/
[![GitHub Sponsors](https://img.shields.io/badge/$-sponsors-brightgreen)](https://github.com/sponsors/epoberezkin)


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

I will get straight to the point - I need your support to ensure that the development of Ajv continues.
[<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/)

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:
- [GitHub sponsors page](https://github.com/sponsors/epoberezkin) (GitHub will match it)
- [Ajv Open Collective️](https://opencollective.com/ajv)
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).

Ajv also joined [OpenJS Foundation](https://openjsf.org/) – having this support will help ensure the longevity and stability of Ajv for all its users.

This [blog post](https://www.poberezkin.com/posts/2020-08-14-ajv-json-validator-mozilla-open-source-grant-openjs-foundation.html) has more details.

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.
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.

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.

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

- **90** contributors 🏗
- **5,000** dependent npm packages ⚙️
- **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) ⭐️
- **5,000,000** dependent repositories on GitHub 🚀
- **120,000,000** npm downloads per month! 💯
Since I asked to support Ajv development 40 people and 6 organizations contributed via GitHub and OpenCollective - this support helped receiving the MOSS grant!

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.
Your continuing support is very important - the funds will be used to develop and maintain Ajv once the next major version is released.

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

Thank you.


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

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).


## Install

Expand Down
46 changes: 18 additions & 28 deletions lib/dot/coerce.def
Original file line number Diff line number Diff line change
Expand Up @@ -4,55 +4,45 @@
, $coerced = 'coerced' + $lvl;
}}
var {{=$dataType}} = typeof {{=$data}};
{{? it.opts.coerceTypes == 'array'}}
if ({{=$dataType}} == 'object' && Array.isArray({{=$data}})) {{=$dataType}} = 'array';
{{?}}

var {{=$coerced}} = undefined;

{{ var $bracesCoercion = ''; }}
{{~ $coerceToTypes:$type:$i }}
{{? $i }}
if ({{=$coerced}} === undefined) {
{{ $bracesCoercion += '}'; }}
{{?}}

{{? it.opts.coerceTypes == 'array' && $type != 'array' }}
if ({{=$dataType}} == 'array' && {{=$data}}.length == 1) {
{{=$coerced}} = {{=$data}} = {{=$data}}[0];
{{=$dataType}} = typeof {{=$data}};
/*if ({{=$dataType}} == 'object' && Array.isArray({{=$data}})) {{=$dataType}} = 'array';*/
}
{{?}}
{{? it.opts.coerceTypes == 'array' }}
if ({{=$dataType}} == 'object' && Array.isArray({{=$data}}) && {{=$data}}.length == 1) {
{{=$data}} = {{=$data}}[0];
{{=$dataType}} = typeof {{=$data}};
if ({{=it.util.checkDataType(it.schema.type, $data, it.opts.strictNumbers)}}) {{=$coerced}} = {{=$data}};
}
{{?}}

if ({{=$coerced}} !== undefined) ;
{{~ $coerceToTypes:$type:$i }}
{{? $type == 'string' }}
if ({{=$dataType}} == 'number' || {{=$dataType}} == 'boolean')
else if ({{=$dataType}} == 'number' || {{=$dataType}} == 'boolean')
{{=$coerced}} = '' + {{=$data}};
else if ({{=$data}} === null) {{=$coerced}} = '';
{{?? $type == 'number' || $type == 'integer' }}
if ({{=$dataType}} == 'boolean' || {{=$data}} === null
else if ({{=$dataType}} == 'boolean' || {{=$data}} === null
|| ({{=$dataType}} == 'string' && {{=$data}} && {{=$data}} == +{{=$data}}
{{? $type == 'integer' }} && !({{=$data}} % 1){{?}}))
{{=$coerced}} = +{{=$data}};
{{?? $type == 'boolean' }}
if ({{=$data}} === 'false' || {{=$data}} === 0 || {{=$data}} === null)
else if ({{=$data}} === 'false' || {{=$data}} === 0 || {{=$data}} === null)
{{=$coerced}} = false;
else if ({{=$data}} === 'true' || {{=$data}} === 1)
{{=$coerced}} = true;
{{?? $type == 'null' }}
if ({{=$data}} === '' || {{=$data}} === 0 || {{=$data}} === false)
else if ({{=$data}} === '' || {{=$data}} === 0 || {{=$data}} === false)
{{=$coerced}} = null;
{{?? it.opts.coerceTypes == 'array' && $type == 'array' }}
if ({{=$dataType}} == 'string' || {{=$dataType}} == 'number' || {{=$dataType}} == 'boolean' || {{=$data}} == null)
else if ({{=$dataType}} == 'string' || {{=$dataType}} == 'number' || {{=$dataType}} == 'boolean' || {{=$data}} == null)
{{=$coerced}} = [{{=$data}}];
{{?}}
{{~}}

{{= $bracesCoercion }}

if ({{=$coerced}} === undefined) {
else {
{{# def.error:'type' }}
} else {
}

if ({{=$coerced}} !== undefined) {
{{# def.setParentData }}
{{=$data}} = {{=$coerced}};
{{? !$dataLvl }}if ({{=$parentData}} !== undefined){{?}}
Expand Down
3 changes: 2 additions & 1 deletion lib/dot/definitions.def
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@

{{## def.nonEmptySchema:_schema:
(it.opts.strictKeywords
? typeof _schema == 'object' && Object.keys(_schema).length > 0
? (typeof _schema == 'object' && Object.keys(_schema).length > 0)
|| _schema === false
: it.util.schemaHasRules(_schema, it.RULES.all))
#}}

Expand Down
2 changes: 1 addition & 1 deletion lib/dot/validate.jst
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
it.baseId = it.baseId || it.rootId;
delete it.isTop;

it.dataPathArr = [undefined];
it.dataPathArr = [""];

if (it.schema.default !== undefined && it.opts.useDefaults && it.opts.strictDefaults) {
var $defaultMsg = 'default is ignored in the schema root';
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ajv",
"version": "6.12.3",
"version": "6.12.4",
"description": "Another JSON Schema Validator",
"main": "lib/ajv.js",
"typings": "lib/ajv.d.ts",
Expand Down
3 changes: 2 additions & 1 deletion spec/boolean.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ describe('boolean schemas', function() {
ajvs = [
new Ajv,
new Ajv({allErrors: true}),
new Ajv({inlineRefs: false})
new Ajv({inlineRefs: false}),
new Ajv({strictKeywords: true}),
];
});

Expand Down
28 changes: 23 additions & 5 deletions spec/coercion.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,32 +144,50 @@ var coercionArrayRules = JSON.parse(JSON.stringify(coercionRules));
coercionArrayRules.string.array = [
{ from: ['abc'], to: 'abc' },
{ from: [123], to: '123' },
{ from: [true], to: 'true'},
{ from: [null], to: ''},
{ from: [{}], to: undefined },
{ from: ['abc', 'def'], to: undefined },
{ from: [], to: undefined }
];
coercionArrayRules.number.array = [
{ from: [1.5], to: 1.5 },
{ from: ['1.5'], to: 1.5 }
{ from: ['1.5'], to: 1.5 },
{ from: [true], to: 1 },
{ from: [null], to: 0 },
{ from: ['abc'], to: undefined },
{ from: [{}], to: undefined },
];
coercionArrayRules.integer.array = [
{ from: [1], to: 1 },
{ from: ['1'], to: 1 },
{ from: [true], to: 1 },
{ from: [null], to: 0 }
{ from: [null], to: 0 },
{ from: [1.5], to: undefined },
{ from: ['abc'], to: undefined },
{ from: [{}], to: undefined },
];
coercionArrayRules.boolean.array = [
{ from: [true], to: true },
{ from: ['true'], to: true },
{ from: [1], to: true }
{ from: [1], to: true },
{ from: [null], to: false },
{ from: ['abc'], to: undefined },
{ from: [2], to: undefined },
{ from: [{}], to: undefined },
];
coercionArrayRules.null.array = [
{ from: [null], to: null },
{ from: [''], to: null },
{ from: [0], to: null },
{ from: [false], to: null }
{ from: [false], to: null },
{ from: ['abc'], to: undefined },
{ from: [1], to: undefined },
{ from: [true], to: undefined },
{ from: [{}], to: undefined },
];
coercionArrayRules.object.array = [
{ from: [{}], to: undefined }
{ from: [{}], to: undefined }
];

coercionArrayRules.array = {
Expand Down

0 comments on commit a3c7eba

Please sign in to comment.