Skip to content
Snippets Groups Projects
Commit 91400966 authored by Javier Cejudo's avatar Javier Cejudo Committed by Alvaro Cabrera Durán
Browse files

Feat: add option to override random generation (#307)

parent 14afebff
Branches
Tags
No related merge requests found
......@@ -415,6 +415,7 @@ You may define following options for `jsf` that alter its behavior:
- `defaultInvalidTypeProduct`: - default value generated for a schema with invalid type (works only if `failOnInvalidTypes` is set to `false`)
- `maxItems`: number - Configure a maximum amount of items to generate in an array. This will override the maximum items found inside a JSON Schema.
- `maxLength`: number - Configure a maximum length to allow generating strings for. This will override the maximum length found inside a JSON Schema.
- `random`: Function - a replacement for `Math.random` to support pseudorandom number generation.
Set options just as below:
......
......@@ -53,6 +53,7 @@
"jasmine-node": "2.0.0-beta4",
"jayschema": "^0.3.1",
"lodash.template": "^4.4.0",
"seedrandom": "^2.4.3",
"semver": "^5.3.0",
"tslint": "^4.0.2",
"tv4": "^1.2.7",
......
const seedrandom = require('seedrandom');
module.exports = {
register: function(jsf) {
return jsf.option({
random: seedrandom('some seed')
});
}
};
[
{
"description": "random option",
"tests": [
{
"description": "should allow pseudorandom value generation",
"schema": {
"type": "object",
"properties": {
"a": {
"type": "integer",
"minimum": 0,
"maximum": 100
},
"b": {
"type": "string"
},
"c": {
"pattern": "^[a-z]{2}[0-9]{3}$"
}
},
"required": ["a", "b", "c"]
},
"valid": true,
"equal": {
"a": 39,
"b": "dolore et qui ex",
"c": "hv837"
},
"repeat": 1,
"require": "core/option/random"
}
]
}
]
......@@ -4,6 +4,10 @@ import option = require('../api/option');
// set maximum default, see #193
RandExp.prototype.max = 10;
// same implementation as the original except using our random
RandExp.prototype.randInt = (a, b) =>
a + Math.floor(option('random')() * (1 + b - a));
type Dependency = any;
/**
......
import Registry = require('./Registry');
type Option = boolean|number;
type Option = boolean|number|Function;
/**
* This class defines a registry for custom formats used within JSF.
......@@ -18,6 +18,7 @@ class OptionRegistry extends Registry<Option> {
this.data['defaultMinItems'] = 0;
this.data['defaultRandExpMax'] = 10;
this.data['alwaysFakeOptionals'] = false;
this.data['random'] = Math.random;
}
}
......
/// <reference path="../index.d.ts" />
var option = require('../api/option');
/**
* Returns random element of a collection
*
......@@ -7,7 +9,7 @@
* @returns {T}
*/
function pick<T>(collection: T[]): T {
return collection[Math.floor(Math.random() * collection.length)];
return collection[Math.floor(option('random')() * collection.length)];
}
/**
......@@ -23,7 +25,7 @@ function shuffle<T>(collection: T[]): T[] {
length: number = collection.length;
for (; length > 0;) {
key = Math.floor(Math.random() * length);
key = Math.floor(option('random')() * length);
// swap
tmp = copy[--length];
copy[length] = copy[key];
......@@ -48,7 +50,7 @@ var MIN_NUMBER = -100,
* @see http://stackoverflow.com/a/1527820/769384
*/
function getRandomInt(min: number, max: number): number {
return Math.floor(Math.random() * (max - min + 1)) + min;
return Math.floor(option('random')() * (max - min + 1)) + min;
}
/**
......
var option = require('../api/option');
/**
* Generates randomized boolean value.
*
* @returns {boolean}
*/
function booleanGenerator(): boolean {
return Math.random() > 0.5;
return option('random')() > 0.5;
}
export = booleanGenerator;
......@@ -55,7 +55,7 @@ var stringType: FTypeGenerator = function stringType(value: IStringSchema): stri
}
while (output.length < minLength) {
output += Math.random() > 0.7 ? thunk() : randexp('.+');
output += option('random')() > 0.7 ? thunk() : randexp('.+');
}
if (output.length > maxLength) {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment