|
1 | 1 | import { isAddress } from 'ethers'; |
2 | 2 | import { IExec } from 'iexec'; |
3 | | -import { ValidationError, boolean, number, string } from 'yup'; |
| 3 | +import { NULL_ADDRESS } from 'iexec/utils'; |
| 4 | +import { ValidationError, boolean, number, object, string } from 'yup'; |
4 | 5 |
|
5 | 6 | export const isValidProvider = async (iexec: IExec) => { |
6 | 7 | const client = await iexec.config.resolveContractsClient(); |
@@ -61,3 +62,59 @@ export const positiveNumberSchema = () => |
61 | 62 |
|
62 | 63 | export const booleanSchema = () => |
63 | 64 | boolean().strict().typeError('${path} should be a boolean'); |
| 65 | + |
| 66 | +const isPositiveIntegerStringTest = (value: string) => /^\d+$/.test(value); |
| 67 | + |
| 68 | +const stringSchema = () => |
| 69 | + string().strict().typeError('${path} should be a string'); |
| 70 | + |
| 71 | +const positiveIntegerStringSchema = () => |
| 72 | + string().test( |
| 73 | + 'is-positive-int', |
| 74 | + '${path} should be a positive integer', |
| 75 | + (value) => isUndefined(value) || isPositiveIntegerStringTest(value) |
| 76 | + ); |
| 77 | + |
| 78 | +const positiveStrictIntegerStringSchema = () => |
| 79 | + string().test( |
| 80 | + 'is-positive-strict-int', |
| 81 | + '${path} should be a strictly positive integer', |
| 82 | + (value) => |
| 83 | + isUndefined(value) || |
| 84 | + (value !== '0' && isPositiveIntegerStringTest(value)) |
| 85 | + ); |
| 86 | + |
| 87 | +export const campaignRequestSchema = () => |
| 88 | + object({ |
| 89 | + app: addressSchema().required(), |
| 90 | + appmaxprice: positiveIntegerStringSchema().required(), |
| 91 | + workerpool: addressSchema().required(), |
| 92 | + workerpoolmaxprice: positiveIntegerStringSchema().required(), |
| 93 | + dataset: addressSchema().oneOf([NULL_ADDRESS]).required(), |
| 94 | + datasetmaxprice: positiveIntegerStringSchema().oneOf(['0']).required(), |
| 95 | + params: stringSchema() |
| 96 | + .test( |
| 97 | + 'is-valid-bulk-params', |
| 98 | + '${path} should be a valid JSON string with bulk_cid field', |
| 99 | + (value) => { |
| 100 | + try { |
| 101 | + // eslint-disable-next-line @typescript-eslint/naming-convention |
| 102 | + const { bulk_cid } = JSON.parse(value); |
| 103 | + if (typeof bulk_cid === 'string') { |
| 104 | + return true; |
| 105 | + } |
| 106 | + } catch {} |
| 107 | + return false; |
| 108 | + } |
| 109 | + ) |
| 110 | + .required(), |
| 111 | + requester: addressSchema().required(), |
| 112 | + beneficiary: addressSchema().required(), |
| 113 | + callback: addressSchema().required(), |
| 114 | + category: positiveIntegerStringSchema().required(), |
| 115 | + volume: positiveStrictIntegerStringSchema().required(), |
| 116 | + tag: stringSchema().required(), |
| 117 | + trust: positiveIntegerStringSchema().required(), |
| 118 | + salt: stringSchema().required(), |
| 119 | + sign: stringSchema().required(), |
| 120 | + }).typeError('${path} should be a BulkRequest object'); |
0 commit comments