Skip to content

Custom parsers are typed to receive strings, but spec requires default values to match the type #102

@novemberborn

Description

@novemberborn
const hex = envalid.makeValidator<Buffer>(value => Buffer.from(value, 'hex'))

envalid.cleanEnv({
  VALUE: 'decafbad',
}, {
  VALUE: hex({ default: 'ff' }), // TypeScript expects a Buffer or undefined
})

The parser is typed as receiving a string value:

parser: (input: string) => any,

However the validator specs infer the validated type and mandate it for the default and devDefault values. That's nice for numbers and booleans but I suspect it'll trip up the json validator. It works for my hex validator above but only because Node.js doesn't throw an error for the unnecessary encoding argument.

Perhaps the default values should always be strings, and be typed as such?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions