Skip to content
This repository was archived by the owner on Mar 7, 2024. It is now read-only.

Commit 3357c0b

Browse files
author
Morten Christensen
committed
bugfixes, struct null-check, comment field
1 parent b0caac1 commit 3357c0b

File tree

7 files changed

+32
-17
lines changed

7 files changed

+32
-17
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,10 @@ v1.0.0 Initial stable release
22
v2.0.0 ES5 retargeting for easier browser compatibility.
33
Removed isRoot.
44
v2.0.1 Updated dependencies to latest versions, incl. typescript to 2.1.5
5-
Arrays in model are now declared as ReadOnly.
5+
Arrays in model are now declared as ReadOnly.
6+
v2.1.1 Fixed JsonSchema bug.
7+
Made values in TypedField nullable
8+
Added optional comment fields.
9+
Turned on strict null-checks for typescript.
10+
Removed test files from distribution npm
11+

package.json

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "json-schema-js-gui-model",
3-
"version": "2.0.1",
3+
"version": "2.1.1",
44
"description": "Handy gui model and associated translator that is useful when constructing javascript UI forms from json-schemas",
55
"main": "dist/index.js",
66
"types": "dist/index.d.ts",
@@ -18,7 +18,10 @@
1818
"mapToGuiModel": "dist/cli/index.js"
1919
},
2020
"files": [
21-
"dist",
21+
"dist/cli",
22+
"dist/dependencies",
23+
"dist/lib",
24+
"dist/index*",
2225
"LICENSE",
2326
"CHANGELOG.md",
2427
"README.md"
@@ -44,15 +47,15 @@
4447
"@types/chai": "3.4.34",
4548
"@types/core-js": "0.9.35",
4649
"@types/mocha": "2.2.38",
47-
"@types/node": "7.0.0",
50+
"@types/node": "7.0.4",
4851
"chai": "3.5.0",
4952
"ghooks": "^2.0.0",
5053
"mocha": "3.2.0",
5154
"nodemon": "1.11.0",
5255
"npm-run-all": "4.0.1",
5356
"rimraf": "2.5.4",
5457
"ts-npm-lint": "0.1.0",
55-
"tslint": "4.3.1",
58+
"tslint": "4.4.2",
5659
"typescript": "2.1.5"
5760
},
5861
"engines": {

src/cli/index.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const usageString = 'Usage: ' + npmCmd + ' sourcePath destPath';
2020
/**
2121
* Help that abouts with error output
2222
*/
23-
function failure(msg) {
23+
function failure(msg: string) {
2424
console.error(msg);
2525
exit(-1);
2626
}
@@ -35,7 +35,7 @@ let sourcePath = userArgs[0];
3535
let destPath = userArgs[1];
3636

3737
// Read input:
38-
let source: string;
38+
let source: string = '';
3939

4040
if (!existsSync(sourcePath)) {
4141
failure('Error. Could not locate source file \"' + sourcePath + '\".');

src/dependencies/json-schema.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@
2323
*/
2424

2525
// Adapted from: https://gist.github.com/enriched/c84a2a99f886654149908091a3183e15
26-
// with following changes: format attribute added.
26+
// with a few changes such as:
27+
// * format attribute added.
28+
// * type of $schema is a string
2729

2830
export interface JsonSchema {
2931
$ref?: string;
@@ -39,7 +41,7 @@ export interface JsonSchema {
3941
* It is recommended that the meta-schema is
4042
* included in the root of any JSON Schema
4143
*/
42-
$schema?: JsonSchema;
44+
$schema?: string;
4345
/**
4446
* Title of the schema
4547
*/

src/lib/gui-model.mapper.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ function processProperties(obj: JsonSchema, dataKeyPath: string, schemaPath: str
4747
/**
4848
* Process a json schema key/value property definition.
4949
*/
50-
function processProperty(key: string, value: any, requiredItem: boolean, keyPath: string, schemaPath: string, accumulatedErrors: TranslationError[]): GuiElement {
50+
function processProperty(key: string, value: any, requiredItem: boolean, keyPath: string, schemaPath: string, accumulatedErrors: TranslationError[]): GuiElement | null {
5151
let type = value.type;
5252
if (!isString(type)) {
5353
addError(accumulatedErrors, 'Type elements must be strings (not ' + typeof type + ') ', schemaPath);
@@ -75,7 +75,7 @@ function processProperty(key: string, value: any, requiredItem: boolean, keyPath
7575

7676
validateField(type, dataSubType, defaultValue, enumValues, keyPath, schemaPath, requiredItem, accumulatedErrors);
7777

78-
let prop: GuiElement;
78+
let prop: GuiElement | null;
7979
switch (type) {
8080
case 'number': prop = createNumberField(key, keyPath, label, tooltip, defaultValue, requiredItem, dataSubType, enumValues);
8181
break;
@@ -141,7 +141,7 @@ function validateField(type: string, dataSubType: SubDataType, defaultValue: any
141141
* Create an immutable gui input dropdown element for a number, making any contained objects immutable in the process.
142142
*/
143143
function createNumberField(key: string, objectPath: string, label: string, tooltip: string, defaultValue: number,
144-
required: boolean, dataType: SubDataType, values: number[] = undefined): TypedField<number> {
144+
required: boolean, dataType: SubDataType, values: (number[] | undefined) = undefined): TypedField<number> {
145145
return Object.freeze<TypedField<number>>({
146146
kind: 'field',
147147
name: key,
@@ -161,7 +161,7 @@ function createNumberField(key: string, objectPath: string, label: string, toolt
161161
* Create an immutable gui input dropdown element for an integer, making any contained objects immutable in the process.
162162
*/
163163
function createIntegerField(key: string, objectPath: string, label: string, tooltip: string, defaultValue: number,
164-
required: boolean, dataType: SubDataType, values: number[] = undefined): TypedField<number> {
164+
required: boolean, dataType: SubDataType, values: (number[] | undefined) = undefined): TypedField<number> {
165165
return Object.freeze<TypedField<number>>({
166166
kind: 'field',
167167
name: key,
@@ -181,7 +181,7 @@ function createIntegerField(key: string, objectPath: string, label: string, tool
181181
* Create an immutable gui input element for a boolean, making any contained objects immutable in the process.
182182
*/
183183
function createBooleanField(key: string, objectPath: string, label: string, tooltip: string, defaultValue: boolean,
184-
required: boolean, dataType: SubDataType, values: boolean[] = undefined): TypedField<boolean> {
184+
required: boolean, dataType: SubDataType, values: (boolean[] | undefined) = undefined): TypedField<boolean> {
185185
return Object.freeze<TypedField<boolean>>({
186186
kind: 'field',
187187
name: key,
@@ -201,7 +201,7 @@ function createBooleanField(key: string, objectPath: string, label: string, tool
201201
* Create an immutable gui input dropdown element for a string, making any contained objects immutable in the process.
202202
*/
203203
function createStringField(key: string, objectPath: string, label: string, tooltip: string, defaultValue: string,
204-
required: boolean, dataType: SubDataType, values: string[] = undefined): TypedField<string> {
204+
required: boolean, dataType: SubDataType, values: (string[] | undefined) = undefined): TypedField<string> {
205205
return Object.freeze<TypedField<string>>({
206206
kind: 'field',
207207
name: key,

src/lib/gui-model.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ export interface GuiElementBase {
6060
*/
6161
readonly dataObjectPath: string;
6262
readonly required: boolean;
63+
readonly comment?: string;
6364
}
6465

6566

@@ -73,8 +74,8 @@ export interface FieldBase extends GuiElementBase {
7374
};
7475

7576
export interface TypedField<T> extends FieldBase {
76-
readonly defaultValue: T;
77-
readonly values?: ReadonlyArray<T>;
77+
readonly defaultValue: T | null;
78+
readonly values?: ReadonlyArray<T | null>;
7879
}
7980

8081
/**

tsconfig.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@
1313
"allowJs": false,
1414
"removeComments": false,
1515
"forceConsistentCasingInFileNames": true,
16+
"noImplicitAny": true,
1617
"noImplicitReturns": true,
18+
"suppressImplicitAnyIndexErrors": true,
1719
"noFallthroughCasesInSwitch": true,
20+
"strictNullChecks": true,
1821
"pretty": true,
1922
"listFiles": false,
2023
"listEmittedFiles": false

0 commit comments

Comments
 (0)