Skip to content

Commit

Permalink
Rename custom errors to custom error handlers for clarity
Browse files Browse the repository at this point in the history
  • Loading branch information
ooade committed Mar 6, 2020
1 parent fdb3a95 commit c7d363f
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 18 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,12 @@ const schema: Schema = {
}
};

const customErrors: CustomErrors = {
const CustomErrorHandlers: CustomErrorHandlers = {
domainError: ({ value, domain }) =>
`${value} doesn't satify the ${domain} requirement`
};

const validate = validator(schema, customErrors);
const validate = validator(schema, CustomErrorHandlers);
```

**Props available to the error types:**
Expand Down
14 changes: 7 additions & 7 deletions __test__/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import test from 'tape';
import validator, { CustomErrors, Schema } from '../src';
import validator, { CustomErrorHandlers, Schema } from '../src';

const domains = {
IS_INTEGER: (value: number) => Number.isInteger(value),
Expand Down Expand Up @@ -79,12 +79,12 @@ test('should use custom required error message', t => {
}
};

const customErrors: CustomErrors = {
const CustomErrorHandlers: CustomErrorHandlers = {
requiredError: ({ key }) => `You need to pass the required value for ${key}`
};

try {
const validate = validator(schema, customErrors);
const validate = validator(schema, CustomErrorHandlers);
validate({ height: 2 });
} catch (err) {
t.equal(
Expand All @@ -107,13 +107,13 @@ test('should use custom type error message', t => {
}
};

const customErrors: CustomErrors = {
const CustomErrorHandlers: CustomErrorHandlers = {
typeError: ({ value, type }) =>
`You passed the wrong type (${type}) with value (${value})`
};

try {
const validate = validator(schema, customErrors);
const validate = validator(schema, CustomErrorHandlers);
validate({ age: '12', height: 2 });
} catch (err) {
t.equal(
Expand All @@ -137,13 +137,13 @@ test('should use custom domain error message', t => {
}
};

const customErrors: CustomErrors = {
const CustomErrorHandlers: CustomErrorHandlers = {
domainError: ({ value, domain }) =>
`${value} doesn't satify the ${domain} requirement`
};

try {
const validate = validator(schema, customErrors);
const validate = validator(schema, CustomErrorHandlers);
validate({ age: -1, height: 2 });
} catch (err) {
t.equal(
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "yooda",
"version": "0.2.2",
"version": "0.2.3",
"description": "🧘‍♂️600b validation library with custom handlers and messages",
"source": "src/index.ts",
"main": "build/index.js",
Expand Down
19 changes: 11 additions & 8 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,16 @@ export interface Schema {
[key: string]: SchemaBody;
}

export type CustomErrors = {
export type CustomErrorHandlers = {
requiredError?: CustomErrorHandler<{ key: string }>;
typeError?: CustomErrorHandler<TypeErrorProps>;
domainError?: CustomErrorHandler<DomainErrorProps>;
};

const validator = (schema: Schema, customErrors?: CustomErrors) => {
const validator = (
schema: Schema,
CustomErrorHandlers?: CustomErrorHandlers
) => {
const handler: ProxyHandler<Data> = {
set(target, key: string, value: any) {
const targetSchema = schema[key];
Expand All @@ -48,8 +51,8 @@ const validator = (schema: Schema, customErrors?: CustomErrors) => {

if (typeOf(value) !== type) {
throw new TypeError(
customErrors?.typeError
? customErrors.typeError({
CustomErrorHandlers?.typeError
? CustomErrorHandlers.typeError({
key,
type,
value: JSON.stringify(value)
Expand All @@ -62,8 +65,8 @@ const validator = (schema: Schema, customErrors?: CustomErrors) => {
domainArr.forEach(domain => {
if (!domain(value)) {
throw new Error(
customErrors?.domainError
? customErrors.domainError({
CustomErrorHandlers?.domainError
? CustomErrorHandlers.domainError({
key,
type,
value: JSON.stringify(value),
Expand All @@ -87,8 +90,8 @@ const validator = (schema: Schema, customErrors?: CustomErrors) => {

if (required && !data[key]) {
throw new Error(
customErrors?.requiredError
? customErrors.requiredError({ key })
CustomErrorHandlers?.requiredError
? CustomErrorHandlers.requiredError({ key })
: `${key} is required`
);
}
Expand Down

0 comments on commit c7d363f

Please sign in to comment.