fixo
Fixo is an extension to JSON parser, it allows you to extend and compose JSON objects.
You could use Fixo for configuration files, test fixtures and other cases when you need to create complex objects with dynamic content.
With Fixo
, you could:
- define a
master
object and extend it into differentprofile
variations - set the
default
property values to be shared with the rest of the object properties - compose an object from different sources with different
profiles
The core of fixo
is the resolvers
and macros
. Resolvers
allow you to transform an object property value as a whole, while macros
allow you to replace parts of a string value with dynamic content.
Installation
$ npm install fixo
How to use it
var Fixo = ; // Initialize with optionsvar fixo = ; // Load objectfixo; // Or with Promisefixo; // Load object property for a profilefixo; // Load multiple objectsfixo; // Load JSON file in a folderfixo;
Options
- srcDir: Data source directory to lookup the JSON files (default:
.
) - defaultProfile: Object profile key (default:
master
)
Data file
Test fixtures example:
/test/fixtures profile.json cards.json banks.json
Inheritance and override
Fixo supports object inheritance and override in the following sequence:
GB
extends frommaster
GB-en
extends frommaster
,GB
,en
GB-en-dev
extends frommaster
,GB
,en
,GB-en
Profile keys can be anything, it could be a combination of environment and device names if you are using fixo
to load a configuration file:
Default values
Shared default values. Visa, Diners, Amex extend the default values:
"master": "default": "expiry_date": "08/18" "cvv": "123" "visa": "account_number": "1111111111111111" "cvv": "123" "diners": "account_number": "22222222222222" "amex": "account_number": "333333333333333"
Value composition
Resolvers
Resolver allows you replace a property value as a whole. Fixo comes with two default resolvers, but you could easily create your own resolver:
- get: To retrieve a property value of current object
- get.[profile]: To retrieve a property value of current object for the given the profile
- include: To include an object property from another file
- include.[profile]: To include an object property value from another file matching the given profile
"master": "account": "name": "Walter Mitty" "locale": "en-US" "wallet": "locale": "get:account.locale" "bank": "include:banks.citibank" "card": "include.CA:cards.visa" "GB": "account": "name": "William James"
Note:
profile
is propagated to the downstream resolvers:
include.CA:cards.visa
will loadcards.visa
forCA
profile, any resolvers found incard.visa
will look forCA
profile.
How to Create Custom Resolver
Fixo provides load
and loadSync
methods, thus custom resolver function should provide both sync
and async
implementations.
Resolver function will be called multiple times for each nested object if there is any matching custom property. Example:
// Fixture name: profile.json "first_name": "uppercase:Michael" "last_name": "uppercase:Troy" // Custom resovler that converts all matching property value to uppercase// @param props {Object} - Matching property values:// {// [fieldName]: {// value: '[property value]',// profile: '[profile]'// },// ...// } { var result = {}; Object; // async implementation if callback ; // sync implementation else return result; } // Register resolverfixo; // Load profile - asyncfixo; // Load profile - syncvar profile = fixo;console; // Ouput: "first_name": "MICHAEL" "last_name": "TROY"
Please refer to the default get
and include
resolvers for reference implementation.
Note: Dash (-) character is not allowed for the resolver name.
Macros
Use macros if you need to replace parts of a string value with dynamic content:
Fixo shipped with one built-in macro for now, i.e. random
. The idea is, it should be easy for you create various macros to suit your test cases.
To add/remove a macro:
fixo; fixo;
random
macro generates random 12 alphanumeric characters by default. You could pass arguments to the macro by appending -
to the macro name. random
macro takes in two arguments, i.e. type (alpha|numeric|alphanum)
and length
.
Note: Dash (-) character is not allowed for the macro name.
License
Copyright (C) 2016 PayPal
Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.