Skip to content

Update of JSON schema definitions #1000

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions packages/core/src/generators/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,12 @@
THE SOFTWARE.
*/

import { JsonSchema } from '../models/jsonSchema';
import { JsonSchema4 } from '../models/jsonSchema4';

const ADDITIONAL_PROPERTIES = 'additionalProperties';
const REQUIRED_PROPERTIES = 'required';

type Properties = {[property: string]: JsonSchema};
type Properties = {[property: string]: JsonSchema4};

const distinct = (array: any[], discriminator: (item: any) => string): any[] => {
const known = {};
Expand All @@ -51,12 +51,12 @@ class Gen {

}

schemaObject = (data: Object): JsonSchema => {
const props = this.properties(data);
const schema: JsonSchema = {
'type': 'object',
'properties': props,
'additionalProperties': this.findOption(props)(ADDITIONAL_PROPERTIES)
schemaObject = (data: Object): JsonSchema4 => {
const props: Properties = this.properties(data);
const schema: JsonSchema4 = {
type: 'object',
properties: props,
additionalProperties: this.findOption(props)(ADDITIONAL_PROPERTIES)
};
const required = this.findOption(props)(REQUIRED_PROPERTIES);
if (required.length > 0) {
Expand All @@ -81,7 +81,7 @@ class Gen {
);
}

property = (data: any): JsonSchema => {
property = (data: any): JsonSchema4 => {
switch (typeof data) {
case 'string':
return { 'type': 'string' };
Expand All @@ -104,15 +104,15 @@ class Gen {
}
}

schemaObjectOrArray = (data: any): JsonSchema => {
schemaObjectOrArray = (data: any): JsonSchema4 => {
if (data instanceof Array) {
return this.schemaArray(data as any[]);
} else {
return this.schemaObject(data);
}
}

schemaArray = (data: any[]): JsonSchema => {
schemaArray = (data: any[]): JsonSchema4 => {
if (data.length > 0) {
const allProperties = data.map(this.property);
const uniqueProperties = distinct(allProperties, prop => JSON.stringify(prop));
Expand Down Expand Up @@ -144,9 +144,9 @@ class Gen {
* @param {any} options any additional options that may alter the generated JSON schema
* @returns {JsonSchema} the generated schema
*/
export const generateJsonSchema = (instance: Object, options: any = {}): JsonSchema => {
export const generateJsonSchema = (instance: Object, options: any = {}): JsonSchema4 => {

const findOption = (props: Properties) => (optionName: string) => {
const findOption = (props: Properties) => (optionName: string): boolean | string[] => {
switch (optionName) {
case ADDITIONAL_PROPERTIES:
if (options.hasOwnProperty(ADDITIONAL_PROPERTIES)) {
Expand Down
4 changes: 3 additions & 1 deletion packages/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,9 @@
*/
export * from './util';

export * from './models/jsonSchema';
export { JsonSchema } from './models/jsonSchema';
export { JsonSchema4 } from './models/jsonSchema4';
export { JsonSchema7 } from './models/jsonSchema7';
export * from './store';
export * from './actions';
import * as Actions from './actions';
Expand Down
180 changes: 3 additions & 177 deletions packages/core/src/models/jsonSchema.ts
Original file line number Diff line number Diff line change
@@ -1,178 +1,4 @@
/*
The MIT License

Copyright (c) 2018 EclipseSource Munich
https://github.com/eclipsesource/jsonforms

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
/**
* MIT License
*
* Copyright (c) 2016 Richard Adams (https://github.com/enriched)
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
import { JsonSchema4 } from './jsonSchema4';
import { JsonSchema7 } from './jsonSchema7';

export interface JsonSchema {
$ref?: string;
/////////////////////////////////////////////////
// Schema Metadata
/////////////////////////////////////////////////
/**
* This is important because it tells refs where
* the root of the document is located
*/
id?: string;
/**
* It is recommended that the meta-schema is
* included in the root of any JSON Schema
*/
$schema?: JsonSchema;
/**
* Title of the schema
*/
title?: string;
/**
* Schema description
*/
description?: string;
/**
* Default json for the object represented by
* this schema
*/
'default'?: any;

/////////////////////////////////////////////////
// Number Validation
/////////////////////////////////////////////////
/**
* The value must be a multiple of the number
* (e.g. 10 is a multiple of 5)
*/
multipleOf?: number;
maximum?: number;
/**
* If true maximum must be > value, >= otherwise
*/
exclusiveMaximum?: boolean;
minimum?: number;
/**
* If true minimum must be < value, <= otherwise
*/
exclusiveMinimum?: boolean;

/////////////////////////////////////////////////
// String Validation
/////////////////////////////////////////////////
maxLength?: number;
minLength?: number;
/**
* This is a regex string that the value must
* conform to
*/
pattern?: string;

/////////////////////////////////////////////////
// Array Validation
/////////////////////////////////////////////////
additionalItems?: boolean | JsonSchema;
items?: JsonSchema | JsonSchema[];
maxItems?: number;
minItems?: number;
uniqueItems?: boolean;

/////////////////////////////////////////////////
// Object Validation
/////////////////////////////////////////////////
maxProperties?: number;
minProperties?: number;
required?: string[];
additionalProperties?: boolean | JsonSchema;
/**
* Holds simple JSON Schema definitions for
* referencing from elsewhere.
*/
definitions?: {[key: string]: JsonSchema};
/**
* The keys that can exist on the object with the
* json schema that should validate their value
*/
properties?: {[property: string]: JsonSchema};
/**
* The key of this object is a regex for which
* properties the schema applies to
*/
patternProperties?: {[pattern: string]: JsonSchema};
/**
* If the key is present as a property then the
* string of properties must also be present.
* If the value is a JSON Schema then it must
* also be valid for the object if the key is
* present.
*/
dependencies?: {[key: string]: JsonSchema | string[]};

/////////////////////////////////////////////////
// Generic
/////////////////////////////////////////////////
/**
* Enumerates the values that this schema can be
* e.g.
* {"type": "string",
* "enum": ["red", "green", "blue"]}
*/
'enum'?: any[];
/**
* The basic type of this schema, can be one of
* [string, number, object, array, boolean, null]
* or an array of the acceptable types
*/
type?: string | string[];

/////////////////////////////////////////////////
// Combining Schemas
/////////////////////////////////////////////////
allOf?: JsonSchema[];
anyOf?: JsonSchema[];
oneOf?: JsonSchema[];
/**
* The entity being validated must not match this schema
*/
not?: JsonSchema;

format?: string;
const?: any;
}
export type JsonSchema = JsonSchema4 | JsonSchema7;
Loading