validationSchema.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. import * as Yup from "yup";
  2. import { checkAddress } from "@polkadot/util-crypto";
  3. // TODO: If we really need this (currency unit) we can we make "Validation" a functiction that returns an object.
  4. // We could then "instantialize" it in "withFormContainer" where instead of passing
  5. // "validationSchema" (in each form component file) we would just pass "validationSchemaKey" or just "proposalType" (ie. SetLead).
  6. // Then we could let the "withFormContainer" handle the actual "validationSchema" for "withFormik". In that case it could easily
  7. // pass stuff like totalIssuance or currencyUnit here (ie.: const validationSchema = Validation(currencyUnit, totalIssuance)[proposalType];)
  8. const CURRENCY_UNIT = undefined;
  9. // All
  10. const TITLE_MAX_LENGTH = 40;
  11. const RATIONALE_MAX_LENGTH = 3000;
  12. // Text
  13. const DESCRIPTION_MAX_LENGTH = 5000;
  14. // Runtime Upgrade
  15. const FILE_SIZE_BYTES_MIN = 1;
  16. const FILE_SIZE_BYTES_MAX = 2000000;
  17. // Set Election Parameters
  18. const ANNOUNCING_PERIOD_MAX = 43200;
  19. const ANNOUNCING_PERIOD_MIN = 14400;
  20. const VOTING_PERIOD_MIN = 14400;
  21. const VOTING_PERIOD_MAX = 28800;
  22. const REVEALING_PERIOD_MIN = 14400;
  23. const REVEALING_PERIOD_MAX = 28800;
  24. const MIN_COUNCIL_STAKE_MIN = 1;
  25. const MIN_COUNCIL_STAKE_MAX = 100000;
  26. const NEW_TERM_DURATION_MIN = 14400;
  27. const NEW_TERM_DURATION_MAX = 432000;
  28. const CANDIDACY_LIMIT_MIN = 25;
  29. const CANDIDACY_LIMIT_MAX = 100;
  30. const COUNCIL_SIZE_MAX = 20;
  31. const COUNCIL_SIZE_MIN = 4;
  32. const MIN_VOTING_STAKE_MIN = 1;
  33. const MIN_VOTING_STAKE_MAX = 100000;
  34. // Spending
  35. const TOKENS_MIN = 0;
  36. const TOKENS_MAX = 2000000;
  37. // Set Validator Count
  38. const MAX_VALIDATOR_COUNT_MIN = 4;
  39. const MAX_VALIDATOR_COUNT_MAX = 100;
  40. // Content Working Group Mint Capacity
  41. const MINT_CAPACITY_MIN = 0;
  42. const MINT_CAPACITY_MAX = 1000000;
  43. // Set Storage Role Parameters
  44. const MIN_STAKE_MIN = 1;
  45. const MIN_STAKE_MAX = 10000000;
  46. const MIN_ACTORS_MIN = 0;
  47. const MIN_ACTORS_MAX = 1;
  48. const MAX_ACTORS_MIN = 2;
  49. const MAX_ACTORS_MAX = 99;
  50. const REWARD_MIN = 1;
  51. const REWARD_MAX = 999;
  52. const REWARD_PERIOD_MIN = 600;
  53. const REWARD_PERIOD_MAX = 3600;
  54. const BONDING_PERIOD_MIN = 600;
  55. const BONDING_PERIOD_MAX = 28800;
  56. const UNBONDING_PERIOD_MIN = 600;
  57. const UNBONDING_PERIOD_MAX = 28800;
  58. const MIN_SERVICE_PERIOD_MIN = 600;
  59. const MIN_SERVICE_PERIOD_MAX = 28800;
  60. const STARTUP_GRACE_PERIOD_MIN = 600;
  61. const STARTUP_GRACE_PERIOD_MAX = 28800;
  62. const ENTRY_REQUEST_FEE_MIN = 1;
  63. const ENTRY_REQUEST_FEE_MAX = 100000;
  64. function errorMessage(name: string, min?: number | string, max?: number | string, unit?: string): string {
  65. return `${name} should be at least ${min} and no more than ${max}${unit ? ` ${unit}.` : "."}`;
  66. }
  67. /*
  68. Validation is used to validate a proposal form.
  69. Each proposal type should validate the fields of his form, anything is valid as long as it fits in a Yup Schema.
  70. In a form, validation should be injected in the Yup Schema just by accessing it in this object.
  71. Ex:
  72. // EvictStorageProvider Form
  73. import Validation from 'path/to/validationSchema'
  74. ...
  75. validationSchema: Yup.object().shape({
  76. ...genericFormDefaultOptions.validationSchema,
  77. storageProvider: Validation.EvictStorageProvider.storageProvider
  78. }),
  79. */
  80. type ValidationType = {
  81. All: {
  82. title: Yup.StringSchema<string>;
  83. rationale: Yup.StringSchema<string>;
  84. };
  85. Text: {
  86. description: Yup.StringSchema<string>;
  87. };
  88. RuntimeUpgrade: {
  89. WASM: Yup.StringSchema<string>;
  90. };
  91. SetElectionParameters: {
  92. announcingPeriod: Yup.NumberSchema<number>;
  93. votingPeriod: Yup.NumberSchema<number>;
  94. minVotingStake: Yup.NumberSchema<number>;
  95. revealingPeriod: Yup.NumberSchema<number>;
  96. minCouncilStake: Yup.NumberSchema<number>;
  97. newTermDuration: Yup.NumberSchema<number>;
  98. candidacyLimit: Yup.NumberSchema<number>;
  99. councilSize: Yup.NumberSchema<number>;
  100. };
  101. Spending: {
  102. tokens: Yup.NumberSchema<number>;
  103. destinationAccount: Yup.StringSchema<string>;
  104. };
  105. SetLead: {
  106. workingGroupLead: Yup.StringSchema<string>;
  107. };
  108. SetContentWorkingGroupMintCapacity: {
  109. mintCapacity: Yup.NumberSchema<number>;
  110. };
  111. EvictStorageProvider: {
  112. storageProvider: Yup.StringSchema<string | null>;
  113. };
  114. SetValidatorCount: {
  115. maxValidatorCount: Yup.NumberSchema<number>;
  116. };
  117. SetStorageRoleParameters: {
  118. min_stake: Yup.NumberSchema<number>;
  119. min_actors: Yup.NumberSchema<number>;
  120. max_actors: Yup.NumberSchema<number>;
  121. reward: Yup.NumberSchema<number>;
  122. reward_period: Yup.NumberSchema<number>;
  123. bonding_period: Yup.NumberSchema<number>;
  124. unbonding_period: Yup.NumberSchema<number>;
  125. min_service_period: Yup.NumberSchema<number>;
  126. startup_grace_period: Yup.NumberSchema<number>;
  127. entry_request_fee: Yup.NumberSchema<number>;
  128. };
  129. };
  130. const Validation: ValidationType = {
  131. All: {
  132. title: Yup.string()
  133. .required("Title is required!")
  134. .max(TITLE_MAX_LENGTH, `Title should be under ${TITLE_MAX_LENGTH} characters.`),
  135. rationale: Yup.string()
  136. .required("Rationale is required!")
  137. .max(RATIONALE_MAX_LENGTH, `Rationale should be under ${RATIONALE_MAX_LENGTH} characters.`)
  138. },
  139. Text: {
  140. description: Yup.string()
  141. .required("Description is required!")
  142. .max(DESCRIPTION_MAX_LENGTH, `Description should be under ${DESCRIPTION_MAX_LENGTH}`)
  143. },
  144. RuntimeUpgrade: {
  145. WASM: Yup.string()
  146. .required("A file is required")
  147. .min(FILE_SIZE_BYTES_MIN, "File is empty.")
  148. .max(FILE_SIZE_BYTES_MAX, `The maximum file size is ${FILE_SIZE_BYTES_MAX} bytes.`)
  149. },
  150. SetElectionParameters: {
  151. announcingPeriod: Yup.number()
  152. .required("All fields must be filled!")
  153. .integer("This field must be an integer.")
  154. .min(
  155. ANNOUNCING_PERIOD_MIN,
  156. errorMessage("The announcing period", ANNOUNCING_PERIOD_MIN, ANNOUNCING_PERIOD_MAX, "blocks")
  157. )
  158. .max(
  159. ANNOUNCING_PERIOD_MAX,
  160. errorMessage("The announcing period", ANNOUNCING_PERIOD_MIN, ANNOUNCING_PERIOD_MAX, "blocks")
  161. ),
  162. votingPeriod: Yup.number()
  163. .required("All fields must be filled!")
  164. .integer("This field must be an integer.")
  165. .min(VOTING_PERIOD_MIN, errorMessage("The voting period", VOTING_PERIOD_MIN, VOTING_PERIOD_MAX, "blocks"))
  166. .max(VOTING_PERIOD_MAX, errorMessage("The voting period", VOTING_PERIOD_MIN, VOTING_PERIOD_MAX, "blocks")),
  167. minVotingStake: Yup.number()
  168. .required("All fields must be filled!")
  169. .integer("This field must be an integer.")
  170. .min(
  171. MIN_VOTING_STAKE_MIN,
  172. errorMessage("The minimum voting stake", MIN_VOTING_STAKE_MIN, MIN_VOTING_STAKE_MAX, CURRENCY_UNIT)
  173. )
  174. .max(
  175. MIN_VOTING_STAKE_MAX,
  176. errorMessage("The minimum voting stake", MIN_VOTING_STAKE_MIN, MIN_VOTING_STAKE_MAX, CURRENCY_UNIT)
  177. ),
  178. revealingPeriod: Yup.number()
  179. .required("All fields must be filled!")
  180. .integer("This field must be an integer.")
  181. .min(
  182. REVEALING_PERIOD_MIN,
  183. errorMessage("The revealing period", REVEALING_PERIOD_MIN, REVEALING_PERIOD_MAX, "blocks")
  184. )
  185. .max(
  186. REVEALING_PERIOD_MAX,
  187. errorMessage("The revealing period", REVEALING_PERIOD_MIN, REVEALING_PERIOD_MAX, "blocks")
  188. ),
  189. minCouncilStake: Yup.number()
  190. .required("All fields must be filled!")
  191. .integer("This field must be an integer.")
  192. .min(
  193. MIN_COUNCIL_STAKE_MIN,
  194. errorMessage("The minimum council stake", MIN_COUNCIL_STAKE_MIN, MIN_COUNCIL_STAKE_MAX, CURRENCY_UNIT)
  195. )
  196. .max(
  197. MIN_COUNCIL_STAKE_MAX,
  198. errorMessage("The minimum council stake", MIN_COUNCIL_STAKE_MIN, MIN_COUNCIL_STAKE_MAX, CURRENCY_UNIT)
  199. ),
  200. newTermDuration: Yup.number()
  201. .required("All fields must be filled!")
  202. .integer("This field must be an integer.")
  203. .min(
  204. NEW_TERM_DURATION_MIN,
  205. errorMessage("The new term duration", NEW_TERM_DURATION_MIN, NEW_TERM_DURATION_MAX, "blocks")
  206. )
  207. .max(
  208. NEW_TERM_DURATION_MAX,
  209. errorMessage("The new term duration", NEW_TERM_DURATION_MIN, NEW_TERM_DURATION_MAX, "blocks")
  210. ),
  211. candidacyLimit: Yup.number()
  212. .required("All fields must be filled!")
  213. .integer("This field must be an integer.")
  214. .min(CANDIDACY_LIMIT_MIN, errorMessage("The candidacy limit", CANDIDACY_LIMIT_MIN, CANDIDACY_LIMIT_MAX))
  215. .max(CANDIDACY_LIMIT_MAX, errorMessage("The candidacy limit", CANDIDACY_LIMIT_MIN, CANDIDACY_LIMIT_MAX)),
  216. councilSize: Yup.number()
  217. .required("All fields must be filled!")
  218. .integer("This field must be an integer.")
  219. .min(COUNCIL_SIZE_MIN, errorMessage("The council size", COUNCIL_SIZE_MIN, COUNCIL_SIZE_MAX))
  220. .max(COUNCIL_SIZE_MAX, errorMessage("The council size", COUNCIL_SIZE_MIN, COUNCIL_SIZE_MAX))
  221. },
  222. Spending: {
  223. tokens: Yup.number()
  224. .positive("The token amount should be positive.")
  225. .integer("This field must be an integer.")
  226. .max(TOKENS_MAX, errorMessage("The amount of tokens", TOKENS_MIN, TOKENS_MAX))
  227. .required("You need to specify an amount of tokens."),
  228. destinationAccount: Yup.string()
  229. .required("Select a destination account!")
  230. .test("address-test", "${account} is not a valid address.", account => !!checkAddress(account, 5))
  231. },
  232. SetLead: {
  233. workingGroupLead: Yup.string().required("Select a proposed lead!")
  234. },
  235. SetContentWorkingGroupMintCapacity: {
  236. mintCapacity: Yup.number()
  237. .positive("Mint capacity should be positive.")
  238. .integer("This field must be an integer.")
  239. .min(MINT_CAPACITY_MIN, errorMessage("Mint capacity", MINT_CAPACITY_MIN, MINT_CAPACITY_MAX, CURRENCY_UNIT))
  240. .max(MINT_CAPACITY_MAX, errorMessage("Mint capacity", MINT_CAPACITY_MIN, MINT_CAPACITY_MAX, CURRENCY_UNIT))
  241. .required("You need to specify a mint capacity.")
  242. },
  243. EvictStorageProvider: {
  244. storageProvider: Yup.string()
  245. .nullable()
  246. .required("Select a storage provider!")
  247. },
  248. SetValidatorCount: {
  249. maxValidatorCount: Yup.number()
  250. .required("Enter the max validator count")
  251. .integer("This field must be an integer.")
  252. .min(
  253. MAX_VALIDATOR_COUNT_MIN,
  254. errorMessage("The max validator count", MAX_VALIDATOR_COUNT_MIN, MAX_VALIDATOR_COUNT_MAX)
  255. )
  256. .max(
  257. MAX_VALIDATOR_COUNT_MAX,
  258. errorMessage("The max validator count", MAX_VALIDATOR_COUNT_MIN, MAX_VALIDATOR_COUNT_MAX)
  259. )
  260. },
  261. SetStorageRoleParameters: {
  262. min_stake: Yup.number()
  263. .required("All parameters are required")
  264. .positive("The minimum stake should be positive.")
  265. .integer("This field must be an integer.")
  266. .max(MIN_STAKE_MAX, errorMessage("Minimum stake", MIN_STAKE_MIN, MIN_STAKE_MAX, CURRENCY_UNIT)),
  267. min_actors: Yup.number()
  268. .required("All parameters are required")
  269. .integer("This field must be an integer.")
  270. .min(MIN_ACTORS_MIN, errorMessage("Minimum actors", MIN_ACTORS_MIN, MIN_ACTORS_MAX))
  271. .max(MIN_ACTORS_MAX, errorMessage("Minimum actors", MIN_ACTORS_MIN, MIN_ACTORS_MAX)),
  272. max_actors: Yup.number()
  273. .required("All parameters are required")
  274. .integer("This field must be an integer.")
  275. .min(MAX_ACTORS_MIN, errorMessage("Max actors", MAX_ACTORS_MIN, MAX_ACTORS_MAX))
  276. .max(MAX_ACTORS_MAX, errorMessage("Max actors", MAX_ACTORS_MIN, MAX_ACTORS_MAX)),
  277. reward: Yup.number()
  278. .required("All parameters are required")
  279. .integer("This field must be an integer.")
  280. .min(REWARD_MIN, errorMessage("Reward", REWARD_MIN, REWARD_MAX, CURRENCY_UNIT))
  281. .max(REWARD_MAX, errorMessage("Reward", REWARD_MIN, REWARD_MAX, CURRENCY_UNIT)),
  282. reward_period: Yup.number()
  283. .required("All parameters are required")
  284. .integer("This field must be an integer.")
  285. .min(REWARD_PERIOD_MIN, errorMessage("The reward period", REWARD_PERIOD_MIN, REWARD_PERIOD_MAX, "blocks"))
  286. .max(REWARD_PERIOD_MAX, errorMessage("The reward period", REWARD_PERIOD_MIN, REWARD_PERIOD_MAX, "blocks")),
  287. bonding_period: Yup.number()
  288. .required("All parameters are required")
  289. .integer("This field must be an integer.")
  290. .min(BONDING_PERIOD_MIN, errorMessage("The bonding period", BONDING_PERIOD_MIN, BONDING_PERIOD_MAX, "blocks"))
  291. .max(BONDING_PERIOD_MAX, errorMessage("The bonding period", BONDING_PERIOD_MIN, BONDING_PERIOD_MAX, "blocks")),
  292. unbonding_period: Yup.number()
  293. .required("All parameters are required")
  294. .integer("This field must be an integer.")
  295. .min(
  296. UNBONDING_PERIOD_MIN,
  297. errorMessage("The unbonding period", UNBONDING_PERIOD_MIN, UNBONDING_PERIOD_MAX, "blocks")
  298. )
  299. .max(
  300. UNBONDING_PERIOD_MAX,
  301. errorMessage("The unbonding period", UNBONDING_PERIOD_MIN, UNBONDING_PERIOD_MAX, "blocks")
  302. ),
  303. min_service_period: Yup.number()
  304. .required("All parameters are required")
  305. .integer("This field must be an integer.")
  306. .min(
  307. MIN_SERVICE_PERIOD_MIN,
  308. errorMessage("The minimum service period", MIN_SERVICE_PERIOD_MIN, MIN_SERVICE_PERIOD_MAX, "blocks")
  309. )
  310. .max(
  311. MIN_SERVICE_PERIOD_MAX,
  312. errorMessage("The minimum service period", MIN_SERVICE_PERIOD_MIN, MIN_SERVICE_PERIOD_MAX, "blocks")
  313. ),
  314. startup_grace_period: Yup.number()
  315. .required("All parameters are required")
  316. .integer("This field must be an integer.")
  317. .min(
  318. STARTUP_GRACE_PERIOD_MIN,
  319. errorMessage("The startup grace period", STARTUP_GRACE_PERIOD_MIN, STARTUP_GRACE_PERIOD_MAX, "blocks")
  320. )
  321. .max(
  322. STARTUP_GRACE_PERIOD_MAX,
  323. errorMessage("The startup grace period", STARTUP_GRACE_PERIOD_MIN, STARTUP_GRACE_PERIOD_MAX, "blocks")
  324. ),
  325. entry_request_fee: Yup.number()
  326. .required("All parameters are required")
  327. .integer("This field must be an integer.")
  328. .min(
  329. ENTRY_REQUEST_FEE_MIN,
  330. errorMessage("The entry request fee", ENTRY_REQUEST_FEE_MIN, ENTRY_REQUEST_FEE_MAX, CURRENCY_UNIT)
  331. )
  332. .max(
  333. STARTUP_GRACE_PERIOD_MAX,
  334. errorMessage("The entry request fee", ENTRY_REQUEST_FEE_MIN, ENTRY_REQUEST_FEE_MAX, CURRENCY_UNIT)
  335. ),
  336. }
  337. };
  338. export default Validation;