Skip to content

Commit

Permalink
Rename SchemaError to ValidationResult
Browse files Browse the repository at this point in the history
PR-URL: #463
  • Loading branch information
tshemsedinov committed Nov 19, 2023
1 parent 707b91a commit 8d20257
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 26 deletions.
10 changes: 5 additions & 5 deletions lib/metadata.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const errPrefix = 'Field';
const OPTIONS = ['validate', 'parse', 'serialize', 'format'];
const metadataCollections = ['indexes', 'options'];

class SchemaError {
class ValidationResult {
#path;

constructor(path) {
Expand All @@ -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;
Expand Down Expand Up @@ -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) {
Expand All @@ -123,4 +123,4 @@ class SchemaMetadata {
}
}

module.exports = { SchemaMetadata, SchemaError };
module.exports = { SchemaMetadata, ValidationResult };
16 changes: 8 additions & 8 deletions lib/prototypes/abstract.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { SchemaError } = require('../metadata.js');
const { ValidationResult } = require('../metadata.js');
const { formatters, checks } = require('../util.js');

class AbstractType {
Expand All @@ -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)}`);
}
}

Expand Down
8 changes: 4 additions & 4 deletions lib/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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() {
Expand Down
12 changes: 6 additions & 6 deletions lib/struct.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { SchemaError } = require('./metadata.js');
const { ValidationResult } = require('./metadata.js');
const { formatters, isInstanceOf } = require('./util.js');

class Struct {
Expand All @@ -20,26 +20,26 @@ 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]);
for (const name of names) {
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;
}
}

Expand Down
6 changes: 3 additions & 3 deletions metaschema.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ interface Relation {
type: Cardinality;
}

interface SchemaError {
interface ValidationResult {
valid: boolean;
errors: string[];
}
Expand Down Expand Up @@ -65,13 +65,13 @@ export class Schema {
get types(): object;
checkConsistency(): Array<string>;
findReference(name: string): Schema;
check(value: any): SchemaError;
check(value: any): ValidationResult;
toInterface(): string;
attach(...namespaces: Array<Model>): void;
detouch(...namespaces: Array<Model>): 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;
Expand Down

0 comments on commit 8d20257

Please sign in to comment.