@validatem/is-port-number 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. package/README.md +25 -0
  2. package/index.js +13 -0
  3. package/package.json +34 -0
  4. package/test.js +32 -0
package/README.md ADDED
@@ -0,0 +1,25 @@
1
+ # @validatem/is-port-number
2
+
3
+ Validates that a given value is a valid port number. This validator is meant to be used with [Validatem](https://validatem.cryto.net/), the universal validation library.
4
+
5
+ This validator treats all integers between 0 and 65535 (inclusive) as valid port numbers, as these are typically the port numbers accepted by networking APIs. Note that this includes 0 (typically used to indicate 'random port choice') as well as ephemeral port numbers that are typically used for outgoing connections, since these are system-specific behaviours.
6
+
7
+ ## Usage
8
+
9
+ More extensive usage documentation can be found on the [Validatem website](https://validatem.cryto.net).
10
+
11
+ ```js
12
+ validateValue(value, [ isPortNumber ]);
13
+ ```
14
+
15
+ This validator does not take any options.
16
+
17
+ ## Possible error codes
18
+
19
+ - __validatem.is-number.number:__ The given value is not a Number type.
20
+ - __validatem.is-integer:__ The given value is not an integer.
21
+ - __validatem.is-port-number:__ The given value is not within the range of valid port numbers.
22
+
23
+ ## License
24
+
25
+ WTFPL or CC0, at your choice.
package/index.js ADDED
@@ -0,0 +1,13 @@
1
+ "use strict";
2
+
3
+ const isInteger = require("@validatem/is-integer");
4
+ const ValidationError = require("@validatem/error");
5
+
6
+ module.exports = [
7
+ isInteger,
8
+ function (value) {
9
+ if (value < 0 || value > 65535) {
10
+ throw new ValidationError(`Must be a valid port number between 0 and 65535`, { code: "validatem.is-port-number" });
11
+ }
12
+ }
13
+ ];
package/package.json ADDED
@@ -0,0 +1,34 @@
1
+ {
2
+ "name": "@validatem/is-port-number",
3
+ "version": "1.0.0",
4
+ "description": "Validator for determining whether a value is a valid port number",
5
+ "main": "index.js",
6
+ "repository": {
7
+ "url": "https://codeberg.org/validatem/is-port-number.git"
8
+ },
9
+ "keywords": [
10
+ "validatem",
11
+ "validation",
12
+ "port",
13
+ "network"
14
+ ],
15
+ "author": "Sven Slootweg <admin@cryto.net>",
16
+ "license": "WTFPL OR CC0-1.0",
17
+ "devEngines": {
18
+ "packageManager": {
19
+ "name": "pnpm",
20
+ "version": "^11.21.0",
21
+ "onFail": "download"
22
+ }
23
+ },
24
+ "dependencies": {
25
+ "@validatem/error": "^1.1.0",
26
+ "@validatem/is-integer": "^0.1.1"
27
+ },
28
+ "devDependencies": {
29
+ "@validatem/core": "^0.6.3"
30
+ },
31
+ "scripts": {
32
+ "test": "echo \"Error: no test specified\" && exit 1"
33
+ }
34
+ }
package/test.js ADDED
@@ -0,0 +1,32 @@
1
+ "use strict";
2
+
3
+ const test = require("node:test");
4
+ const assert = require("node:assert");
5
+ const { validateValue } = require("@validatem/core");
6
+
7
+ const isPortNumber = require(".");
8
+
9
+ test("valid value", () => {
10
+ validateValue(5287, [ isPortNumber ]);
11
+ });
12
+
13
+ test("negative value", () => {
14
+ assert.throws(
15
+ () => validateValue(-5287, [ isPortNumber ]),
16
+ (error) => error.errors[0].code === "validatem.is-port-number"
17
+ );
18
+ });
19
+
20
+ test("string value", () => {
21
+ assert.throws(
22
+ () => validateValue("5287", [ isPortNumber ]),
23
+ (error) => error.errors[0].code === "validatem.is-number.number"
24
+ );
25
+ });
26
+
27
+ test("float", () => {
28
+ assert.throws(
29
+ () => validateValue(5287.42, [ isPortNumber ]),
30
+ (error) => error.errors[0].code === "validatem.is-integer"
31
+ );
32
+ });