Skip to content

Commit

Permalink
refactor: use named exports
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Use named exports instead of single exported object.
Replace:
  import clean from "data-cleaner"
with:
  import * as clean from "data-cleaner"

Refs #7
  • Loading branch information
IlyaSemenov committed May 8, 2019
1 parent 037e709 commit b42a23a
Show file tree
Hide file tree
Showing 12 changed files with 37 additions and 97 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ See [Comparison to other libraries](#comparison-to-other-libraries) below.
## Minimal example

```js
import clean from 'data-cleaner'
import * as clean from 'data-cleaner'

const cleanUser = clean.object({
name: clean.string(),
Expand All @@ -43,7 +43,8 @@ yarn add data-cleaner
Then import or require:

```js
import clean, { ValidationError } from 'data-cleaner'
import * as clean from 'data-cleaner'
import { ValidationError } from 'data-cleaner'

// or

Expand Down Expand Up @@ -658,7 +659,8 @@ Define a *cleaner* for imaginary department visitor registration form with the f
Use imaginary async data access library for data validation.
```js
import clean, { ValidationError } from 'data-cleaner'
import * as clean from 'data-cleaner'
import { ValidationError } from 'data-cleaner'

const cleanVisitorData = clean.object({
fields: {
Expand Down
2 changes: 1 addition & 1 deletion src/cleaners/any.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function setSchema<C extends Cleaner<any, any>>(fn: C, schema: any) {
return fn as WithSchema<C>
}

export default function cleanAny<T = any, V = T>(schema: AnySchema<T, V> = {}) {
export function cleanAny<T = any, V = T>(schema: AnySchema<T, V> = {}) {
if (schema.default !== undefined) {
if (schema.required === undefined) {
schema.required = false
Expand Down
4 changes: 2 additions & 2 deletions src/cleaners/array.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getMessage } from '../utils'
import { ValidationError, ErrorMessages } from '../errors/ValidationError'
import cleanAny, { AnySchema, setSchema } from './any'
import { cleanAny, setSchema, AnySchema } from './any'
import { Cleaner } from '../types'

export interface ArraySchema<E, T, V> extends AnySchema<T, V> {
Expand All @@ -9,7 +9,7 @@ export interface ArraySchema<E, T, V> extends AnySchema<T, V> {
max?: number
}

export default function cleanArray<E = any, T = E[], V = T>(
export function cleanArray<E = any, T = E[], V = T>(
schema: ArraySchema<E, T, V> = {},
) {
const cleaner = cleanAny<T, V>({
Expand Down
4 changes: 2 additions & 2 deletions src/cleaners/boolean.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { getMessage } from '../utils'
import { ValidationError } from '../errors/ValidationError'
import cleanAny, { AnySchema, setSchema } from './any'
import { cleanAny, setSchema, AnySchema } from './any'

export interface BooleanSchema<T, V> extends AnySchema<T, V> {
cast?: boolean
omit?: boolean
}

export default function cleanBoolean<T = boolean, V = T>(
export function cleanBoolean<T = boolean, V = T>(
schema: BooleanSchema<T, V> = {},
) {
const cleaner = cleanAny<T, V>({
Expand Down
6 changes: 2 additions & 4 deletions src/cleaners/date.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import cleanString, { StringSchema } from './string'
import { cleanString, StringSchema } from './string'
import { SchemaError } from '../errors/SchemaError'
import { ValidationError } from '../errors/ValidationError'
import { getMessage } from '../utils'
Expand All @@ -9,9 +9,7 @@ export interface DateSchema<T, V> extends StringSchema<T, V> {
format?: null | 'iso'
}

export default function cleanDate<T = string, V = T>(
schema: DateSchema<T, V> = {},
) {
export function cleanDate<T = string, V = T>(schema: DateSchema<T, V> = {}) {
if (
!(
schema.format === undefined ||
Expand Down
6 changes: 2 additions & 4 deletions src/cleaners/email.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import { validate as validateEmail } from 'email-validator'
import cleanString, { StringSchema } from './string'
import { cleanString, StringSchema } from './string'
import { ValidationError } from '../errors/ValidationError'
import { getMessage } from '../utils'
import { setSchema } from './any'

export interface EmailSchema<T, V> extends StringSchema<T, V> {}

export default function cleanEmail<T = string, V = T>(
schema: EmailSchema<T, V> = {},
) {
export function cleanEmail<T = string, V = T>(schema: EmailSchema<T, V> = {}) {
const cleaner = cleanString<T, V>({
required: schema.required,
default: schema.default,
Expand Down
6 changes: 2 additions & 4 deletions src/cleaners/float.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
import cleanNumber, { NumberSchema } from './number'
import { cleanNumber, NumberSchema } from './number'
import { setSchema } from './any'

export interface FloatSchema<T, V> extends NumberSchema<T, V> {}

export default function cleanFloat<T = number, V = T>(
schema: FloatSchema<T, V> = {},
) {
export function cleanFloat<T = number, V = T>(schema: FloatSchema<T, V> = {}) {
const cleaner = cleanNumber<T, V>({
...schema,
parseNumber: parseFloat,
Expand Down
4 changes: 2 additions & 2 deletions src/cleaners/integer.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import cleanNumber, { NumberSchema } from './number'
import { cleanNumber, NumberSchema } from './number'
import { setSchema } from './any'

export interface IntegerSchema<T, V> extends NumberSchema<T, V> {}

export default function cleanInteger<T = number, V = T>(
export function cleanInteger<T = number, V = T>(
schema: IntegerSchema<T, V> = {},
) {
const cleaner = cleanNumber<T, V>({
Expand Down
4 changes: 2 additions & 2 deletions src/cleaners/number.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { getMessage } from '../utils'
import { ValidationError } from '../errors/ValidationError'
import { SchemaError } from '../errors/SchemaError'
import cleanAny, { AnySchema, setSchema } from './any'
import { cleanAny, setSchema, AnySchema } from './any'

export interface NumberSchema<T, V> extends AnySchema<T, V> {
cast?: boolean
Expand All @@ -13,7 +13,7 @@ export interface NumberParserSchema<T, V> extends NumberSchema<T, V> {
parseNumber: (value: any) => number
}

export default function cleanNumber<T = number, V = T>(
export function cleanNumber<T = number, V = T>(
schema: NumberParserSchema<T, V>,
) {
if (schema.parseNumber === undefined) {
Expand Down
6 changes: 2 additions & 4 deletions src/cleaners/object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
ErrorOptions,
} from '../errors/ValidationError'
import { Cleaner } from '../types'
import cleanAny, { AnySchema, setSchema } from './any'
import { cleanAny, setSchema, AnySchema } from './any'

export type Dict = Record<string, any>

Expand All @@ -21,9 +21,7 @@ export interface ObjectSchema<T, V> extends AnySchema<T, V> {
groupErrors?: boolean
}

export default function cleanObject<T = Dict, V = T>(
schema: ObjectSchema<T, V>,
) {
export function cleanObject<T = Dict, V = T>(schema: ObjectSchema<T, V>) {
if (!schema || typeof schema.fields !== 'object') {
throw new SchemaError('clean.object schema must include fields.')
}
Expand Down
4 changes: 2 additions & 2 deletions src/cleaners/string.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { getMessage } from '../utils'
import { SchemaError } from '../errors/SchemaError'
import { ValidationError } from '../errors/ValidationError'
import cleanAny, { AnySchema, setSchema } from './any'
import { cleanAny, setSchema, AnySchema } from './any'

export interface StringSchema<T, V> extends AnySchema<T, V> {
blank?: boolean | null
cast?: boolean
}

export default function cleanString<T = string, V = string>(
export function cleanString<T = string, V = string>(
schema: StringSchema<T, V> = {},
) {
if (schema.blank === null) {
Expand Down
80 changes: 13 additions & 67 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,73 +1,19 @@
// My original intent was to export default cleaners to be universally and easily used in CJS, ES6 and TS

// However, with simple exports (commented below) this will not work in TS:
// import clean from 'data-cleaner'
// insead, one will need to write:
// import * as clean from 'data-cleaner'
// which is not what I want.

// --------------------------------------

// export { default as any, AnySchema } from './cleaners/any'
// export { default as string, StringSchema } from './cleaners/string'
// export { default as date, DateSchema } from './cleaners/date'
// export { default as integer, IntegerSchema } from './cleaners/integer'
// export { default as float, FloatSchema } from './cleaners/float'
// export { default as boolean, BooleanSchema } from './cleaners/boolean'
// export { default as array, ArraySchema } from './cleaners/array'
// export { default as object, ObjectSchema } from './cleaners/object'

// export { SchemaError } from './errors/SchemaError'
// export { ValidationError } from './errors/ValidationError'

// export { CleanerOptions, Cleaner } from './types'

// --------------------------------------

// So for the time being I ended up with this:

import cleanAny from './cleaners/any'
import cleanString from './cleaners/string'
import cleanDate from './cleaners/date'
import cleanEmail from './cleaners/email'
import cleanInteger from './cleaners/integer'
import cleanFloat from './cleaners/float'
import cleanBoolean from './cleaners/boolean'
import cleanArray from './cleaners/array'
import cleanObject from './cleaners/object'
import { SchemaError } from './errors/SchemaError'
import { ValidationError } from './errors/ValidationError'

export default {
any: cleanAny,
string: cleanString,
date: cleanDate,
email: cleanEmail,
integer: cleanInteger,
float: cleanFloat,
boolean: cleanBoolean,
array: cleanArray,
object: cleanObject,
// TODO: convert the below to named exports??
SchemaError,
ValidationError,
}

// Export types as named exports.
// Unlike exporting of values, this does not break CJS/ES6 default export.

export { Cleaner } from './types'
export { AnySchema } from './cleaners/any'
export { StringSchema } from './cleaners/string'
export { DateSchema } from './cleaners/date'
export { EmailSchema } from './cleaners/email'
export { IntegerSchema } from './cleaners/integer'
export { FloatSchema } from './cleaners/float'
export { BooleanSchema } from './cleaners/boolean'
export { ArraySchema } from './cleaners/array'
export { ObjectSchema } from './cleaners/object'

export { cleanAny as any, AnySchema } from './cleaners/any'
export { cleanArray as array, ArraySchema } from './cleaners/array'
export { cleanBoolean as boolean, BooleanSchema } from './cleaners/boolean'
export { cleanDate as date, DateSchema } from './cleaners/date'
export { cleanEmail as email, EmailSchema } from './cleaners/email'
export { cleanFloat as float, FloatSchema } from './cleaners/float'
export { cleanInteger as integer, IntegerSchema } from './cleaners/integer'
export { cleanObject as object, ObjectSchema } from './cleaners/object'
export { cleanString as string, StringSchema } from './cleaners/string'

export { SchemaError } from './errors/SchemaError'
export {
ErrorMessage,
ErrorMessages,
FieldErrorMessages,
ValidationError,
} from './errors/ValidationError'

0 comments on commit b42a23a

Please sign in to comment.