Skip to content

Commit

Permalink
feat: add ip versions support
Browse files Browse the repository at this point in the history
  • Loading branch information
ItMaga committed Jul 27, 2023
1 parent 75db05c commit b9052de
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 5 deletions.
9 changes: 6 additions & 3 deletions lib/generators/StringGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export default class StringGenerator<T extends z.ZodString> implements BaseGener
string = '😀';
break;
case 'ip':
string = this.getRandomIP();
string = this.getRandomIP(check.version!);
break;
case 'includes':
case 'startsWith':
Expand Down Expand Up @@ -62,7 +62,10 @@ export default class StringGenerator<T extends z.ZodString> implements BaseGener
});
}

public getRandomIP(): string {
return Array.from({ length: 4 }, () => Math.floor(Math.random() * 256)).join('.');
public getRandomIP(version: 'v4' | 'v6'): string {
if (version === 'v4') {
return [0, 0, 0, 0].map(() => Math.floor(Math.random() * 256)).join('.');
}
return [0, 0, 0, 0, 0, 0, 0, 0].map(() => Math.floor(Math.random() * 65536).toString(16)).join(':');
}
}
6 changes: 4 additions & 2 deletions tests/string.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,11 @@ describe('String', () => {
expect(schema);
});
test('ip', () => {
const schema = z.string().ip();
const schemaV4 = z.string().ip({ version: 'v4' });
const schemaV6 = z.string().ip({ version: 'v6' });

expect(schema);
expect(schemaV4);
expect(schemaV6);
});
test('startsWith', () => {
const schema = z.string().startsWith('__MARK__');
Expand Down

0 comments on commit b9052de

Please sign in to comment.