Skip to content

Commit

Permalink
Rename markup validator → structured validator
Browse files Browse the repository at this point in the history
  • Loading branch information
jsamr committed Nov 21, 2022
1 parent 99002f5 commit 8ecd933
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ const env = cleanEnv(process.env, {
```
### TypeScript users

You can use either one of `makeValidator`, `makeExactValidator` and `makeMarkupValidator`
You can use either one of `makeValidator`, `makeExactValidator` and `makeStructuredValidator`
depending on your use case.

#### `makeValidator<BaseT>`
Expand Down Expand Up @@ -189,20 +189,20 @@ const MAX_RETRIES = int({ choices: [1, 2, 3, 4] })
As you can see in this instance, _the output type is exactly `number`, the parameter type of
`makeExactValidator`_. Also note that here, `int` is not parametrizable.

#### `makeMarkupValidator`
#### `makeStructuredValidator`

This validator is meant for inputs which can produce arbitrary output types (e.g. `json`).
The typing logic behaves differently here:

- `makeMarkupValidator` has no type parameter.
- `makeStructuredValidator` has no type parameter.
- When no types can be inferred from context, output type defaults to any.
- Otherwise, infers type from `default` or `devDefault`.
- Also allows validator parametrized types.

Below is an example of a validator for query parameters (e.g. `option1=foo&option2=bar`)

```ts
const queryParams = makeMarkupValidator((input: string) => {
const queryParams = makeStructuredValidator((input: string) => {
const params = new URLSearchParams(input)
return Object.fromEntries(params.entries())
})
Expand Down
4 changes: 2 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,12 @@ export interface BaseValidator<BaseT> {
<T extends BaseT>(spec: OptionalSpec<T>): OptionalValidatorSpec<T>
}

// Such validator inputs a markup language such as JSON.
// Such validator inputs a structured input format such as JSON.
// Because it can output complex types, including objects:
// - it has no supertype
// - it fallbacks to 'any' when no type information can be inferred
// from the spec object.
export interface MarkupValidator {
export interface StructuredValidator {
// Defaults to any when no argument (prevents 'unknown')
(): RequiredValidatorSpec<any>
// Allow overriding output type with type parameter
Expand Down
10 changes: 5 additions & 5 deletions src/validators.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Spec, ValidatorSpec, BaseValidator, MarkupValidator, ExactValidator } from './types'
import { Spec, ValidatorSpec, BaseValidator, StructuredValidator, ExactValidator } from './types'
import { EnvError } from './errors'

// Simplified adaptation of https://github.com/validatorjs/validator.js/blob/master/src/lib/isFQDN.js
Expand Down Expand Up @@ -74,7 +74,7 @@ export const makeExactValidator = <T>(parseFn: (input: string) => T): ExactValid
* It default the output type to any if no type inference can be made.
*
* ```ts
* const queryParams = makeMarkupValidator((input: string) => {
* const queryParams = makeStructuredValidator((input: string) => {
* // Implementation details
* })
* const OPTIONS = queryParams({ default: { option1: true, option2: false } })
Expand All @@ -84,8 +84,8 @@ export const makeExactValidator = <T>(parseFn: (input: string) => T): ExactValid
* @param parseFn - A function to parse and validate input.
* @returns A validator which output type is exactly `T`
*/
export const makeMarkupValidator = (parseFn: (input: string) => unknown): MarkupValidator => {
return internalMakeValidator(parseFn) as MarkupValidator
export const makeStructuredValidator = (parseFn: (input: string) => unknown): StructuredValidator => {
return internalMakeValidator(parseFn) as StructuredValidator
}

// We use exact validator here because narrowing down to either 'true' or 'false'
Expand Down Expand Up @@ -164,7 +164,7 @@ export const url = makeValidator<string>((x: string) => {
* })
* ```
*/
export const json = makeMarkupValidator((x: string) => {
export const json = makeStructuredValidator((x: string) => {
try {
return JSON.parse(x)
} catch (e) {
Expand Down
4 changes: 2 additions & 2 deletions tests/types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
num,
RequiredValidatorSpec,
OptionalValidatorSpec,
makeMarkupValidator,
makeStructuredValidator,
json,
makeValidator,
} from '../src'
Expand Down Expand Up @@ -149,7 +149,7 @@ describe('validators types', () => {
).toEqualTypeOf<RequiredValidatorSpec<'foo' | 'bar'>>()
})
test('structured data validator', () => {
const validator = makeMarkupValidator(() => ({}))
const validator = makeStructuredValidator(() => ({}))
expectTypeOf(validator()).toEqualTypeOf<RequiredValidatorSpec<any>>()
expectTypeOf(validator({ default: {} as any })).toEqualTypeOf<RequiredValidatorSpec<any>>()
expectTypeOf(validator({ default: undefined })).toEqualTypeOf<OptionalValidatorSpec<any>>()
Expand Down

0 comments on commit 8ecd933

Please sign in to comment.