Comparing version 4.1.1 to 4.2.0
import { DecodeError } from './errors.js'; | ||
import { Schema } from './schema.js'; | ||
import { ArraySchemaDef, NumberSchemaDef, ObjectSchemaDef, SchemaDef, SchemaDefType, StringSchemaDef } from './schema-def.js'; | ||
export interface DecodeOptions { | ||
throw: boolean; | ||
strictRequired: boolean; | ||
} | ||
export declare class DecodeJob<T> { | ||
readonly decoder: Schema<T>; | ||
readonly schema: Schema<T>; | ||
readonly value: unknown; | ||
options: DecodeOptions; | ||
errors: DecodeError[]; | ||
constructor(decoder: Schema<T>, value: unknown); | ||
constructor(schema: Schema<T>, value: unknown, options?: Partial<DecodeOptions>); | ||
decode(): T; | ||
@@ -10,0 +15,0 @@ protected decodeAny<T>(schema: SchemaDef<T>, value: unknown, path: string): any; |
@@ -6,11 +6,12 @@ import { coerce } from './coerce.js'; | ||
export class DecodeJob { | ||
constructor(decoder, value) { | ||
this.decoder = decoder; | ||
constructor(schema, value, options = {}) { | ||
this.schema = schema; | ||
this.value = value; | ||
this.errors = []; | ||
this.options = Object.assign({ throw: true, strictRequired: false }, options); | ||
} | ||
decode() { | ||
const res = this.decodeAny(this.decoder.schema, this.value, ''); | ||
if (this.errors.length > 0) { | ||
throw new ValidationError(this.decoder.schema, this.errors); | ||
const res = this.decodeAny(this.schema.schema, this.value, ''); | ||
if (this.options.throw && this.errors.length > 0) { | ||
throw new ValidationError(this.schema.schema, this.errors); | ||
} | ||
@@ -30,4 +31,5 @@ return res; | ||
value = this.defaultValue(schema); | ||
// This used to be an option, but decided to remove it to make less room for interpretation | ||
// this.errors.push({ path, message: 'must not be null' }); | ||
if (this.options.strictRequired && schema.default == null) { | ||
this.errors.push({ path, message: 'must not be null' }); | ||
} | ||
} | ||
@@ -131,3 +133,3 @@ // Any Schema | ||
decodeRef(schemaId, value, path) { | ||
const refSchema = this.decoder.getRef(schemaId); | ||
const refSchema = this.schema.getRef(schemaId); | ||
if (!refSchema) { | ||
@@ -134,0 +136,0 @@ this.errors.push({ path, message: `unknown type ${schemaId}` }); |
@@ -0,1 +1,2 @@ | ||
import { DecodeOptions } from './decode.js'; | ||
import { SchemaDef } from './schema-def.js'; | ||
@@ -8,3 +9,3 @@ import { DeepPartial } from './util.js'; | ||
create(partials: DeepPartial<T>): T; | ||
decode(value: unknown): T; | ||
decode(value: unknown, options?: Partial<DecodeOptions>): T; | ||
getRef(schemaId: string): SchemaDef | null; | ||
@@ -11,0 +12,0 @@ getRefs(): Map<string, import("./schema-def.js").UnknownSchemaDef>; |
@@ -10,4 +10,4 @@ import { DecodeJob } from './decode.js'; | ||
} | ||
decode(value) { | ||
return new DecodeJob(this, value).decode(); | ||
decode(value, options = {}) { | ||
return new DecodeJob(this, value, options).decode(); | ||
} | ||
@@ -14,0 +14,0 @@ getRef(schemaId) { |
{ | ||
"name": "airtight", | ||
"version": "4.1.1", | ||
"version": "4.2.0", | ||
"description": "JSON Schema inspired library for validating and decoding messages", | ||
@@ -5,0 +5,0 @@ "type": "module", |
31809
609