Skip to content

Commit

Permalink
feat(validathor): Rename args to modifiers (#17)
Browse files Browse the repository at this point in the history
* feat(lib): rename args to modifiers

This better indicates the expected intent to the end user

* feat(lib): bump version

* chore(lib): remove nonexistent schema export
  • Loading branch information
Kosai106 authored Jul 30, 2024
1 parent 962eca8 commit 51d34a6
Show file tree
Hide file tree
Showing 7 changed files with 25 additions and 16 deletions.
6 changes: 6 additions & 0 deletions packages/validathor/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# @nordic-ui/validathor

## 0.0.4

### Patch Changes

- Rename schema `args` to `modifiers` to better indicate intent to end user

## 0.0.3

### Patch Changes
Expand Down
2 changes: 1 addition & 1 deletion packages/validathor/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@nordic-ui/validathor",
"version": "0.0.3",
"version": "0.0.4",
"description": "A simple validation library",
"author": "Kevin Østerkilde <[email protected]>",
"main": "dist/index.cjs.js",
Expand Down
6 changes: 3 additions & 3 deletions packages/validathor/src/schemas/boolean.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import type { Parser } from '@/types'
import { assert, TypeError } from '@/utils'
import { ERROR_CODES } from '@/utils/errors/errorCodes'

export type BooleanSchemaArgs = Array<Custom<boolean>>
export type BooleanSchemaModifiers = Array<Custom<boolean>>

export const boolean = (
args?: BooleanSchemaArgs,
modifiers?: BooleanSchemaModifiers,
message?: {
type_error?: string
},
Expand All @@ -19,7 +19,7 @@ export const boolean = (
new TypeError(message?.type_error || ERROR_CODES.ERR_TYP_7000.message()),
)

validateModifiers(value, args)
validateModifiers(value, modifiers)

return value
},
Expand Down
9 changes: 6 additions & 3 deletions packages/validathor/src/schemas/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@ import type { Parser } from '@/types'
import { assert, TypeError } from '@/utils'
import { ERROR_CODES } from '@/utils/errors/errorCodes'

export type DateSchemaArgs = Array<Min<Date> | Max<Date> | Custom<Date>>
export type DateSchemaModifiers = Array<Min<Date> | Max<Date> | Custom<Date>>

export const date = (args?: DateSchemaArgs, message?: { type_error?: string }): Parser<Date> => ({
export const date = (
modifiers?: DateSchemaModifiers,
message?: { type_error?: string },
): Parser<Date> => ({
name: 'date' as const,
parse: (value: unknown) => {
assert(
value instanceof Date,
new TypeError(message?.type_error || ERROR_CODES.ERR_TYP_3000.message()),
)

validateModifiers(value, args)
validateModifiers(value, modifiers)

return value
},
Expand Down
6 changes: 3 additions & 3 deletions packages/validathor/src/schemas/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ import type { Parser } from '@/types'
import { assert, TypeError, ValidationError } from '@/utils'
import { ERROR_CODES } from '@/utils/errors/errorCodes'

export type EnumSchemaArgs = Array<string | number>
export type EnumSchemaModifiers = Array<string | number>

export const enum_ = (
args?: EnumSchemaArgs,
modifiers?: EnumSchemaModifiers,
message?: {
type_error?: string
error?: string | ((value: string | number) => string)
Expand All @@ -19,7 +19,7 @@ export const enum_ = (
)

assert(
(args ?? []).includes(value),
(modifiers ?? []).includes(value),
new ValidationError(
typeof message?.error === 'function'
? message?.error(value)
Expand Down
6 changes: 3 additions & 3 deletions packages/validathor/src/schemas/number.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import type { Parser } from '@/types'
import { assert, TypeError } from '@/utils'
import { ERROR_CODES } from '@/utils/errors/errorCodes'

export type NumberSchemaArgs = Array<
export type NumberSchemaModifiers = Array<
Min<number> | Max<number> | Enumerator<number> | Custom<number>
>

export const number = (
args?: NumberSchemaArgs,
modifiers?: NumberSchemaModifiers,
message?: {
type_error?: string
},
Expand All @@ -25,7 +25,7 @@ export const number = (
new TypeError(message?.type_error || ERROR_CODES.ERR_TYP_1001.message()),
)

validateModifiers(value, args)
validateModifiers(value, modifiers)

return value
},
Expand Down
6 changes: 3 additions & 3 deletions packages/validathor/src/schemas/string.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ import { assert, TypeError } from '@/utils'
import { ERROR_CODES } from '@/utils/errors/errorCodes'

/** An array of the accepted modifier types */
export type StringSchemaArgs = Array<
export type StringSchemaModifiers = Array<
Min<string> | Max<string> | Email | Enumerator<string> | Custom<string>
>

export const string = (
args?: StringSchemaArgs,
modifiers?: StringSchemaModifiers,
message?: {
type_error?: string
},
Expand All @@ -24,7 +24,7 @@ export const string = (
)
assert(value !== '', new TypeError(message?.type_error || ERROR_CODES.ERR_VAL_2000.message()))

validateModifiers(value, args)
validateModifiers(value, modifiers)

return value
},
Expand Down

0 comments on commit 51d34a6

Please sign in to comment.