|
1 | 1 | import { isAddress } from 'ethers'; |
2 | 2 | import { IExec } from 'iexec'; |
| 3 | +import { NULL_ADDRESS } from 'iexec/utils'; |
3 | 4 | import { ValidationError, boolean, number, object, string } from 'yup'; |
4 | 5 |
|
5 | 6 | export const isValidProvider = async (iexec: IExec) => { |
@@ -54,5 +55,58 @@ export const positiveNumberSchema = () => |
54 | 55 | export const booleanSchema = () => |
55 | 56 | boolean().strict().typeError('${path} should be a boolean'); |
56 | 57 |
|
| 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 | + |
57 | 79 | 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