Network-related validation including IP addresses, MAC addresses, and port numbers. These scalars provide validation for networking data commonly used in system administration and network-aware applications.
Validates IP addresses (both IPv4 and IPv6).
/**
* GraphQL scalar for IP address validation
* Accepts both IPv4 and IPv6 format addresses
*/
const GraphQLIP: GraphQLScalarType;Example Values:
"192.168.1.1" (IPv4)"10.0.0.1" (IPv4)"2001:0db8:85a3:0000:0000:8a2e:0370:7334" (IPv6)"::1" (IPv6 loopback)"fe80::1" (IPv6 link-local)Validates IPv4 addresses specifically.
/**
* GraphQL scalar for IPv4 address validation
* Accepts IPv4 format: xxx.xxx.xxx.xxx (0-255 for each octet)
*/
const GraphQLIPv4: GraphQLScalarType;Example Values:
"192.168.1.1""10.0.0.1""127.0.0.1""255.255.255.255""0.0.0.0"Validates IPv6 addresses specifically.
/**
* GraphQL scalar for IPv6 address validation
* Accepts IPv6 format including compressed notation
*/
const GraphQLIPv6: GraphQLScalarType;Example Values:
"2001:0db8:85a3:0000:0000:8a2e:0370:7334""2001:db8:85a3::8a2e:370:7334" (compressed)"::1" (loopback)"fe80::1" (link-local)"::ffff:192.0.2.1" (IPv4-mapped)Validates MAC (Media Access Control) address format.
/**
* GraphQL scalar for MAC address validation
* Accepts MAC address in various common formats
*/
const GraphQLMAC: GraphQLScalarType;Example Values:
"00:14:22:01:23:45""00-14-22-01-23-45""0014.2201.2345""001422012345"Validates network port numbers (1-65535).
/**
* GraphQL scalar for port number validation
* Accepts integers from 1 to 65535 (valid port range)
*/
const GraphQLPort: GraphQLScalarType;Example Values:
80 (HTTP)443 (HTTPS)22 (SSH)3000 (development server)65535 (maximum port)import {
GraphQLIP,
GraphQLIPv4,
GraphQLIPv6,
GraphQLMAC,
GraphQLPort
} from "graphql-scalars";
// In a GraphQL schema
const ServerType = new GraphQLObjectType({
name: "Server",
fields: {
ipAddress: { type: GraphQLIP },
ipv4Address: { type: GraphQLIPv4 },
ipv6Address: { type: GraphQLIPv6 },
macAddress: { type: GraphQLMAC },
port: { type: GraphQLPort },
},
});
// Using with type definitions
const typeDefs = `
scalar IP
scalar IPv4
scalar IPv6
scalar MAC
scalar Port
type Server {
hostname: String!
ipAddress: IP
ipv4Address: IPv4
ipv6Address: IPv6
macAddress: MAC
port: Port
}
type NetworkInterface {
name: String!
ipv4: IPv4
ipv6: IPv6
mac: MAC
}
`;GraphQL Operations:
query GetServerInfo($serverId: ID!) {
server(id: $serverId) {
hostname
ipAddress
port
networkInterfaces {
name
ipv4
ipv6
mac
}
}
}
mutation UpdateServer($id: ID!, $input: ServerInput!) {
updateServer(id: $id, input: $input) {
id
ipAddress
port
}
}Variables:
{
"serverId": "srv-123",
"input": {
"ipAddress": "192.168.1.100",
"port": 8080,
"macAddress": "00:14:22:01:23:45"
}
}Each network scalar performs specific format validation:
Validation Examples:
// Valid network data
const validServer = {
ipAddress: "192.168.1.1", // Valid IPv4
ipv6Address: "::1", // Valid IPv6 (compressed)
macAddress: "00:14:22:01:23:45", // Valid MAC format
port: 3000 // Valid port number
};
// Invalid network data
const invalidServer = {
ipAddress: "999.999.999.999", // Invalid IPv4 (octets > 255)
ipv6Address: "invalid::address", // Invalid IPv6 format
macAddress: "xx:xx:xx:xx:xx:xx", // Invalid MAC (non-hex)
port: 70000 // Invalid port (> 65535)
};IP Addresses are used for:
MAC Addresses are used for:
Port Numbers are used for:
// String constants for schema building
const IP: string; // "scalar IP"
const IPv4: string; // "scalar IPv4"
const IPv6: string; // "scalar IPv6"
const MAC: string; // "scalar MAC"
const Port: string; // "scalar Port"// Mock data generators for testing
const IPMock: () => string;
const IPv4Mock: () => string;
const IPv6Mock: () => string;
const MACMock: () => string;
const PortMock: () => number;Network scalars provide specific validation error messages:
// IP validation errors
"Value is not a valid IP address: 999.999.999.999"
// MAC validation errors
"Value is not a valid MAC address: xx:xx:xx:xx:xx:xx"
// Port validation errors
"Value is not a valid port number: 70000 (must be 1-65535)"