$darkmode
fable::schema::String Class Reference

#include <string.hpp>

Inheritance diagram for fable::schema::String:
[legend]
Collaboration diagram for fable::schema::String:
[legend]

Public Types

using Type = std::string
 

Public Member Functions

 String (Type *ptr, std::string &&desc)
 
String not_empty () &&
 
size_t min_length () const
 
String min_length (size_t value) &&
 
void set_min_length (size_t value)
 
size_t max_length () const
 
String max_length (size_t value) &&
 
void set_max_length (size_t value)
 
const std::string & pattern () const
 
String pattern (const std::string &value) &&
 
void set_pattern (const std::string &value)
 
String c_identifier () &&
 
bool interpolate () const
 
String interpolate (bool value) &&
 
void set_interpolate (bool value)
 
Environmentenvironment () const
 
String environment (Environment *env) &&
 
void set_environment (Environment *env)
 
const std::vector< std::string > & enum_of () const
 
String enum_of (std::vector< std::string > &&init)
 
void set_enum_of (std::vector< std::string > &&init)
 
Json json_schema () const override
 
void validate (const Conf &c) const override
 
void to_json (Json &j) const override
 
void from_conf (const Conf &c) override
 
Json serialize (const Type &x) const
 
Type deserialize (const Conf &c) const
 
void reset_ptr () override
 
- Public Member Functions inherited from fable::schema::Base< String >
 Base (JsonType t, std::string &&desc)
 
 Base (JsonType t)
 
 Base (std::string &&desc)
 
Interfaceclone () const override
 
 operator Box () const
 
JsonType type () const override
 
std::string type_string () const override
 
Json usage () const override
 
bool is_required () const override
 
String require () &&
 
String required (bool value) &&
 
String reset_pointer () &&
 
bool has_description () const
 
void set_description (const std::string &s) override
 
void set_description (std::string &&s)
 
const std::string & description () const override
 
String description (std::string &&desc) &&
 
- Public Member Functions inherited from fable::schema::Interface
virtual bool is_variant () const
 
virtual bool is_valid (const Conf &c) const
 
virtual Json to_json () const
 

Additional Inherited Members

- Protected Member Functions inherited from fable::schema::Base< String >
void validate_type (const Conf &c) const
 
void throw_error (const Conf &c, const char *format, Args... args) const
 
void throw_error (const ConfError &e) const
 
void throw_wrong_type (const Conf &c) const
 
void augment_schema (Json &j) const
 
- Protected Attributes inherited from fable::schema::Base< String >
JsonType type_
 
bool required_
 
std::string desc_
 

Detailed Description

String de-/serializes a string.

A string can have the following validation properties set:

  • minimum length (in bytes)
  • maximum length (in bytes)
  • regular expression pattern
  • with or without ENV variable interpolation
  • with or withot a special environment for interpolation

The String schema type allows these properties to be specified and will validate them during deserialization.

Member Function Documentation

◆ c_identifier()

String fable::schema::String::c_identifier ( ) &&

Ensure that the input matches an identifier as roughly specified by C.

This is shorthand for setting the pattern to FABLE_REGEX_C_IDENTIFIER.

Here is the call graph for this function:

◆ enum_of() [1/2]

const std::vector< std::string > & fable::schema::String::enum_of ( ) const

Return the set of valid values for the string.

Returns
valid values

◆ enum_of() [2/2]

String fable::schema::String::enum_of ( std::vector< std::string > &&  init)

Set the valid values for the string.

Note
If a pattern is set, the string will need to validate against the pattern in addition to this list.
Parameters
initlist of valid values
Returns
*this, for chaining

◆ environment() [1/2]

Environment * fable::schema::String::environment ( ) const

Return the Environment used for variable interpolation.

Returns
environment

◆ environment() [2/2]

String fable::schema::String::environment ( Environment env) &&

Set the Environment used for variable interpolation.

See also
String::set_interpolate(bool)
Parameters
environment
Returns
*this, for chaining

◆ from_conf()

void fable::schema::String::from_conf ( const Conf )
overridevirtual

Apply the input JSON configuration.

This does not validate the input.

Implements fable::schema::Interface.

◆ interpolate() [1/2]

bool fable::schema::String::interpolate ( ) const

Return whether shell-style variable interpolation is enabled (default false).

Returns
true if enabled

◆ interpolate() [2/2]

String fable::schema::String::interpolate ( bool  value) &&

Set whether variable interpolation is enabled.

The following strings are then interpolated:

This uses the Environment set by the set_environment() method. If no environment is set, a default empty environment is used with OS environment fallback.

