or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

colors.mddata-formats.mddate-time.mdfinancial.mdgeographic.mdidentifiers.mdindex.mdnetwork.mdnumeric.mdspecialized.mdstring-text.mdutilities.md
tile.json

string-text.mddocs/

String and Text Scalars

String validation for common text formats including emails, URLs, phone numbers, and constrained text. These scalars provide validation for text data commonly used in web applications.

Capabilities

EmailAddress Scalar

Validates email address format according to HTML specification.

/**
 * GraphQL scalar for email address validation
 * Validates according to HTML spec: https://html.spec.whatwg.org/multipage/input.html#valid-e-mail-address
 * Accepts standard email address format
 */
const GraphQLEmailAddress: GraphQLScalarType;

Example Values:

  • "user@example.com"
  • "firstname.lastname@company.co.uk"
  • "test+label@domain.org"
  • "user123@subdomain.example.com"

URL Scalar

Validates URL format for web addresses.

/**
 * GraphQL scalar for URL validation
 * Accepts valid URL format including protocol
 */
const GraphQLURL: GraphQLScalarType;

Example Values:

  • "https://www.example.com"
  • "http://localhost:3000"
  • "https://api.example.com/v1/users"
  • "ftp://files.example.com/documents"

NonEmptyString Scalar

Validates strings that contain at least one character.

/**
 * GraphQL scalar for non-empty string validation
 * Accepts strings with at least one character (not empty or whitespace-only)
 */
const GraphQLNonEmptyString: GraphQLScalarType;

Example Values:

  • "Hello"
  • "A"
  • " text with spaces "
  • "123"

Invalid Values:

  • "" (empty string)
  • " " (whitespace-only)

PhoneNumber Scalar

Validates phone number format.

/**
 * GraphQL scalar for phone number validation
 * Accepts various international phone number formats
 */
const GraphQLPhoneNumber: GraphQLScalarType;

Example Values:

  • "+1-555-123-4567"
  • "+44 20 7946 0958"
  • "(555) 123-4567"
  • "+33 1 42 86 83 26"

Usage Examples

import { 
  GraphQLEmailAddress,
  GraphQLURL,
  GraphQLNonEmptyString,
  GraphQLPhoneNumber
} from "graphql-scalars";

// In a GraphQL schema
const UserType = new GraphQLObjectType({
  name: "User",
  fields: {
    email: { type: GraphQLEmailAddress },
    website: { type: GraphQLURL },
    name: { type: GraphQLNonEmptyString },
    phone: { type: GraphQLPhoneNumber },
  },
});

// Using with type definitions
const typeDefs = `
  scalar EmailAddress
  scalar URL
  scalar NonEmptyString
  scalar PhoneNumber
  
  type User {
    email: EmailAddress
    website: URL
    name: NonEmptyString
    phone: PhoneNumber
  }
`;

Validation Examples:

// Valid user data
const validUser = {
  email: "alice@example.com",
  website: "https://alice.dev",
  name: "Alice Johnson",
  phone: "+1-555-123-4567"
};

// Invalid user data (will fail validation)
const invalidUser = {
  email: "not-an-email",           // Invalid email format
  website: "not-a-url",            // Missing protocol
  name: "",                        // Empty string
  phone: "123"                     // Invalid phone format
};

GraphQL Query Example:

mutation CreateUser($input: UserInput!) {
  createUser(input: $input) {
    id
    email
    website
    name
    phone
  }
}

Variables:

{
  "input": {
    "email": "newuser@example.com",
    "website": "https://newuser.blog",
    "name": "New User",
    "phone": "+1-555-987-6543"
  }
}

Validation Behavior

Each string scalar performs comprehensive validation:

  • EmailAddress: Uses regex pattern matching against HTML specification
  • URL: Validates protocol, domain, and path structure
  • NonEmptyString: Ensures string has at least one non-whitespace character
  • PhoneNumber: Accepts various international formats with country codes

All string scalars provide descriptive error messages when validation fails, making debugging easier during development.

Type Definitions

// String constants for schema building
const EmailAddress: string;    // "scalar EmailAddress"
const URL: string;             // "scalar URL"
const NonEmptyString: string;  // "scalar NonEmptyString"
const PhoneNumber: string;     // "scalar PhoneNumber"

Mock Data

// Mock data generators for testing
const EmailAddressMock: () => string;
const URLMock: () => string;
const NonEmptyStringMock: () => string;
const PhoneNumberMock: () => string;

Error Handling

String scalars provide specific error messages for different validation failures:

// EmailAddress validation errors
"Value is not a valid email address: invalid-email"

// URL validation errors  
"Value is not a valid URL: not-a-url"

// NonEmptyString validation errors
"Value must be a non-empty string: (empty string)"

// PhoneNumber validation errors
"Value is not a valid phone number: 123"