Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add disabling of validators #125

Merged
merged 6 commits into from
Jun 11, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { getGlobalValidationEnabled, setGlobalValidationEnabled } from './lib/configs';
import { Shapes } from './lib/Shapes';

export const s = new Shapes();

export * from './lib/configs';
export * from './lib/errors/BaseError';
export * from './lib/errors/CombinedError';
export * from './lib/errors/CombinedPropertyError';
Expand All @@ -15,4 +16,3 @@ export * from './lib/errors/ValidationError';
export * from './lib/Result';
export * from './type-exports';
export * from './utility-types';
export { getGlobalValidationEnabled, setGlobalValidationEnabled };
7 changes: 1 addition & 6 deletions src/validators/ArrayValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
arrayLengthRangeInclusive
} from '../constraints/ArrayLengthConstraints';
import type { IConstraint } from '../constraints/base/IConstraint';
import { getGlobalValidationEnabled } from '../lib/configs';
import type { BaseError } from '../lib/errors/BaseError';
import { CombinedPropertyError } from '../lib/errors/CombinedPropertyError';
import { ValidationError } from '../lib/errors/ValidationError';
Expand Down Expand Up @@ -79,11 +78,7 @@ export class ArrayValidator<T> extends BaseValidator<T[]> {
return Result.err(new ValidationError('s.array(T)', 'Expected an array', values));
}

if (this.isValidationEnabled === false) {
return Result.ok(values);
}

if (this.isValidationEnabled === null && !getGlobalValidationEnabled()) {
if (!this.shouldValidatorRunConstraints) {
return Result.ok(values);
}

Expand Down
14 changes: 9 additions & 5 deletions src/validators/BaseValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,7 @@ export abstract class BaseValidator<T> {
public parse(value: unknown): T {
// If validation is disabled (at the validator or global level), we only run the `handle` method, which will do some basic checks
// (like that the input is a string for a string validator)
if (this.isValidationEnabled === false) {
return this.handle(value).unwrap();
}

if (this.isValidationEnabled === null && !getGlobalValidationEnabled()) {
if (!this.shouldValidatorRunConstraints) {
return this.handle(value).unwrap();
}

Expand All @@ -90,6 +86,14 @@ export abstract class BaseValidator<T> {
return this.isValidationEnabled;
}

protected get shouldValidatorRunConstraints() {
vladfrangu marked this conversation as resolved.
Show resolved Hide resolved
if (this.isValidationEnabled === null) {
return getGlobalValidationEnabled();
}

return this.isValidationEnabled;
vladfrangu marked this conversation as resolved.
Show resolved Hide resolved
}

protected clone(): this {
const clone: this = Reflect.construct(this.constructor, [this.constraints]);
clone.isValidationEnabled = this.isValidationEnabled;
Expand Down
7 changes: 1 addition & 6 deletions src/validators/MapValidator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { IConstraint } from '../constraints/base/IConstraint';
import { getGlobalValidationEnabled } from '../lib/configs';
import type { BaseError } from '../lib/errors/BaseError';
import { CombinedPropertyError } from '../lib/errors/CombinedPropertyError';
import { ValidationError } from '../lib/errors/ValidationError';
Expand All @@ -25,11 +24,7 @@ export class MapValidator<K, V> extends BaseValidator<Map<K, V>> {
return Result.err(new ValidationError('s.map(K, V)', 'Expected a map', value));
}

if (this.isValidationEnabled === false) {
return Result.ok(value);
}

if (this.isValidationEnabled === null && !getGlobalValidationEnabled()) {
if (!this.shouldValidatorRunConstraints) {
return Result.ok(value);
}

Expand Down
7 changes: 1 addition & 6 deletions src/validators/ObjectValidator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { IConstraint } from '../constraints/base/IConstraint';
import { getGlobalValidationEnabled } from '../lib/configs';
import type { BaseError } from '../lib/errors/BaseError';
import { CombinedPropertyError } from '../lib/errors/CombinedPropertyError';
import { MissingPropertyError } from '../lib/errors/MissingPropertyError';
Expand Down Expand Up @@ -127,11 +126,7 @@ export class ObjectValidator<T extends NonNullObject> extends BaseValidator<T> {
return Result.err(new ValidationError('s.object(T)', 'Expected the value to not be an array', value));
}

if (this.isValidationEnabled === false) {
return Result.ok(value as T);
}

if (this.isValidationEnabled === null && !getGlobalValidationEnabled()) {
if (!this.shouldValidatorRunConstraints) {
return Result.ok(value as T);
}

Expand Down
7 changes: 1 addition & 6 deletions src/validators/RecordValidator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { IConstraint } from '../constraints/base/IConstraint';
import { getGlobalValidationEnabled } from '../lib/configs';
import type { BaseError } from '../lib/errors/BaseError';
import { CombinedPropertyError } from '../lib/errors/CombinedPropertyError';
import { ValidationError } from '../lib/errors/ValidationError';
Expand Down Expand Up @@ -31,11 +30,7 @@ export class RecordValidator<T> extends BaseValidator<Record<string, T>> {
return Result.err(new ValidationError('s.record(T)', 'Expected the value to not be an array', value));
}

if (this.isValidationEnabled === false) {
return Result.ok(value as Record<string, T>);
}

if (this.isValidationEnabled === null && !getGlobalValidationEnabled()) {
if (!this.shouldValidatorRunConstraints) {
return Result.ok(value as Record<string, T>);
}

Expand Down
7 changes: 1 addition & 6 deletions src/validators/SetValidator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { IConstraint } from '../constraints/base/IConstraint';
import { getGlobalValidationEnabled } from '../lib/configs';
import type { BaseError } from '../lib/errors/BaseError';
import { CombinedError } from '../lib/errors/CombinedError';
import { ValidationError } from '../lib/errors/ValidationError';
Expand All @@ -23,11 +22,7 @@ export class SetValidator<T> extends BaseValidator<Set<T>> {
return Result.err(new ValidationError('s.set(T)', 'Expected a set', values));
}

if (this.isValidationEnabled === false) {
return Result.ok(values);
}

if (this.isValidationEnabled === null && !getGlobalValidationEnabled()) {
if (!this.shouldValidatorRunConstraints) {
return Result.ok(values);
}

Expand Down
7 changes: 1 addition & 6 deletions src/validators/TupleValidator.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { IConstraint } from '../constraints/base/IConstraint';
import { getGlobalValidationEnabled } from '../lib/configs';
import type { BaseError } from '../lib/errors/BaseError';
import { CombinedPropertyError } from '../lib/errors/CombinedPropertyError';
import { ValidationError } from '../lib/errors/ValidationError';
Expand Down Expand Up @@ -27,11 +26,7 @@ export class TupleValidator<T extends any[]> extends BaseValidator<[...T]> {
return Result.err(new ValidationError('s.tuple(T)', `Expected an array of length ${this.validators.length}`, values));
}

if (this.isValidationEnabled === false) {
return Result.ok(values as [...T]);
}

if (this.isValidationEnabled === null && !getGlobalValidationEnabled()) {
if (!this.shouldValidatorRunConstraints) {
return Result.ok(values as [...T]);
}

Expand Down