@peculiar/json-schema
Advanced tools
Comparing version 1.0.5 to 1.0.6
@@ -42,3 +42,3 @@ 'use strict'; | ||
has(target) { | ||
return this.items.has(target); | ||
return this.items.has(target) || !!this.findParentSchema(target); | ||
} | ||
@@ -53,3 +53,3 @@ get(target) { | ||
create(target) { | ||
const schema = { items: {} }; | ||
const schema = { items: {}, target }; | ||
const parentSchema = this.findParentSchema(target); | ||
@@ -79,7 +79,7 @@ if (parentSchema) { | ||
class JsonSerializer { | ||
static serialize(obj, replacer, space) { | ||
const json = this.toJSON(obj); | ||
static serialize(obj, targetSchema, replacer, space) { | ||
const json = this.toJSON(obj, targetSchema || undefined); | ||
return JSON.stringify(json, replacer, space); | ||
} | ||
static toJSON(obj) { | ||
static toJSON(obj, targetSchema) { | ||
let res; | ||
@@ -96,4 +96,8 @@ if (isConvertible(obj)) { | ||
else if (typeof obj === "object") { | ||
if (schemaStorage.has(obj.constructor)) { | ||
const schema = schemaStorage.get(obj.constructor); | ||
if (targetSchema && !schemaStorage.has(targetSchema)) { | ||
throw new Error("Cannot get schema for `targetSchema` param"); | ||
} | ||
targetSchema = (targetSchema || obj.constructor); | ||
if (schemaStorage.has(targetSchema)) { | ||
const schema = schemaStorage.get(targetSchema); | ||
res = {}; | ||
@@ -208,2 +212,6 @@ for (const key in schema.items) { | ||
schema = schemaStorage.get(target.constructor); | ||
if (schema.target !== target.constructor) { | ||
schema = schemaStorage.create(target.constructor); | ||
schemaStorage.set(target.constructor, schema); | ||
} | ||
} | ||
@@ -210,0 +218,0 @@ const copyOptions = Object.assign({ type: exports.JsonPropTypes.Any }, options); |
@@ -36,4 +36,9 @@ declare namespace JsonSchema { | ||
class JsonSerializer { | ||
public static serialize(obj: any, replacer?: (key: string, value: any) => any, space?: string | number): string; | ||
public static toJSON(obj: any): any; | ||
public static serialize( | ||
obj: any, | ||
targetSchema?: IEmptyConstructor<any> | null, | ||
replacer?: (key: string, value: any) => any, | ||
space?: string | number, | ||
): string; | ||
public static toJSON(obj: any, targetSchema?: IEmptyConstructor<any> | null): any; | ||
} | ||
@@ -40,0 +45,0 @@ |
{ | ||
"name": "@peculiar/json-schema", | ||
"version": "1.0.5", | ||
"version": "1.0.6", | ||
"description": "This package uses ES2015 decorators to simplify JSON schema creation and use", | ||
@@ -5,0 +5,0 @@ "main": "build/index.js", |
16350
257