Skip to content

feat: accept minOccurs and maxOccurs in parsers.array #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ to _your_ definition of valid. See [how to use it](#usage).
- [`parsers.port(value: string): number`](#parsersportvalue-string-number)
- [`parsers.whitelist(whitelistedValues: string[]): Parser<string>`](#parserswhitelistwhitelistedvalues-string-parserstring)
- [`parsers.regex(pattern: Regex): Parser<string>`](#parsersregexpattern-regex-parserstring)
- [`parsers.array<T>({ parser: Parser<T>, separator?: string }): Parser<T>`](#parsersarrayt-parser-parsert-separator-string--parsert)
- [`parsers.array<T>({ parser: Parser<T>, separator?: string, minOccurs?: number, maxOccurs?: number }): Parser<T>`](#parsers-array)
- [`parsers.positiveInteger(value: string): number`](#parserspositiveintegervalue-string-number)
- [`parsers.nonPositiveInteger(value: string): number`](#parsersnonpositiveintegervalue-string-number)
- [`parsers.negativeInteger(value: string): number`](#parsersnegativeintegervalue-string-number)
Expand Down Expand Up @@ -232,7 +232,7 @@ const env = makeEnv({
});
```

#### `parsers.array<T>({ parser: Parser<T>, separator?: string }): Parser<T>`
#### <a name="parsers-array"></a> `parsers.array<T>({ parser: Parser<T>, separator?: string, minOccurs?: number, maxOccurs?: number }): Parser<T>`

Takes a parser and returns a parser that parses a list of values.
The default value separator is `,`.
Expand Down
47 changes: 47 additions & 0 deletions src/parsers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,53 @@ describe('parsers.array', () => {

expect(() => parser(serializedValue)).toThrow();
});

test('parses an array of values with minOccurs and maxOccurs', () => {
const values = ['test1', 'test2', 'test3'];

const serializedValue = values.join(',');
const expectedValue = values;

const parser = parsers.array({
parser: parsers.string,
minOccurs: 1,
maxOccurs: 4,
});

expect(parser(serializedValue)).toEqual(expectedValue);
});

test('throws when minOccurs is violated', () => {
const values = ['test1', 'test2', 'test3'];

const serializedValue = values.join(',');

const parser = parsers.array({
parser: parsers.string,
minOccurs: 4,
maxOccurs: 4,
});

expect(() => parser(serializedValue)).toThrow(
'array has fewer than 4 items',
);
});

test('throws when maxOccurs is violated', () => {
const values = ['test1', 'test2', 'test3'];

const serializedValue = values.join(',');

const parser = parsers.array({
parser: parsers.string,
minOccurs: 1,
maxOccurs: 2,
});

expect(() => parser(serializedValue)).toThrow(
'array has more than 2 items',
);
});
});

describe('parsers.positiveInteger', () => {
Expand Down
22 changes: 20 additions & 2 deletions src/parsers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,8 @@ export function regex(pattern: RegExp): Parser<string> {
export type ArrayParserArgs<TType> = Readonly<{
parser: Parser<TType>;
separator?: string;
minOccurs?: number;
maxOccurs?: number;
}>;

const defaultArraySeparator = ',';
Expand All @@ -178,15 +180,31 @@ const defaultArraySeparator = ',';
export function array<TType>(
args: ArrayParserArgs<TType>,
): Parser<readonly TType[]> {
const separator = args.separator || defaultArraySeparator;
const {
parser,
separator = defaultArraySeparator,
minOccurs,
maxOccurs,
} = args;

const arrayParser: Parser<readonly TType[]> = (serializedArray) => {
const serializedValues = serializedArray.split(separator);

const values = serializedValues.map((serializedValue) =>
args.parser(serializedValue),
parser(serializedValue),
);

if (minOccurs !== undefined && values.length < minOccurs) {
throw new EnvironmentVariableError(
`array has fewer than ${minOccurs} items`,
);
}
if (maxOccurs !== undefined && values.length > maxOccurs) {
throw new EnvironmentVariableError(
`array has more than ${maxOccurs} items`,
);
}

return values;
};

Expand Down