Skip to content

Commit 678bad5

Browse files
author
Sascha Goldhofer
committed
update: docs and configuration of getTemplate *addOptionalProps*
1 parent 56a7520 commit 678bad5

File tree

7 files changed

+116
-80
lines changed

7 files changed

+116
-80
lines changed

CHANGELOG.md

Lines changed: 74 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,34 @@
11
## Changelog
22

3-
- [Breaking] renamed `JSON` types and variables to `Json`
4-
- [Breaking] remove `oneOfSchema` helper property in favor of `getOneOfOrigin()` non-enumerable function
5-
- [Breaking] `getTemplate` will add only required properties per default. Use `addOptionalProps:true` to change this behaviour
6-
- [add] introduce `mergeSchema`, `reduceSchema` and `resolveDynamicSchema`
7-
- [refactor] move dynamic resolvers `anyOf`, `allOf`, `oneOf`, `dependencies` and `if` to features modules
3+
- [Breaking] renamed `JSON` types and variables to `Json`
4+
- [Breaking] remove `oneOfSchema` helper property in favor of `getOneOfOrigin()` non-enumerable function
5+
- [Breaking] `getTemplate` will add only required properties per default. Use `addOptionalProps:true` to change this behaviour
6+
- [Breaking] change `unique-items-error` to point to error for duplicated item.
7+
8+
- [add] introduce `mergeSchema`, `reduceSchema` and `resolveDynamicSchema`
9+
- [refactor] move dynamic resolvers `anyOf`, `allOf`, `oneOf`, `dependencies` and `if` to features modules
810

911
### 7.0.0
1012

1113
- changed core interface to draft for simpler configuration using a configuration map
1214

1315
**Breaking Changes**
1416

15-
- replaced `Core` interface by new `Draft` interface
16-
- changed export of `Interface` to `Draft`
17-
- changed export of `Interface` to `Draft`
18-
- renamed `addSchema` to `addRemoteSchema`
19-
- changed api of `compileSchema` to have an additional schema-parameter for rootSchema reference
20-
- changed `compileSchema` and `addRemoteSchema` to work on instance state, instead of global state
21-
- `addRemoteSchema`, `compileSchema` now requires draft instance as first parameter
22-
- removed direct export of following functions: `addValidator`, `compileSchema`, `createSchemaOf`, `each`, `eachSchema`, `getChildSchemaSelection`, `getSchema`, `getTemplate`, `isValid`, `step`, `validate`. They are still accessible under the draftConfigs of each draft-version
23-
- changed draft version of `JsonEditor` to draft07
17+
- replaced `Core` interface by new `Draft` interface
18+
- changed export of `Interface` to `Draft`
19+
- changed export of `Interface` to `Draft`
20+
- renamed `addSchema` to `addRemoteSchema`
21+
- changed api of `compileSchema` to have an additional schema-parameter for rootSchema reference
22+
- changed `compileSchema` and `addRemoteSchema` to work on instance state, instead of global state
23+
- `addRemoteSchema`, `compileSchema` now requires draft instance as first parameter
24+
- removed direct export of following functions: `addValidator`, `compileSchema`, `createSchemaOf`, `each`, `eachSchema`, `getChildSchemaSelection`, `getSchema`, `getTemplate`, `isValid`, `step`, `validate`. They are still accessible under the draftConfigs of each draft-version
25+
- changed draft version of `JsonEditor` to draft07
2426

2527
**Milestone**
2628

27-
- [] configurable and consistent draft api
28-
- [] expose all function under their draft-version
29-
- [] remove global states in remotes
30-
29+
- [] configurable and consistent draft api
30+
- [] expose all function under their draft-version
31+
- [] remove global states in remotes
3132

3233
### 6.1.0
3334

@@ -37,76 +38,77 @@
3738

3839
### version 4.0
3940

