Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Min and Max to pactum-matchers for int and float #24

Open
wants to merge 16 commits into
base: main
Choose a base branch
from
38 changes: 38 additions & 0 deletions src/compare.js
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,25 @@ function compareWithInt(actual, rule, path) {
if (type !== 'number') {
throw `Json doesn't have type 'number' at '${path}' but found '${type}'`;
} else {
if ('options' in rule && rule.options !== undefined) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Move this new code to a common function and reuse for both int and float comparisons

const options = rule.options;
if ('min' in options) {
const min = options.min;
if (typeof min !== 'number') {
throw `Value for min of '${path}' is not a number`;
} else if (actual < min) {
throw `Value ${actual} at '${path}' is less than minimum ${min}`
}
}
if ('max' in options) {
const max = options.max;
if (typeof max !== 'number') {
throw `Value for max of '${path}' is not a number`
} else if (actual > max) {
throw `Value ${actual} at '${path}' is more than maximum ${max}`
}
}
}
const pattern = patterns.INT;
if (!pattern.test(actual)) {
throw `Json doesn't have 'integer' number at '${path}' but found '${actual}'`;
Expand All @@ -224,6 +243,25 @@ function compareWithFloat(actual, rule, path) {
if (type !== 'number') {
throw `Json doesn't have type 'number' at '${path}' but found '${type}'`;
} else {
if ('options' in rule && rule.options !== undefined) {
const options = rule.options;
if ('min' in options) {
const min = options.min;
if (typeof min !== 'number') {
throw `Value for min of '${path}' is not a number`;
} else if (actual < min) {
throw `Value ${actual} at '${path}' is less than minimum ${min}`
}
}
if ('max' in options) {
const max = options.max;
if (typeof max !== 'number') {
throw `Value for max of '${path}' is not a number`
} else if (actual > max) {
throw `Value ${actual} at '${path}' is more than maximum ${max}`
}
}
}
const pattern = patterns.FLOAT;
if (!pattern.test(actual)) {
throw `Json doesn't have 'float' number at '${path}' but found '${actual}'`;
Expand Down
4 changes: 2 additions & 2 deletions src/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,12 @@ export function any(value?: any): object;
/**
* Int matching
*/
export function int(value?: number): object;
export function int(value?: number, options?: { min?: number, max?: number }): object;

/**
* Float matching
*/
export function float(value?: number): object;
export function float(value?: number, options?: { min?: number, max?: number }): object;

/**
* Greater than given number matching
Expand Down
10 changes: 6 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,19 @@ function any(value) {
};
}

function int(value) {
function int(value, options) {
return {
value: value || 123,
pactum_type: 'INT'
pactum_type: 'INT',
options
};
}

function float(value) {
function float(value, options) {
return {
value: value || 123.456,
pactum_type: 'FLOAT'
pactum_type: 'FLOAT',
options,
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function setMatchingRules(rules, data, path) {
rules[path] = { match: 'any' };
break;
default:
rules[path] = { match: data.pactum_type.toLowerCase() };
rules[path] = { match: data.pactum_type.toLowerCase(), options: data.options };
break;
}
}
Expand Down