The string "${SHELL}" for example will evaluate to the current shell as defined in the OS environment, unless explicitely set in an Environment that is passed to set_environment().

Alternatives can be provided, such as with "${NOT_EXIST-alternate string}".

See also
Environment
String::set_environment(Environment)
Parameters
valuetrue to enable
Returns
*this, for chaining

◆ json_schema()

Json fable::schema::String::json_schema ( ) const
overridevirtual

Return the JSON schema.

Example output:

{ "$schema": "http://json-schema.org/draft-07/schema#", "description": "stand-in no-operation simulator", "properties": { "vehicles": { "description": "list of vehicle names to make available", "items": { "type": "string" }, "type": "array" } }, "title": "nop", "additionalProperties": false, "type": "object" }

See the following links for the specification:

Implements fable::schema::Interface.

◆ max_length() [1/2]

size_t fable::schema::String::max_length ( ) const

Return the current maximum length (max size_t value if unset).

Returns
maximum string length in bytes

◆ max_length() [2/2]

String fable::schema::String::max_length ( size_t  value) &&

Set the maximum string length in bytes.

Parameters
valuemax bytes
Returns
*this, for chaining

◆ min_length() [1/2]

size_t fable::schema::String::min_length ( ) const

Return the current minimum length (0 if unset).

Returns
minimum string length in bytes

◆ min_length() [2/2]

String fable::schema::String::min_length ( size_t  value) &&

Set the minimum string length in bytes.

Parameters
valuemin bytes
Returns
*this, for chaining

◆ not_empty()

String fable::schema::String::not_empty ( ) &&

Ensure input is not empty if present.

This is effectively an alias for min_length(1).

Returns
*this, for chaining
Here is the call graph for this function:

◆ pattern() [1/2]

const std::string & fable::schema::String::pattern ( ) const

Return the regular expression pattern the string should match.

Returns
regex pattern

◆ pattern() [2/2]

String fable::schema::String::pattern ( const std::string &  value) &&

Set the string regular expression pattern.

Parameters
valueregex pattern
Returns
*this, for chaining

◆ reset_ptr()

void fable::schema::String::reset_ptr ( )
overridevirtual

Reset the internal pointer to nullptr, protecting against invalid access.

This should be used when a schema is used after the backing data has been deleted.

Implements fable::schema::Interface.

Here is the call graph for this function:

◆ set_enum_of()

void fable::schema::String::set_enum_of ( std::vector< std::string > &&  init)

Set the valid values for the string.

Note
If a pattern is set, the string will need to validate against the pattern in addition to this list.
Parameters
initlist of valid values

◆ set_environment()

void fable::schema::String::set_environment ( Environment env)

Set the Environment used for variable interpolation.

See also
String::set_interpolate(bool)
Parameters
environment

◆ set_interpolate()

void fable::schema::String::set_interpolate ( bool  value)

Set whether variable interpolation is enabled.

The following strings are then interpolated:

This uses the Environment set by the set_environment() method. If no environment is set, a default empty environment is used with OS environment fallback.

The string "${SHELL}" for example will evaluate to the current shell as defined in the OS environment, unless explicitely set in an Environment that is passed to set_environment().

Alternatives can be provided, such as with "${NOT_EXIST-alternate string}".

See also
Environment
String::set_environment(Environment)
Parameters
valuetrue to enable

◆ set_max_length()

void fable::schema::String::set_max_length ( size_t  value)

Set the maximum string length in bytes.

Parameters
valuemaximum string length in bytes

◆ set_min_length()

void fable::schema::String::set_min_length ( size_t  value)

Set the minimum string length in bytes.

Parameters
valuemin bytes
Here is the caller graph for this function:

◆ set_pattern()

void fable::schema::String::set_pattern ( const std::string &  value)

Set the string regular expression pattern.

Parameters
valueregex pattern
Here is the caller graph for this function:

◆ to_json()

void fable::schema::String::to_json ( Json ) const
overridevirtual

Return the current value of the destination.

Warning: This is NOT an efficient operation, but it can be useful for cases where speed is not important.

Implements fable::schema::Interface.

◆ validate()

void fable::schema::String::validate ( const Conf c) const
overridevirtual

Validate the input JSON configuration.

If you don't have a Conf but would like to validate a Json type, then construct a Conf on the fly:

s.validate(Conf{j});

This function is not provided inline to prevent incorrect use.

Implements fable::schema::Interface.

Here is the call graph for this function:

The documentation for this class was generated from the following files: