@metreeca/blue - v0.12.0
    Preparing search index...

    Module string

    String shape types and operations.

    Defines the shape describing the textual values a resource may carry and its constraint types, and provides the factories stating them, mapping the JSON string type to XSD 1.0 string and temporal datatypes. The string factory bounds a shape by length, lexical and enumeration constraints; the format factories fix the datatype and lexical pattern of a particular format.

    Important

    Contradictory constraints are rejected as the shape is built, so a shape that exists admits at least one value: stating minLength above maxLength throws a TraceError.

    XSD Datatype ¹ Factory Description Format
    string string Unicode character sequence
    string text Single-line plain text
    string markdown Markdown formatted text
    string email RFC 5321 email address
    string phone ITU-T E.164 telephone number
    string iri RFC 3987 IRI reference
    string url RFC 3986 hierarchical URL
    string tag BCP 47 language tag
    gYear year ² ISO 8601 year YYYY[Z/±hh:mm]
    date date ISO 8601 date YYYY-MM-DD[Z/±hh:mm]
    time time ISO 8601 time hh:mm:ss[.sss][Z/±hh:mm]
    dateTime instant ISO 8601 date+time YYYY-MM-DDThh:mm:ss[.sss][TZ]
    dateTime timestamp ³ ISO 8601 UTC timestamp YYYY-MM-DDThh:mm:ss.sssZ
    duration duration ISO 8601 duration [-]PnYnMnDTnHnMnS

    ¹ XSD 1.0 datatypes are referenced by RDF 1.1 and JSON-LD 1.1 as normative

    ² XSD 1.1 Part 2 § D.3.4 permits optional timezone indicators for gYear as a deviation from ISO 8601

    ³ Requires exactly 3 fractional second digits (millisecond precision) and UTC timezone (Z only); typed as xsd:dateTime rather than the more specific xsd:dateTimeStamp for compatibility with SPARQL temporal functions, which are defined over xsd:dateTime

    Note

    The temporal shorthands check the lexical form only: every field is bounded to its legal range and a timezone offset to the ±14:00 range XSD admits, but fields are not cross-checked. As a limitation of that check, a day is bounded to 31 whatever month it falls in, so a date the calendar does not carry, like 2026-02-31, is admitted: whoever needs a real date checks the day against the month.

    Compatibility

    JSON XSD JavaScript
    UTF-8 encoded Unicode (XML 1.0 Char) UTF-16 encoded (compatible)

    Defining string shapes

    import { string } from '@metreeca/blue/string';

    const value = string(); // unconstrained string
    const name = string({ minLength: 1, maxLength: 100 }); // length-constrained
    const code = string({ pattern: /^[A-Z]{3}-\d{4}$/ }); // pattern-constrained
    const status = string({ in: ["active", "inactive"] }); // enumeration-constrained
    const state = string("open", "closed"); // the same, stated as bare values
    Note

    An enumeration also narrows the value the shape describes: status admits "active" | "inactive", not the whole textual domain.

    Specialised string factories

    Predefined factories for common string formats:

    import {
    text, markdown, email, phone, iri, url, tag, date, time, instant, timestamp, duration
    } from '@metreeca/blue/string';

    const label = text(); // single-line plain text
    const body = markdown(); // Markdown formatted text
    const contact = email(); // RFC 5321 email address
    const mobile = phone(); // ITU-T E.164 telephone number
    const identifier = iri(); // RFC 3987 IRI reference
    const link = url(); // RFC 3986 hierarchical URL
    const language = tag(); // BCP 47 language tag
    const birthday = date(); // ISO 8601 date (YYYY-MM-DD)
    const start = time(); // ISO 8601 time (hh:mm:ss)
    const created = instant(); // ISO 8601 datetime
    const modified = timestamp(); // ISO 8601 timestamp (millisecond precision, UTC)
    const validity = duration(); // ISO 8601 duration

    Using in resource shapes

    import { multiple, optional, required, resource } from '@metreeca/blue/resource';
    import { date, email, string } from '@metreeca/blue/string';

    const Person = resource({
    name: required(string({ minLength: 1 })),
    email: optional(email()),
    birthDate: optional(date())
    });

    Type Aliases

    StringShape

    Describes a textual value.

    StringConstraints

    Constraints accepted by the string shape factory.

    StringLengthConstraints

    Length bounds accepted by the string shape factories.

    StringValueConstraints

    Value constraints accepted by the string shape factories.

    Functions

    string

    Creates a string shape.

    text

    Creates a shape for single-line plain text values.

    markdown

    Creates a shape for Markdown text values.

    email

    Creates a shape for email address values.

    phone

    Creates a shape for telephone number values.

    iri

    Creates a shape for Internationalized Resource Identifier values.

    url

    Creates a shape for hierarchical URL values.

    tag

    Creates a shape for language tag values.

    year

    Creates a shape for ISO 8601 year values (YYYY).

    date

    Creates a shape for ISO 8601 calendar date values (YYYY-MM-DD).

    time

    Creates a shape for ISO 8601 time of day values (hh:mm:ss).

    instant

    Creates a shape for ISO 8601 date and time values (YYYY-MM-DDThh:mm:ss).

    timestamp

    Creates a shape for UTC timestamp values with millisecond precision (YYYY-MM-DDThh:mm:ss.sssZ).

    duration

    Creates a shape for ISO 8601 duration values ([-]PnYnMnDTnHnMnS).