40-
- [] Fix -- latest benchmark tests
41-
- [] Fix -- iterate schema (using typeDefs)
42-
- [] Fix -- scopes per schema-instance
43-
- [] Fix -- insane $ref resolution 'node' can be in 'root/node' or 'root/folder/node'
44-
- [] Refactor -- remove duplication from resolveRef.strict and resolveRef.withOverwrite
45-
- [] Change -- improve function apis (param order, rootSchema per default)
46-
- [] Fix -- `getTemplate` to resolve $ref to infinity
41+
- [] Fix -- latest benchmark tests
42+
- [] Fix -- iterate schema (using typeDefs)
43+
- [] Fix -- scopes per schema-instance
44+
- [] Fix -- insane $ref resolution 'node' can be in 'root/node' or 'root/folder/node'
45+
- [] Refactor -- remove duplication from resolveRef.strict and resolveRef.withOverwrite
46+
- [] Change -- improve function apis (param order, rootSchema per default)
47+
- [] Fix -- `getTemplate` to resolve $ref to infinity
4748

4849
**Breaking Changes**
4950

50-
- `iterateSchema` renamed to `eachSchema`
51-
- `validate` and `isValid` changed signature from (schema, data, [pointer]) to (data, [schema], [pointer])
52-
- `validateAsync` changed signature from (schema, data, [pointer], [onError]) to (data, [{ schema, pointer, onError }])
53-
- `getTemplate` changed signature from (schema, data) to (data, [schema])
54-
- `getSchema` changed signature from (schema, data, [pointer]) to (pointer, [data], [schema])
55-
- `each` changed signature from (schema, data, [pointer]) to (data, [schema], [pointer])
56-
- `resolveOneOf` changed signature from (schema, data, [pointer]) to (data, [schema], [pointer])
57-
- `precompileSchema` renamed to `compileSchema`
51+
- `iterateSchema` renamed to `eachSchema`
52+
- `validate` and `isValid` changed signature from (schema, data, [pointer]) to (data, [schema], [pointer])
53+
- `validateAsync` changed signature from (schema, data, [pointer], [onError]) to (data, [{ schema, pointer, onError }])
54+
- `getTemplate` changed signature from (schema, data) to (data, [schema])
55+
- `getSchema` changed signature from (schema, data, [pointer]) to (pointer, [data], [schema])
56+
- `each` changed signature from (schema, data, [pointer]) to (data, [schema], [pointer])
57+
- `resolveOneOf` changed signature from (schema, data, [pointer]) to (data, [schema], [pointer])
58+
- `precompileSchema` renamed to `compileSchema`
5859

5960
**Milestone** consistent feature support
6061

61-
- [] no side-effects on added remote-schemas
62-
- [] rootSchema should always be compiled
63-
- [] Add missing support for allOf and anyOf type definitions in 'step' and 'getTemplate'
64-
- [] Complete schema support in iterateSchema
65-
62+
- [] no side-effects on added remote-schemas
63+
- [] rootSchema should always be compiled
64+
- [] Add missing support for allOf and anyOf type definitions in 'step' and 'getTemplate'
65+
- [] Complete schema support in iterateSchema
6666

6767
## 2017
6868

69-
- [~] Features -- Improve validation maps to add & hook (!) custom entries (WIP, Add tests)
70-
- [] Fix -- Return all errors in oneOf-validation
71-
- [] Feature -- Error progress notification for async validation
72-
- [] Refactor -- Keyword validators should only be called for defined keyword
73-
- [] Feature -- getSchema of patternProperties
69+
- [~] Features -- Improve validation maps to add & hook (!) custom entries (WIP, Add tests)
70+
- [] Fix -- Return all errors in oneOf-validation
71+
- [] Feature -- Error progress notification for async validation
72+
- [] Refactor -- Keyword validators should only be called for defined keyword
73+
- [] Feature -- getSchema of patternProperties
7474

7575
**Milestone** add remaining draft04 features
76-
- [] remote references
77-
- [] default format validations
78-
- [] definitions
79-
- [] dependencies
80-
- [] Features -- allOf
81-
- [] Features -- anyOf
82-
- [] Features -- type-array
83-
- [] Features -- patternProperties
84-
- [] Features -- uniqueItems
85-
- [] Features -- oneOf: fail for multiple matching oneof-schemas
86-
- [] Features -- oneOf: for non-arrays
87-
- [] Features -- required (array of properties). Currently every property is required by default
8876

77+
- [] remote references
78+
- [] default format validations
79+
- [] definitions
80+
- [] dependencies
81+
- [] Features -- allOf
82+
- [] Features -- anyOf
83+
- [] Features -- type-array
84+
- [] Features -- patternProperties
85+
- [] Features -- uniqueItems
86+
- [] Features -- oneOf: fail for multiple matching oneof-schemas
87+
- [] Features -- oneOf: for non-arrays
88+
- [] Features -- required (array of properties). Currently every property is required by default
8989

9090
## 16/12
9191

92-
- [] Testing (validate real json files)
93-
- [] Test + document core differences
94-
- [] Add async validation
92+
- [] Testing (validate real json files)
93+
- [] Test + document core differences
94+
- [] Add async validation
9595

9696
**Milestone** customizable default and form (json-editor) validation
97-
- [] Sanitize Errors
98-
- [] Features -- Add core: Form, fix core: Draft04 - by using separate functions
99-
- [] Add getTemplate to core (resolveOneOf)
100-
- [] Breaking -- Add sort of 'core' to customize validation, stepper, errors etc and reduce requried arguments
97+
98+
- [] Sanitize Errors
99+
- [] Features -- Add core: Form, fix core: Draft04 - by using separate functions
100+
- [] Add getTemplate to core (resolveOneOf)
101+
- [] Breaking -- Add sort of 'core' to customize validation, stepper, errors etc and reduce requried arguments
101102

102103
**Milestone** custom validator (form-validation, oneOfProperty)
103-
- [] Features -- additionalProperties: Boolean | Schema
104-
- [] Features -- additionalItems: Boolean | Schema
105-
- [] Features -- Add support for type "integer"
106-
- [] Features -- oneOf -> oneOfProperty ( + Documentation)
107-
- [] Breaking -- change isValid to return boolean
108-
- [] Breaking -- use `step` in isValid -- bad: circular dependencies with step -> guessOneOfSchema -> isValid --X-> step
109-
- [] Features -- items: [] schema (order/defined indices)
110-
- [] Features -- not
111-
- [] Features -- return custom errors in data validation
112-
- [] Basics
104+
105+
- [] Features -- additionalProperties: Boolean | Schema
106+
- [] Features -- additionalItems: Boolean | Schema
107+
- [] Features -- Add support for type "integer"
108+
- [] Features -- oneOf -> oneOfProperty ( + Documentation)
109+
- [] Breaking -- change isValid to return boolean
110+
- [] Breaking -- use `step` in isValid -- bad: circular dependencies with step -> guessOneOfSchema -> isValid --X-> step
111+
- [] Features -- items: [] schema (order/defined indices)
112+
- [] Features -- not
113+
- [] Features -- return custom errors in data validation
114+
- [] Basics

