From 8d2025740c419374d7e25227c6c9d68f1e24061f Mon Sep 17 00:00:00 2001 From: Timur Shemsedinov Date: Sun, 19 Nov 2023 09:33:53 +0200 Subject: [PATCH] Rename `SchemaError` to `ValidationResult` PR-URL: https://github.com/metarhia/metaschema/pull/463 --- lib/metadata.js | 10 +++++----- lib/prototypes/abstract.js | 16 ++++++++-------- lib/schema.js | 8 ++++---- lib/struct.js | 12 ++++++------ metaschema.d.ts | 6 +++--- 5 files changed, 26 insertions(+), 26 deletions(-) diff --git a/lib/metadata.js b/lib/metadata.js index d02e4a11..79b6a432 100644 --- a/lib/metadata.js +++ b/lib/metadata.js @@ -6,7 +6,7 @@ const errPrefix = 'Field'; const OPTIONS = ['validate', 'parse', 'serialize', 'format']; const metadataCollections = ['indexes', 'options']; -class SchemaError { +class ValidationResult { #path; constructor(path) { @@ -16,10 +16,10 @@ class SchemaError { } add(err) { - if (SchemaError.isInstance(err)) { + if (ValidationResult.isInstance(err)) { this.errors.push(...err.errors); } else { - const errs = SchemaError.format(err, this.#path); + const errs = ValidationResult.format(err, this.#path); if (errs) this.errors.push(...errs); } this.valid = this.errors.length === 0; @@ -114,7 +114,7 @@ class SchemaMetadata { validate(value, path) { if (!this.options.validate) return null; - const error = new SchemaError(path); + const error = new ValidationResult(path); try { return error.add(this.options.validate(value, path)); } catch (err) { @@ -123,4 +123,4 @@ class SchemaMetadata { } } -module.exports = { SchemaMetadata, SchemaError }; +module.exports = { SchemaMetadata, ValidationResult }; diff --git a/lib/prototypes/abstract.js b/lib/prototypes/abstract.js index 41680bb0..44d698ff 100644 --- a/lib/prototypes/abstract.js +++ b/lib/prototypes/abstract.js @@ -1,6 +1,6 @@ 'use strict'; -const { SchemaError } = require('../metadata.js'); +const { ValidationResult } = require('../metadata.js'); const { formatters, checks } = require('../util.js'); class AbstractType { @@ -27,19 +27,19 @@ class AbstractType { } check(value, path) { - const schemaError = new SchemaError(path); + const result = new ValidationResult(path); const isEmpty = value === null || value === undefined; - if (!this.required && isEmpty) return schemaError; + if (!this.required && isEmpty) return result; try { - schemaError.add(this.checkType(value, path)); - if (this.validate) schemaError.add(this.validate(value, path)); + result.add(this.checkType(value, path)); + if (this.validate) result.add(this.validate(value, path)); for (const [name, subCheck] of Object.entries(AbstractType.checks)) { if (!this[name]) continue; - schemaError.add(subCheck(value, this)); + result.add(subCheck(value, this)); } - return schemaError; + return result; } catch (err) { - return schemaError.add(`validation failed ${String(err)}`); + return result.add(`validation failed ${String(err)}`); } } diff --git a/lib/schema.js b/lib/schema.js index f8e85272..42b64737 100644 --- a/lib/schema.js +++ b/lib/schema.js @@ -3,7 +3,7 @@ const { isFirstUpper } = require('metautil'); const { TYPES } = require('./types.js'); const { Preprocessor } = require('./preprocessor.js'); -const { SchemaMetadata, SchemaError } = require('./metadata.js'); +const { SchemaMetadata, ValidationResult } = require('./metadata.js'); const { Struct } = require('./struct.js'); const { isInstanceOf } = require('./util.js'); @@ -67,9 +67,9 @@ class Schema extends SchemaMetadata { } check(source, path = this.name) { - const schemaError = new SchemaError(path); - schemaError.add(this.validate(source, path)); - return schemaError.add(this.fields.check(source, path)); + const result = new ValidationResult(path); + result.add(this.validate(source, path)); + return result.add(this.fields.check(source, path)); } toInterface() { diff --git a/lib/struct.js b/lib/struct.js index 3e888228..e21c7d8a 100644 --- a/lib/struct.js +++ b/lib/struct.js @@ -1,6 +1,6 @@ 'use strict'; -const { SchemaError } = require('./metadata.js'); +const { ValidationResult } = require('./metadata.js'); const { formatters, isInstanceOf } = require('./util.js'); class Struct { @@ -20,7 +20,7 @@ class Struct { } check(source, path = '') { - const schemaError = new SchemaError(path || this.name); + const result = new ValidationResult(path || this.name); const keys = Object.keys(source); const fields = Object.keys(this); const names = new Set([...fields, ...keys]); @@ -28,18 +28,18 @@ class Struct { const value = source[name]; const type = this[name]; if (!type) { - schemaError.add(`Field "${name}" is not expected`); + result.add(`Field "${name}" is not expected`); continue; } if (!isInstanceOf(type, 'Type')) continue; const nestedPath = path ? `${path}.${name}` : name; if (type.required && !keys.includes(name)) { - schemaError.add(`Field "${nestedPath}" is required`); + result.add(`Field "${nestedPath}" is required`); continue; } - schemaError.add(type.check(value, nestedPath)); + result.add(type.check(value, nestedPath)); } - return schemaError; + return result; } } diff --git a/metaschema.d.ts b/metaschema.d.ts index 5250a8d7..76cda536 100644 --- a/metaschema.d.ts +++ b/metaschema.d.ts @@ -26,7 +26,7 @@ interface Relation { type: Cardinality; } -interface SchemaError { +interface ValidationResult { valid: boolean; errors: string[]; } @@ -65,13 +65,13 @@ export class Schema { get types(): object; checkConsistency(): Array; findReference(name: string): Schema; - check(value: any): SchemaError; + check(value: any): ValidationResult; toInterface(): string; attach(...namespaces: Array): void; detouch(...namespaces: Array): void; toString(): string; toJSON(): object; - validate(value: any, path: string): SchemaError; + validate(value: any, path: string): ValidationResult; } export function createSchema(name: string, src: string): Schema;