Generates a random string based on a given charset with length within a specified range.
Configuration
This Value Generator requires configuration describing the attributes of the desired string looking
as follows:
This configuration requires a specification of the length as a range withing which the
output string should be of, and a set of characters the result string should consist of:
The following configuration example could be used to generate strings of length
somewhere in between 0 and 15 characters while using only characters of the set
['a', 'b', 'c', 'd', 'e', 'f']
This Value Generator is a template String processor. It lets you generate any string you desire by
the specified template that is being filled up during the generation.
// Declaration of variables with values being generatedexportinterfaceStringTemplateVariables{[variable:string]:(// This is a description of schema fields in declarative mannerDeclarativeFieldDefinition|// Or, you can pass the Value Generator directly as an instanceValueGenerator<GeneratedValue,ValueGeneratorConfig>);}// Declaration of custom charsets assigned to tags - you can use// these tags in your template string and these will be then replaced// with a random character from the specified set of charactersexportinterfaceCustomCharsets{[tag:string]:Charset}// The root configuration declarationexporttypeStringTemplateGeneratorConfig={template:string;variables?:StringTemplateVariables;customCharsets?:CustomCharsets;}&ValueGeneratorConfig;
The following configuration example could be used to generate strings of length
somewhere in between 0 and 15 characters while using only characters of the set
['a', 'b', 'c', 'd', 'e', 'f']
Expected output of such complex configuration could look something like
this:
'ID-foo_value-foo_value-bar_value-dvUT-73-zx'
Variables
Handling variables is one of the basic template features supported. The variable substitution
is split into two steps:
Value Generation - To generate a value being used in the final string, you need to provide
the desired Value Generator. You can use for example a Random String generator, Random Number generator
or Context Accessor; the only requirement is to be actually a Value generator. Then, when this Template
String Generator is requested for template generation, it will always generate new values for this
provisioning. Keep in mind that this value is being reused during the whole single generation.
Replacement - To replace the generated value in the template, you use a specific syntax to tell the
Template generator where to put the value to - using {variableName}. The whole substring with brackets
is then replaced with the actual value prepared by the generator specified by the variableName.
This generator is very useful for generating random strings of a specific
form. But it can also be used also (as noted above) to generate strings
with placeholders - variables.
constconfig={template:'<< string template using {variable_name} as input >>',variables:{variable_name:{// Value Generator declaration}}}
Custom Charsets
Custom charsets is another feature enabling the client to use a custom set of characters
at a specific place in the template.
By defining the tag and assigning the desired Charset, the Value Generator seeks these tags
in the template and substitutes it with a randomly selected character.
You can specify those in an optional customCharsets property.
Predefined Charsets
The most basic feature of the String Template Generator is the ability to use common
character sets.
%d describes any digit in range [0, 9]
%D describes any digit in range [1, 9] (can be useful when want a set of digits to not
start with a zero)
%c describes any lowercase alphabetic character (like [a-z] regular expression)
%C describes any uppercase alphabetic character (like [A-Z] regular expression)
// Configuration of the String builderconstconfig:StringTemplateGeneratorConfig={template:'UID_{userIdFromContext}-%userClass-item-%D%d%d%d%d%d',variables:{userIdFromContext:{type:'context-input',config:{path:'userId'}}},customCharsets:{'%userClass':['a','x','r','w']}};// Generator using the given configurationconstgenerator=newStringTemplateGenerator(config);// Context passing the userIdconstcontext:FabricationContext={userId:123456789}console.log(generator.generate(context));
// Configuration of the String builderconstconfig:StringTemplateGeneratorConfig={template:'UID_{userIdFromContext}-%userClass-item-%D%d%d%d%d%d',variables:{userIdFromContext:{type:'context-input',config:{path:'userId'}}},customCharsets:{'%userClass':['a','x','r','w']}};// Generator using the given configurationconstgenerator=getValueGenerator('string-template',config);// Context passing the userIdconstcontext:FabricationContext={userId:123456789}console.log(generator.generate(context));
constschema:SchemaInput={fields:{userItemId:{type:'string-template',config:{template:'UID_{userIdFromContext}-%userClass-item-%D%d%d%d%d%d',variables:{userIdFromContext:{type:'context-input',config:{path:'userId'}}},customCharsets:{'%userClass':['a','x','r','w']}}}}}// Fabricator generating Falsa of such shapeconstfabricator=newFabricator(schema);// Context passing the userIdconstcontext:FabricationContext={userId:123456789}console.log(fabricator.generate(context));