README.md

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -671,7 +671,7 @@ expect(resolvedSchema).to.deep.eq({
671671

672672
## Draft customization
673673

674-
[custom resolvers](#custom-resolvers) | [custom validators](#custom-validators) | [custom errors](#custom-errors)
674+
[getTemplate default options](#get-template-default-options) | [custom resolvers](#custom-resolvers) | [custom validators](#custom-validators) | [custom errors](#custom-errors)
675675

676676
Each `Draft` in `json-schema-library` is build around a [DraftConfig](./lib/draft/index.ts#19). A `DraftConfig` holds all _functions_ and _configurations_ for each json-schema drafts. The `DraftConfig` is your main way to alter or extend behaviour for `json-schema-library`. You can either create your own _draftConfig_ or adjust any existing _draftConfig_. For the current drafts (4-7), each _draftConfig_ is exposed along with its actual _class_. For example:
677677

@@ -695,6 +695,26 @@ new Draft07(mySchema, { resolveOneOf: resolveOneOfFuzzy });
695695
new Draft({ ...draft07Config, resolveOneOf: resolveOneOfFuzzy }, mySchema);
696696
```
697697

698+
### getTemplate default options
699+
700+
With **version 8** _json-schema-library_ has changed `getTemplate` to only add required properties per default. This can be changed on draft initialization, by passing `templateDefaultOptions` in the _draftConfig_:
701+
702+
```js
703+
const draft = new Draft(schema, {
704+
templateDefaultOptions: {
705+
addOptionalProps: true
706+
}
707+
});
708+
```
709+
710+
**Note:** You can still pass options to getTemplate overriding the draft default settings by:
711+
712+
```js
713+
const data = draft.getTemplate(data, draft.getSchema(), {
714+
addOptionalProps: true
715+
});
716+
```
717+
698718
### custom resolvers
699719

700720
A _resolver_ is a simple method implementing a specific feature of json-schema to retrieve a sub-schema. Implementing the signature of each resolver you can create and pass your own resolvers.
@@ -901,8 +921,9 @@ const error: JsonError = createError("EnumError", { data: { pointer: "#/location
901921

902922
### v8.0.0
903923

904-
- Renamed JSON to JSON, mainly: `JSONError` to `JsonError` and `JSONSchema` to `JsonSchema`
905-
- `getTemplate` only adds required properties. Can be changed by setting `addOptionalProps:true`
924+
- Renamed `JSONError` to `JsonError` and `JSONSchema` to `JsonSchema`
925+
- `getTemplate` only adds required properties. Behaviour can be changed by [getTemplate default options](#get-template-default-options)
926+
- internal schema property `oneOfSchema` has been replaced by `schema.getOneOfOrigin()`
906927

907928
### v7.0.0
908929

lib/config/settings.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,9 @@ export default {
22
DECLARATOR_ONEOF: "oneOfProperty",
33
GET_TEMPLATE_RECURSION_LIMIT: 1,
44
floatingPointPrecision: 10000,
5-
propertyBlacklist: ["_id"]
5+
propertyBlacklist: ["_id"],
6+
templateDefaultOptions: {
7+
addOptionalProps: false,
8+
removeInvalidData: false
9+
}
610
};

lib/draft04/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import TYPES from "../validation/type";
2020
import validate from "../validate";
2121
import { DraftConfig, Draft } from "../draft";
2222
import { JsonSchema } from "../types";
23+
import settings from "../config/settings";
2324

2425
const draft04Config: DraftConfig = {
2526
typeKeywords: {
@@ -93,7 +94,9 @@ const draft04Config: DraftConfig = {
9394
resolveOneOf,
9495
resolveRef,
9596
step,
96-
validate
97+
validate,
98+
99+
templateDefaultOptions: settings.templateDefaultOptions
97100
};
98101

99102
class Draft04 extends Draft {

lib/draft06/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import TYPES from "../draft06/validation/type";
2020
import validate from "../validate";
2121
import { DraftConfig, Draft } from "../draft";
2222
import { JsonSchema } from "../types";
23+
import settings from "../config/settings";
2324

2425
const draft06Config: DraftConfig = {
2526
typeKeywords: {
@@ -101,7 +102,9 @@ const draft06Config: DraftConfig = {
101102
resolveOneOf,
102103
resolveRef,
103104
step,
104-
validate
105+
validate,
106+
107+
templateDefaultOptions: settings.templateDefaultOptions
105108
};
106109

107110
class Draft06 extends Draft {

lib/draft07/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import TYPES from "../draft06/validation/type";
2020
import validate from "../validate";
2121
import { DraftConfig, Draft } from "../draft";
2222
import { JsonSchema } from "../types";
23+
import settings from "../config/settings";
2324

2425
const draft07Config: DraftConfig = {
2526
typeKeywords: {
@@ -101,7 +102,9 @@ const draft07Config: DraftConfig = {
101102
resolveOneOf,
102103
resolveRef,
103104
step,
104-
validate
105+
validate,
106+
107+
templateDefaultOptions: settings.templateDefaultOptions
105108
};
106109

107110
class Draft07 extends Draft {

test/unit/issues/issue38.getTemplate.anyOf.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { expect } from "chai";
22
import { Draft07 as Draft } from "../../../lib/draft07";
33

4-
describe.only("issue#38 - getTemplate anyOf should not modify valid default value", () => {
4+
describe("issue#38 - getTemplate anyOf should not modify valid default value", () => {
55
let draft: Draft;
66
beforeEach(() => {
77
draft = new Draft({

0 commit comments

Comments
 (0)