Variable

Variable

new Variable(definitionopt)

A variable inside a collection is similar to variables in any programming construct. The variable has an identifier name (provided by its id) and a value. A variable is optionally accompanied by a variable type. One or more variables can be associated with a collection and can be referred from anywhere else in the collection using the double-brace {{variable-id}} format. Properties can then use the .toObjectResolved function to procure an object representation of the property with all variable references replaced by corresponding values.

Parameters:
Name Type Attributes Description
definition Variable.definition <optional>

Specify the initial value and type of the variable.

Source:

Extends

Members

(static, readonly) types :function

The possible supported types of a variable is defined here. The keys defined here are the possible values of Variable#type.

Additional variable types can be supported by adding the type-casting function to this enumeration.

Type:
  • function
Properties:
Name Type Description
string function

When a variable's type is set to "string", it ensures that Variable#get converts the value of the variable to a string before returning the data.

boolean function

A boolean type of variable can either be set to true or false. Any other value set is converted to Boolean when procured from Variable#get.

number function

A "number" type variable ensures that the value is always represented as a number. A non-number type value is returned as NaN.

array function

A "array" type value stores Array data format

object function

A "object" type value stores Object data format

any function

Free-form type of a value. This is the default for any variable, unless specified otherwise. It ensures that the variable can store data in any type and no conversion is done while using Variable#get.

Source:

disabled :Boolean

This (optional) flag denotes whether this property is disabled or not. Usually, this is helpful when a property is part of a PropertyList. For example, in a PropertyList of Headers, the ones that are disabled can be filtered out and not processed.

Type:
  • Boolean
Overrides:
Source:

id :String

The id of the property is a unique string that identifies this property and can be used to refer to this property from relevant other places. It is a good practice to define the id or let the system auto generate a UUID if one is not defined for properties that require an id.

Type:
  • String
Inherited From:
Source:

key :String

The name of the variable. This is used for referencing this variable from other locations and scripts

Type:
  • String
Source:

name :String

A property can have a distinctive and human-readable name. This is to be used to display the name of the property within Postman, Newman or other runtimes that consume collection. In certain cases, the absence of name might cause the runtime to use the id as a fallback.

Type:
  • String
Inherited From:
Source:

type :Variable.types

Type:
Source:

value :*

Type:
  • *
Source:

Methods

(static) isVariable(obj) → {Boolean}

Parameters:
Name Type Description
obj *
Source:
Returns:
Type
Boolean

cast(value) → {*}

Typecasts a value to the Variable.types of this Variable. Returns the value of the variable converted to the type specified in Variable#type.

Parameters:
Name Type Description
value *
Source:
Returns:
Type
*

describe(content, typeopt)

This function allows to describe the property for the purpose of detailed identification or documentation generation. This function sets or updates the description child-property of this property.

Parameters:
Name Type Attributes Default Description
content String

The content of the description can be provided here as a string. Note that it is expected that if the content is formatted in any other way than simple text, it should be specified in the subsequent type parameter.

type String <optional>
"text/plain"

The type of the content.

Inherited From:
Source:
Example

Add a description to an instance of Collection

 var Collection = require('postman-collection').Collection,
    mycollection;

// create a blank collection
myCollection = new Collection();
myCollection.describe('Hey! This is a cool collection.');

console.log(myCollection.description.toString()); // read the description

findInParents(property, customizeropt) → {*|undefined}

Tries to find the given property locally, and then proceeds to lookup in each parent, going up the chain as necessary. Lookup will continue until customizer returns a truthy value. If used without a customizer, the lookup will stop at the first parent that contains the property.

Parameters:
Name Type Attributes Description
property String
customizer function <optional>
Inherited From:
Source:
Returns:
Type
* | undefined

forEachParent(options, iterator)

Invokes the given iterator for every parent in the parent chain of the given element.

Parameters:
Name Type Description
options Object

A set of options for the parent chain traversal.

Properties
Name Type Attributes Default Description
withRoot Boolean <optional>
<nullable>
false

Set to true to include the collection object as well.

iterator function

The function to call for every parent in the ancestry chain.

Inherited From:
Source:
To Do:
  • Cache the results

get() → {Variable.types}

Gets the value of the variable.

Source:
Returns:
Type
Variable.types

meta() → {*}

Returns the meta keys associated with the property

Inherited From:
Source:
Returns:
Type
*

parent() → {*|undefined}

Returns the parent of item

Inherited From:
Source:
Returns:
Type
* | undefined

set(value)

Sets the value of the variable.

Parameters:
Name Type Description
value *
Source:

toJSON()

Returns the JSON representation of a property, which conforms to the way it is defined in a collection. You can use this method to get the instantaneous representation of any property, including a Collection.

Inherited From:
Source:

toString() → {String}

Returns the stringified value of the variable.

Source:
Returns:
Type
String

update(options)

Updates the type and value of a variable from an object or JSON definition of the variable.

Parameters:
Name Type Description
options Variable.definition
Source:

valueOf(valueopt) → {*}

An alias of this.get and this.set.

Parameters:
Name Type Attributes Description
value * <optional>
Source:
Returns:
Type
*

valueType(typeName, _noCast) → {String}

Sets or gets the type of the value.

Parameters:
Name Type Description
typeName String
_noCast Boolean
Source:
Returns:
  • returns the current type of the variable from the list of Variable.types
Type
String

Type Definitions

definition

The object representation of a Variable consists the variable value and type. It also optionally includes the id and a friendly name of the variable. The id and the name of a variable is usually managed and used when a variable is made part of a VariableList instance.

Type:
  • Object
Properties:
Name Type Attributes Description
value * <optional>

The value of the variable that will be stored and will be typecast to the type set in the variable or passed along in this parameter.

type String <optional>

The type of this variable from the list of types defined at Variable.types.

Source:
Example
{
    "id": "my-var-1",
    "name": "MyFirstVariable",
    "value": "Hello World",
    "type": "string"
}