-
-
Notifications
You must be signed in to change notification settings - Fork 369
feat: valibot validates IP addresses #1555
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
hazrid93
wants to merge
1
commit into
open-circle:main
Choose a base branch
from
hazrid93:hazrid93/28_342
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+99
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,97 @@ | ||
| import { IPV4_REGEX, IPV6_REGEX } from '../../regex.ts'; | ||
| import type { | ||
| BaseIssue, | ||
| BaseValidation, | ||
| ErrorMessage, | ||
| } from '../../types/index.ts'; | ||
| import { _addIssue } from '../../utils/index.ts'; | ||
|
|
||
| /** | ||
| * Validates a [CIDR](https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing) | ||
| * string: an IPv4 or IPv6 address followed by a `/` and a prefix length in | ||
| * range (0-32 for IPv4, 0-128 for IPv6). See issue #342. | ||
| */ | ||
| function isCidr(value: string): boolean { | ||
| const slash = value.lastIndexOf('/'); | ||
| if (slash < 0) return false; | ||
| const ip = value.slice(0, slash); | ||
| const prefix = value.slice(slash + 1); | ||
| if (!/^\d{1,3}$/.test(prefix)) return false; | ||
| const n = Number(prefix); | ||
| if (IPV4_REGEX.test(ip)) { | ||
| return n >= 0 && n <= 32; | ||
| } | ||
| if (IPV6_REGEX.test(ip)) { | ||
| return n >= 0 && n <= 128; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * CIDR issue interface. | ||
| */ | ||
| export interface CidrIssue<TInput extends string> | ||
| extends BaseIssue<TInput> { | ||
| readonly kind: 'validation'; | ||
| readonly type: 'cidr'; | ||
| readonly expected: null; | ||
| readonly received: `"${string}"`; | ||
| readonly requirement: (input: string) => boolean; | ||
| } | ||
|
|
||
| /** | ||
| * CIDR action interface. | ||
| */ | ||
| export interface CidrAction< | ||
| TInput extends string, | ||
| TMessage extends ErrorMessage<CidrIssue<TInput>> | undefined, | ||
| > extends BaseValidation<TInput, TInput, CidrIssue<TInput>> { | ||
| readonly type: 'cidr'; | ||
| readonly reference: typeof cidr; | ||
| readonly expects: null; | ||
| readonly requirement: (input: string) => boolean; | ||
| readonly message: TMessage; | ||
| } | ||
|
|
||
| /** | ||
| * Creates a [CIDR](https://en.wikipedia.org/wiki/Classless_Inter-Domain_Routing) | ||
| * validation action that accepts both IPv4 (`192.168.1.0/24`) and IPv6 | ||
| * (`2001:db8::/32`) CIDR notation. The prefix length must be in range (0-32 for | ||
| * IPv4, 0-128 for IPv6). See issue #342. | ||
| * | ||
| * @returns A CIDR action. | ||
| */ | ||
| export function cidr<TInput extends string>(): CidrAction<TInput, undefined>; | ||
|
|
||
| /** | ||
| * Creates a CIDR validation action. | ||
| * | ||
| * @param message The error message. | ||
| * | ||
| * @returns A CIDR action. | ||
| */ | ||
| export function cidr< | ||
| TInput extends string, | ||
| const TMessage extends ErrorMessage<CidrIssue<TInput>> | undefined, | ||
| >(message: TMessage): CidrAction<TInput, TMessage>; | ||
|
|
||
| // @__NO_SIDE_EFFECTS__ | ||
| export function cidr( | ||
| message?: ErrorMessage<CidrIssue<string>>, | ||
| ): CidrAction<string, ErrorMessage<CidrIssue<string>> | undefined> { | ||
| return { | ||
| kind: 'validation', | ||
| type: 'cidr', | ||
| reference: cidr, | ||
| async: false, | ||
| expects: null, | ||
| requirement: isCidr, | ||
| message, | ||
| '~run'(dataset, config) { | ||
| if (dataset.typed && !this.requirement(dataset.value)) { | ||
| _addIssue(this, 'CIDR', dataset, config); | ||
| } | ||
| return dataset; | ||
| }, | ||
| }; | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export * from './cidr.ts'; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
P3: Library formatting CI will reject this new file because it lacks a final newline. End the file with LF so Prettier's check passes.
Prompt for AI agents