Comparing version 0.7.6 to 0.8.0
@@ -1,20 +0,6 @@ | ||
import { Exception } from 'typesafe-exception'; | ||
import { Schema } from './schema'; | ||
import { SchemaStore } from './store'; | ||
export declare class ValidationError extends Exception<{ | ||
errors: DecodeError[]; | ||
}> { | ||
status: number; | ||
constructor(schema: Schema<unknown>, errors: DecodeError[]); | ||
protected formatMessage(errors: DecodeError[]): string; | ||
} | ||
export interface DecodeError { | ||
path: string[]; | ||
message: string; | ||
} | ||
export interface DecodeOptions { | ||
throw?: boolean; | ||
refs?: Schema<unknown>[]; | ||
} | ||
export declare function decode<T>(schema: Schema<T>, value: unknown, options?: DecodeOptions): T; | ||
export declare class SchemaDecoder<T> { | ||
@@ -24,3 +10,4 @@ readonly schema: Schema<T>; | ||
constructor(schema: Schema<T>, store?: SchemaStore); | ||
create(partials: Partial<T>): T; | ||
decode(value: unknown, options?: DecodeOptions): T; | ||
} |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.SchemaDecoder = exports.decode = exports.ValidationError = void 0; | ||
const typesafe_exception_1 = require("typesafe-exception"); | ||
exports.SchemaDecoder = void 0; | ||
const job_1 = require("./job"); | ||
const store_1 = require("./store"); | ||
const util_1 = require("./util"); | ||
class ValidationError extends typesafe_exception_1.Exception { | ||
constructor(schema, errors) { | ||
var _a; | ||
super('ValidationError', { errors }); | ||
this.status = 400; | ||
const type = util_1.capitalize((_a = schema.id) !== null && _a !== void 0 ? _a : schema.type); | ||
const msg = this.formatMessage(errors); | ||
this.message = `${type} validation failed:\n${msg}`; | ||
} | ||
formatMessage(errors) { | ||
return errors.map(e => { | ||
const pointer = e.path.join('.'); | ||
return ` - ${pointer} ${e.path}`; | ||
}).join('\n'); | ||
} | ||
} | ||
exports.ValidationError = ValidationError; | ||
function decode(schema, value, options = {}) { | ||
return new SchemaDecoder(schema).decode(value, options); | ||
} | ||
exports.decode = decode; | ||
class SchemaDecoder { | ||
@@ -34,2 +11,5 @@ constructor(schema, store) { | ||
} | ||
create(partials) { | ||
return this.decode(partials, { throw: false }); | ||
} | ||
decode(value, options = {}) { | ||
@@ -36,0 +16,0 @@ return new job_1.DecodeJob(this, value, options).decode(); |
export * from './coerce'; | ||
export * from './decode'; | ||
export * from './decoder'; | ||
export * from './defaults'; | ||
export * from './errors'; | ||
export * from './job'; | ||
export * from './schema'; | ||
export * from './store'; |
@@ -14,6 +14,8 @@ "use strict"; | ||
__exportStar(require("./coerce"), exports); | ||
__exportStar(require("./decode"), exports); | ||
__exportStar(require("./decoder"), exports); | ||
__exportStar(require("./defaults"), exports); | ||
__exportStar(require("./errors"), exports); | ||
__exportStar(require("./job"), exports); | ||
__exportStar(require("./schema"), exports); | ||
__exportStar(require("./store"), exports); |
@@ -1,2 +0,3 @@ | ||
import { DecodeError, DecodeOptions, SchemaDecoder } from './decoder'; | ||
import { DecodeOptions, SchemaDecoder } from './decoder'; | ||
import { DecodeError } from './errors'; | ||
import { ArraySchema, NumberSchema, ObjectSchema, Schema, SchemaType, StringSchema } from './schema'; | ||
@@ -10,9 +11,9 @@ export declare class DecodeJob<T> { | ||
decode(): T; | ||
protected decodeAny<T>(schema: Schema<T>, value: unknown, path: string[]): any; | ||
decodeNumber(schema: NumberSchema, value: unknown, path: string[]): number; | ||
decodeString(schema: StringSchema, value: unknown, path: string[]): string; | ||
decodeObject<T>(schema: ObjectSchema<T>, value: unknown, path: string[]): any; | ||
decodeArray<T>(schema: ArraySchema<T>, value: unknown, path: string[]): T[]; | ||
decodeRef(schemaId: string, value: unknown, path: string[]): any; | ||
defaultValue(schema: { | ||
protected decodeAny<T>(schema: Schema<T>, value: unknown, path: string): any; | ||
protected decodeNumber(schema: NumberSchema, value: unknown, path: string): number; | ||
protected decodeString(schema: StringSchema, value: unknown, path: string): string; | ||
protected decodeObject<T>(schema: ObjectSchema<T>, value: unknown, path: string): any; | ||
protected decodeArray<T>(schema: ArraySchema<T>, value: unknown, path: string): T[]; | ||
protected decodeRef(schemaId: string, value: unknown, path: string): any; | ||
protected defaultValue(schema: { | ||
type: SchemaType; | ||
@@ -19,0 +20,0 @@ default?: any; |
@@ -5,4 +5,4 @@ "use strict"; | ||
const coerce_1 = require("./coerce"); | ||
const decoder_1 = require("./decoder"); | ||
const defaults_1 = require("./defaults"); | ||
const errors_1 = require("./errors"); | ||
const util_1 = require("./util"); | ||
@@ -17,5 +17,5 @@ class DecodeJob { | ||
decode() { | ||
const res = this.decodeAny(this.decoder.schema, this.value, []); | ||
const res = this.decodeAny(this.decoder.schema, this.value, ''); | ||
if (this.options.throw && this.errors.length > 0) { | ||
throw new decoder_1.ValidationError(this.decoder.schema, this.errors); | ||
throw new errors_1.ValidationError(this.decoder.schema, this.errors); | ||
} | ||
@@ -108,3 +108,3 @@ return res; | ||
const value = original[key]; | ||
const decoded = this.decodeAny(propSchema, value, path.concat([key])); | ||
const decoded = this.decodeAny(propSchema, value, `${path}.${key}`); | ||
if (decoded !== undefined) { | ||
@@ -120,3 +120,3 @@ result[key] = decoded; | ||
} | ||
result[key] = this.decodeAny(schema.additionalProperties, value, path.concat([key])); | ||
result[key] = this.decodeAny(schema.additionalProperties, value, `${path}.${key}`); | ||
} | ||
@@ -130,3 +130,3 @@ } | ||
for (const value of original) { | ||
const item = this.decodeAny(schema.items, value, path.concat(['*'])); | ||
const item = this.decodeAny(schema.items, value, `${path}.*`); | ||
result.push(item); | ||
@@ -133,0 +133,0 @@ } |
{ | ||
"name": "airtight", | ||
"version": "0.7.6", | ||
"version": "0.8.0", | ||
"description": "Tight subset of JSON schema", | ||
@@ -5,0 +5,0 @@ "main": "out/main/index.js", |
17824
22
477