Skip to content

Commit 93a32d5

Browse files
feat: add campaignRequest validation
1 parent 4e3063a commit 93a32d5

File tree

2 files changed

+557
-484
lines changed

2 files changed

+557
-484
lines changed

src/utils/validators.ts

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { isAddress } from 'ethers';
22
import { IExec } from 'iexec';
3+
import { NULL_ADDRESS } from 'iexec/utils';
34
import { ValidationError, boolean, number, object, string } from 'yup';
45

56
export const isValidProvider = async (iexec: IExec) => {
@@ -54,5 +55,58 @@ export const positiveNumberSchema = () =>
5455
export const booleanSchema = () =>
5556
boolean().strict().typeError('${path} should be a boolean');
5657

58+
const isPositiveIntegerStringTest = (value: string) => /^\d+$/.test(value);
59+
60+
const stringSchema = () =>
61+
string().strict().typeError('${path} should be a string');
62+
63+
const positiveIntegerStringSchema = () =>
64+
string().test(
65+
'is-positive-int',
66+
'${path} should be a positive integer',
67+
(value) => isUndefined(value) || isPositiveIntegerStringTest(value)
68+
);
69+
70+
const positiveStrictIntegerStringSchema = () =>
71+
string().test(
72+
'is-positive-strict-int',
73+
'${path} should be a strictly positive integer',
74+
(value) =>
75+
isUndefined(value) ||
76+
(value !== '0' && isPositiveIntegerStringTest(value))
77+
);
78+
5779
export const campaignRequestSchema = () =>
58-
object().required().typeError('${path} should be a BulkRequest object');
80+
object({
81+
app: addressSchema().required(),
82+
appmaxprice: positiveIntegerStringSchema().required(),
83+
workerpool: addressSchema().required(),
84+
workerpoolmaxprice: positiveIntegerStringSchema().required(),
85+
dataset: addressSchema().oneOf([NULL_ADDRESS]).required(),
86+
datasetmaxprice: positiveIntegerStringSchema().oneOf(['0']).required(),
87+
params: stringSchema()
88+
.test(
89+
'is-valid-bulk-params',
90+
'${path} should be a valid JSON string with bulk_cid field',
91+
(value) => {
92+
try {
93+
// eslint-disable-next-line @typescript-eslint/naming-convention
94+
const { bulk_cid } = JSON.parse(value);
95+
if (typeof bulk_cid === 'string') {
96+
return true;
97+
}
98+
} catch {}
99+
return false;
100+
}
101+
)
102+
.required(),
103+
requester: addressSchema().required(),
104+
beneficiary: addressSchema().required(),
105+
callback: addressSchema().required(),
106+
category: positiveIntegerStringSchema().required(),
107+
volume: positiveStrictIntegerStringSchema().required(),
108+
tag: stringSchema().required(),
109+
trust: positiveIntegerStringSchema().required(),
110+
salt: stringSchema().required(),
111+
sign: stringSchema().required(),
112+
}).typeError('${path} should be a BulkRequest object');

0 commit comments

Comments
 (0)