$darkmode
fable::schema::String Class Reference

#include <string.hpp>

Inheritance diagram for fable::schema::String:
Collaboration diagram for fable::schema::String:

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
 
bool validate (const Conf &c, std::optional< SchemaError > &err) 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 serialize_into (Json &j, const Type &x) const
 
void deserialize_into (const Conf &c, Type &x) const
 
void reset_ptr () override
 
virtual Json to_json () const
 
virtual void to_json (Json &) const=0
 
- Public Member Functions inherited from fable::schema::Base< String >
std::unique_ptr< 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 (std::string s) override
 
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 void validate_or_throw (const Conf &c) const final
 
virtual std::optional< SchemaErrorfail (const Conf &c) const final
 
virtual bool is_valid (const Conf &c) const final
 
virtual Json to_json () const
 

Additional Inherited Members

- Protected Member Functions inherited from fable::schema::Base< String >
 Base (const Base< String > &)=default
 
 Base (Base< String > &&) noexcept=default
 
 Base (JsonType t, std::string desc)
 
 Base (JsonType t)
 
 Base (std::string desc)
 
Base< String > & operator= (const Base< String > &)=default
 
Base< String > & operator= (Base< String > &&) noexcept=default
 
bool validate_type (const Conf &c, std::optional< SchemaError > &err) const
 
SchemaError error (const Conf &c, std::string_view format, Args &&... args) const
 
SchemaError error (const ConfError &e) const
 
SchemaError wrong_type (const Conf &c) const
 
bool set_error (std::optional< SchemaError > &err, const Conf &c, std::string_view format, Args &&... args) const
 
bool set_error (std::optional< SchemaError > &err, const ConfError &e) const
 
bool set_error (std::optional< SchemaError > &err, SchemaError &&e) const
 
bool set_wrong_type (std::optional< SchemaError > &err, const Conf &c) const
 
void augment_schema (Json &j) const
 
- Protected Member Functions inherited from fable::schema::Interface
 Interface (const Interface &)=default
 
 Interface (Interface &&) noexcept=default
 
Interfaceoperator= (const Interface &)=default
 
Interfaceoperator= (Interface &&) noexcept=default
 
- 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.

◆ 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

◆ 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.

◆ 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

◆ set_pattern()

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

Set the string regular expression pattern.

Parameters
valueregex pattern

◆ to_json() [1/3]

virtual Json fable::schema::Interface::to_json
inline

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.

◆ to_json() [2/3]

virtual void fable::schema::Interface::to_json

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.

◆ to_json() [3/3]

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()

bool fable::schema::String::validate ( const Conf c,
std::optional< SchemaError > &  error 
) const
overridevirtual

Validate the input JSON configuration for correctness.

  • This method should only set error if there is an error. This method should not reset error if there is no error. Therefore, the content of error is only valid if the method returns false. This allows you to chain validates and check at the end if there was an error.
  • This method should not throw if there is a schema error!
Parameters
cJSON to check
errorreference to store error if occurred
Returns
true if valid

Implements fable::schema::Interface.

Here is the call graph for this function:

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