@digigov/form 0.6.10 → 0.7.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (108) hide show
  1. package/CHANGELOG.md +12 -1
  2. package/Field/FieldBase.js +110 -0
  3. package/Field/FieldConditional.js +72 -0
  4. package/Field/FieldContainer.js +62 -0
  5. package/Field/index.js +15 -328
  6. package/Field/utils.js +146 -0
  7. package/FormBuilder.js +12 -12
  8. package/Questions/__snapshots__/index.spec.tsx.snap +461 -2499
  9. package/es/Field/FieldBase.js +93 -0
  10. package/es/Field/FieldConditional.js +54 -0
  11. package/es/Field/FieldContainer.js +40 -0
  12. package/es/Field/index.js +11 -308
  13. package/es/Field/utils.js +122 -0
  14. package/es/FormBuilder.js +11 -11
  15. package/es/Questions/__snapshots__/index.spec.tsx.snap +461 -2499
  16. package/es/internal.js +2 -0
  17. package/es/utils.js +5 -3
  18. package/es/validators/index.js +155 -0
  19. package/es/validators/types.js +1 -0
  20. package/es/validators/utils/afm.js +35 -0
  21. package/es/validators/utils/file.js +52 -0
  22. package/es/validators/utils/iban.js +123 -0
  23. package/es/validators/utils/index.js +105 -0
  24. package/es/validators/utils/phone.js +122 -0
  25. package/es/validators/utils/postal_code.js +29 -0
  26. package/es/validators/utils/uuid4.js +20 -0
  27. package/es/{validators.spec.js → validators/validators.spec.js} +1 -1
  28. package/esm/Field/FieldBase.js +93 -0
  29. package/esm/Field/FieldConditional.js +54 -0
  30. package/esm/Field/FieldContainer.js +40 -0
  31. package/esm/Field/index.js +11 -308
  32. package/esm/Field/utils.js +122 -0
  33. package/esm/FormBuilder.js +11 -11
  34. package/esm/Questions/__snapshots__/index.spec.tsx.snap +461 -2499
  35. package/esm/index.js +1 -1
  36. package/esm/internal.js +2 -0
  37. package/esm/utils.js +5 -3
  38. package/esm/validators/index.js +155 -0
  39. package/esm/validators/types.js +1 -0
  40. package/esm/validators/utils/afm.js +35 -0
  41. package/esm/validators/utils/file.js +52 -0
  42. package/esm/validators/utils/iban.js +123 -0
  43. package/esm/validators/utils/index.js +105 -0
  44. package/esm/validators/utils/phone.js +122 -0
  45. package/esm/validators/utils/postal_code.js +29 -0
  46. package/esm/validators/utils/uuid4.js +20 -0
  47. package/esm/{validators.spec.js → validators/validators.spec.js} +1 -1
  48. package/internal.js +21 -0
  49. package/libs/form/src/Field/FieldBase.d.ts +3 -0
  50. package/libs/form/src/Field/FieldConditional.d.ts +4 -0
  51. package/libs/form/src/Field/FieldContainer.d.ts +4 -0
  52. package/libs/form/src/Field/index.d.ts +1 -3
  53. package/libs/form/src/Field/types.d.ts +2 -2
  54. package/libs/form/src/Field/utils.d.ts +5 -0
  55. package/libs/form/src/FormContext.d.ts +2 -2
  56. package/libs/form/src/internal.d.ts +2 -0
  57. package/libs/form/src/types.d.ts +3 -2
  58. package/libs/form/src/validators/index.d.ts +8 -0
  59. package/libs/form/src/validators/types.d.ts +2 -0
  60. package/libs/form/src/validators/utils/afm.d.ts +6 -0
  61. package/libs/form/src/validators/utils/file.d.ts +1 -0
  62. package/libs/form/src/validators/utils/iban.d.ts +2 -0
  63. package/libs/form/src/validators/utils/index.d.ts +12 -0
  64. package/libs/form/src/validators/utils/phone.d.ts +9 -0
  65. package/libs/form/src/validators/utils/postal_code.d.ts +2 -0
  66. package/libs/form/src/validators/utils/uuid4.d.ts +6 -0
  67. package/libs/form/src/{validators.spec.d.ts → validators/validators.spec.d.ts} +0 -0
  68. package/libs/ui/src/core/Button/BackButton.d.ts +1 -1
  69. package/libs/ui/src/core/Button/ButtonLink.d.ts +1 -1
  70. package/libs/ui/src/core/Button/index.d.ts +1 -1
  71. package/libs/ui/src/core/NavList/NavListItem.d.ts +1 -0
  72. package/libs/ui/src/core/NavList/NavListItemBase.d.ts +1 -0
  73. package/libs/ui/src/core/NavList/types.d.ts +1 -0
  74. package/libs-ui/react-core/src/AccordionSectionSummary/index.d.ts +2 -1
  75. package/libs-ui/react-core/src/BackLink/index.d.ts +1 -1
  76. package/libs-ui/react-core/src/Button/index.d.ts +1 -1
  77. package/libs-ui/react-core/src/ButtonLink/index.d.ts +1 -1
  78. package/libs-ui/react-core/src/CallToAction/index.d.ts +1 -1
  79. package/libs-ui/react-core/src/CheckboxItem/index.d.ts +1 -1
  80. package/libs-ui/react-core/src/DateInputItem/index.d.ts +1 -1
  81. package/libs-ui/react-core/src/Field/index.d.ts +3 -3
  82. package/libs-ui/react-core/src/FileUpload/index.d.ts +1 -1
  83. package/libs-ui/react-core/src/Form/index.d.ts +11 -0
  84. package/libs-ui/react-core/src/Link/index.d.ts +1 -1
  85. package/libs-ui/react-core/src/NotificationBannerLink/index.d.ts +1 -1
  86. package/libs-ui/react-core/src/RadioItem/index.d.ts +1 -1
  87. package/libs-ui/react-core/src/Select/index.d.ts +1 -1
  88. package/libs-ui/react-core/src/SvgIcon/index.d.ts +1 -1
  89. package/libs-ui/react-core/src/TabsListItem/index.d.ts +1 -1
  90. package/libs-ui/react-core/src/TextArea/index.d.ts +1 -1
  91. package/libs-ui/react-core/src/TextInput/index.d.ts +1 -1
  92. package/package.json +3 -2
  93. package/utils.js +6 -4
  94. package/validators/index.js +181 -0
  95. package/validators/types.js +5 -0
  96. package/validators/utils/afm.js +45 -0
  97. package/validators/utils/file.js +62 -0
  98. package/validators/utils/iban.js +134 -0
  99. package/validators/utils/index.js +210 -0
  100. package/validators/utils/phone.js +144 -0
  101. package/validators/utils/postal_code.js +40 -0
  102. package/validators/utils/uuid4.js +30 -0
  103. package/validators/validators.spec.js +88 -0
  104. package/es/validators.js +0 -626
  105. package/esm/validators.js +0 -626
  106. package/libs/form/src/validators.d.ts +0 -14
  107. package/validators.js +0 -662
  108. package/validators.spec.js +0 -88
package/esm/index.js CHANGED
@@ -1,4 +1,4 @@
1
- /** @license Digigov v0.6.10
1
+ /** @license Digigov v0.7.0
2
2
  *
3
3
  * This source code is licensed under the BSD-2-Clause license found in the
4
4
  * LICENSE file in the root directory of this source tree.
package/esm/internal.js CHANGED
@@ -1,5 +1,7 @@
1
1
  export { default as Field } from '@digigov/form/Field';
2
2
  export * from '@digigov/form/Field';
3
+ export { default as FieldConditional } from '@digigov/form/Field/FieldConditional';
4
+ export * from '@digigov/form/Field/FieldConditional';
3
5
  export { default as Fieldset } from '@digigov/form/Fieldset';
4
6
  export * from '@digigov/form/Fieldset';
5
7
  export { default as FormBuilder } from '@digigov/form/FormBuilder';
package/esm/utils.js CHANGED
@@ -1,9 +1,11 @@
1
- import _regeneratorRuntime from "@babel/runtime/regenerator";
1
+ import _typeof from "@babel/runtime/helpers/typeof";
2
2
  import _asyncToGenerator from "@babel/runtime/helpers/asyncToGenerator";
3
3
  import _defineProperty from "@babel/runtime/helpers/defineProperty";
4
4
  import _toConsumableArray from "@babel/runtime/helpers/toConsumableArray";
5
5
  import _extends from "@babel/runtime/helpers/extends";
6
6
 
7
+ function _regeneratorRuntime() { "use strict"; /*! regenerator-runtime -- Copyright (c) 2014-present, Facebook, Inc. -- license (MIT): https://github.com/facebook/regenerator/blob/main/LICENSE */ _regeneratorRuntime = function _regeneratorRuntime() { return exports; }; var exports = {}, Op = Object.prototype, hasOwn = Op.hasOwnProperty, $Symbol = "function" == typeof Symbol ? Symbol : {}, iteratorSymbol = $Symbol.iterator || "@@iterator", asyncIteratorSymbol = $Symbol.asyncIterator || "@@asyncIterator", toStringTagSymbol = $Symbol.toStringTag || "@@toStringTag"; function define(obj, key, value) { return Object.defineProperty(obj, key, { value: value, enumerable: !0, configurable: !0, writable: !0 }), obj[key]; } try { define({}, ""); } catch (err) { define = function define(obj, key, value) { return obj[key] = value; }; } function wrap(innerFn, outerFn, self, tryLocsList) { var protoGenerator = outerFn && outerFn.prototype instanceof Generator ? outerFn : Generator, generator = Object.create(protoGenerator.prototype), context = new Context(tryLocsList || []); return generator._invoke = function (innerFn, self, context) { var state = "suspendedStart"; return function (method, arg) { if ("executing" === state) throw new Error("Generator is already running"); if ("completed" === state) { if ("throw" === method) throw arg; return doneResult(); } for (context.method = method, context.arg = arg;;) { var delegate = context.delegate; if (delegate) { var delegateResult = maybeInvokeDelegate(delegate, context); if (delegateResult) { if (delegateResult === ContinueSentinel) continue; return delegateResult; } } if ("next" === context.method) context.sent = context._sent = context.arg;else if ("throw" === context.method) { if ("suspendedStart" === state) throw state = "completed", context.arg; context.dispatchException(context.arg); } else "return" === context.method && context.abrupt("return", context.arg); state = "executing"; var record = tryCatch(innerFn, self, context); if ("normal" === record.type) { if (state = context.done ? "completed" : "suspendedYield", record.arg === ContinueSentinel) continue; return { value: record.arg, done: context.done }; } "throw" === record.type && (state = "completed", context.method = "throw", context.arg = record.arg); } }; }(innerFn, self, context), generator; } function tryCatch(fn, obj, arg) { try { return { type: "normal", arg: fn.call(obj, arg) }; } catch (err) { return { type: "throw", arg: err }; } } exports.wrap = wrap; var ContinueSentinel = {}; function Generator() {} function GeneratorFunction() {} function GeneratorFunctionPrototype() {} var IteratorPrototype = {}; define(IteratorPrototype, iteratorSymbol, function () { return this; }); var getProto = Object.getPrototypeOf, NativeIteratorPrototype = getProto && getProto(getProto(values([]))); NativeIteratorPrototype && NativeIteratorPrototype !== Op && hasOwn.call(NativeIteratorPrototype, iteratorSymbol) && (IteratorPrototype = NativeIteratorPrototype); var Gp = GeneratorFunctionPrototype.prototype = Generator.prototype = Object.create(IteratorPrototype); function defineIteratorMethods(prototype) { ["next", "throw", "return"].forEach(function (method) { define(prototype, method, function (arg) { return this._invoke(method, arg); }); }); } function AsyncIterator(generator, PromiseImpl) { function invoke(method, arg, resolve, reject) { var record = tryCatch(generator[method], generator, arg); if ("throw" !== record.type) { var result = record.arg, value = result.value; return value && "object" == _typeof(value) && hasOwn.call(value, "__await") ? PromiseImpl.resolve(value.__await).then(function (value) { invoke("next", value, resolve, reject); }, function (err) { invoke("throw", err, resolve, reject); }) : PromiseImpl.resolve(value).then(function (unwrapped) { result.value = unwrapped, resolve(result); }, function (error) { return invoke("throw", error, resolve, reject); }); } reject(record.arg); } var previousPromise; this._invoke = function (method, arg) { function callInvokeWithMethodAndArg() { return new PromiseImpl(function (resolve, reject) { invoke(method, arg, resolve, reject); }); } return previousPromise = previousPromise ? previousPromise.then(callInvokeWithMethodAndArg, callInvokeWithMethodAndArg) : callInvokeWithMethodAndArg(); }; } function maybeInvokeDelegate(delegate, context) { var method = delegate.iterator[context.method]; if (undefined === method) { if (context.delegate = null, "throw" === context.method) { if (delegate.iterator["return"] && (context.method = "return", context.arg = undefined, maybeInvokeDelegate(delegate, context), "throw" === context.method)) return ContinueSentinel; context.method = "throw", context.arg = new TypeError("The iterator does not provide a 'throw' method"); } return ContinueSentinel; } var record = tryCatch(method, delegate.iterator, context.arg); if ("throw" === record.type) return context.method = "throw", context.arg = record.arg, context.delegate = null, ContinueSentinel; var info = record.arg; return info ? info.done ? (context[delegate.resultName] = info.value, context.next = delegate.nextLoc, "return" !== context.method && (context.method = "next", context.arg = undefined), context.delegate = null, ContinueSentinel) : info : (context.method = "throw", context.arg = new TypeError("iterator result is not an object"), context.delegate = null, ContinueSentinel); } function pushTryEntry(locs) { var entry = { tryLoc: locs[0] }; 1 in locs && (entry.catchLoc = locs[1]), 2 in locs && (entry.finallyLoc = locs[2], entry.afterLoc = locs[3]), this.tryEntries.push(entry); } function resetTryEntry(entry) { var record = entry.completion || {}; record.type = "normal", delete record.arg, entry.completion = record; } function Context(tryLocsList) { this.tryEntries = [{ tryLoc: "root" }], tryLocsList.forEach(pushTryEntry, this), this.reset(!0); } function values(iterable) { if (iterable) { var iteratorMethod = iterable[iteratorSymbol]; if (iteratorMethod) return iteratorMethod.call(iterable); if ("function" == typeof iterable.next) return iterable; if (!isNaN(iterable.length)) { var i = -1, next = function next() { for (; ++i < iterable.length;) { if (hasOwn.call(iterable, i)) return next.value = iterable[i], next.done = !1, next; } return next.value = undefined, next.done = !0, next; }; return next.next = next; } } return { next: doneResult }; } function doneResult() { return { value: undefined, done: !0 }; } return GeneratorFunction.prototype = GeneratorFunctionPrototype, define(Gp, "constructor", GeneratorFunctionPrototype), define(GeneratorFunctionPrototype, "constructor", GeneratorFunction), GeneratorFunction.displayName = define(GeneratorFunctionPrototype, toStringTagSymbol, "GeneratorFunction"), exports.isGeneratorFunction = function (genFun) { var ctor = "function" == typeof genFun && genFun.constructor; return !!ctor && (ctor === GeneratorFunction || "GeneratorFunction" === (ctor.displayName || ctor.name)); }, exports.mark = function (genFun) { return Object.setPrototypeOf ? Object.setPrototypeOf(genFun, GeneratorFunctionPrototype) : (genFun.__proto__ = GeneratorFunctionPrototype, define(genFun, toStringTagSymbol, "GeneratorFunction")), genFun.prototype = Object.create(Gp), genFun; }, exports.awrap = function (arg) { return { __await: arg }; }, defineIteratorMethods(AsyncIterator.prototype), define(AsyncIterator.prototype, asyncIteratorSymbol, function () { return this; }), exports.AsyncIterator = AsyncIterator, exports.async = function (innerFn, outerFn, self, tryLocsList, PromiseImpl) { void 0 === PromiseImpl && (PromiseImpl = Promise); var iter = new AsyncIterator(wrap(innerFn, outerFn, self, tryLocsList), PromiseImpl); return exports.isGeneratorFunction(outerFn) ? iter : iter.next().then(function (result) { return result.done ? result.value : iter.next(); }); }, defineIteratorMethods(Gp), define(Gp, toStringTagSymbol, "Generator"), define(Gp, iteratorSymbol, function () { return this; }), define(Gp, "toString", function () { return "[object Generator]"; }), exports.keys = function (object) { var keys = []; for (var key in object) { keys.push(key); } return keys.reverse(), function next() { for (; keys.length;) { var key = keys.pop(); if (key in object) return next.value = key, next.done = !1, next; } return next.done = !0, next; }; }, exports.values = values, Context.prototype = { constructor: Context, reset: function reset(skipTempReset) { if (this.prev = 0, this.next = 0, this.sent = this._sent = undefined, this.done = !1, this.delegate = null, this.method = "next", this.arg = undefined, this.tryEntries.forEach(resetTryEntry), !skipTempReset) for (var name in this) { "t" === name.charAt(0) && hasOwn.call(this, name) && !isNaN(+name.slice(1)) && (this[name] = undefined); } }, stop: function stop() { this.done = !0; var rootRecord = this.tryEntries[0].completion; if ("throw" === rootRecord.type) throw rootRecord.arg; return this.rval; }, dispatchException: function dispatchException(exception) { if (this.done) throw exception; var context = this; function handle(loc, caught) { return record.type = "throw", record.arg = exception, context.next = loc, caught && (context.method = "next", context.arg = undefined), !!caught; } for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i], record = entry.completion; if ("root" === entry.tryLoc) return handle("end"); if (entry.tryLoc <= this.prev) { var hasCatch = hasOwn.call(entry, "catchLoc"), hasFinally = hasOwn.call(entry, "finallyLoc"); if (hasCatch && hasFinally) { if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0); if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc); } else if (hasCatch) { if (this.prev < entry.catchLoc) return handle(entry.catchLoc, !0); } else { if (!hasFinally) throw new Error("try statement without catch or finally"); if (this.prev < entry.finallyLoc) return handle(entry.finallyLoc); } } } }, abrupt: function abrupt(type, arg) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.tryLoc <= this.prev && hasOwn.call(entry, "finallyLoc") && this.prev < entry.finallyLoc) { var finallyEntry = entry; break; } } finallyEntry && ("break" === type || "continue" === type) && finallyEntry.tryLoc <= arg && arg <= finallyEntry.finallyLoc && (finallyEntry = null); var record = finallyEntry ? finallyEntry.completion : {}; return record.type = type, record.arg = arg, finallyEntry ? (this.method = "next", this.next = finallyEntry.finallyLoc, ContinueSentinel) : this.complete(record); }, complete: function complete(record, afterLoc) { if ("throw" === record.type) throw record.arg; return "break" === record.type || "continue" === record.type ? this.next = record.arg : "return" === record.type ? (this.rval = this.arg = record.arg, this.method = "return", this.next = "end") : "normal" === record.type && afterLoc && (this.next = afterLoc), ContinueSentinel; }, finish: function finish(finallyLoc) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.finallyLoc === finallyLoc) return this.complete(entry.completion, entry.afterLoc), resetTryEntry(entry), ContinueSentinel; } }, "catch": function _catch(tryLoc) { for (var i = this.tryEntries.length - 1; i >= 0; --i) { var entry = this.tryEntries[i]; if (entry.tryLoc === tryLoc) { var record = entry.completion; if ("throw" === record.type) { var thrown = record.arg; resetTryEntry(entry); } return thrown; } } throw new Error("illegal catch attempt"); }, delegateYield: function delegateYield(iterable, resultName, nextLoc) { return this.delegate = { iterator: values(iterable), resultName: resultName, nextLoc: nextLoc }, "next" === this.method && (this.arg = undefined), ContinueSentinel; } }, exports; }
8
+
7
9
  /* eslint-disable @typescript-eslint/no-explicit-any */
8
10
  var parseErrorSchema = function parseErrorSchema(error, validateAllFieldCriteria) {
9
11
  return Array.isArray(error.inner) && error.inner.length ? error.inner.reduce(function (previous, _ref) {
@@ -29,11 +31,11 @@ export var yupResolver = function yupResolver(schema) {
29
31
  abortEarly: false
30
32
  };
31
33
  return /*#__PURE__*/function () {
32
- var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(values, context) {
34
+ var _ref4 = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime().mark(function _callee(values, context) {
33
35
  var validateAllFieldCriteria,
34
36
  parsedErrors,
35
37
  _args = arguments;
36
- return _regeneratorRuntime.wrap(function _callee$(_context) {
38
+ return _regeneratorRuntime().wrap(function _callee$(_context) {
37
39
  while (1) {
38
40
  switch (_context.prev = _context.next) {
39
41
  case 0:
@@ -0,0 +1,155 @@
1
+ import _typeof from "@babel/runtime/helpers/typeof";
2
+ import * as yup from 'yup';
3
+ import { useMemo } from 'react';
4
+ import dayjs from 'dayjs';
5
+ import customParseFormat from 'dayjs/plugin/customParseFormat';
6
+ import { getYupObjectShape, AFM_VALIDATOR, FILE_MAX_SIZE_VALIDATOR, IBAN_VALIDATOR, MOBILE_PHONE_VALIDATOR, PHONE_NUMBER_VALIDATOR, POSTALCODE_VALIDATOR, UUID4_VALIDATOR } from '@digigov/form/validators/utils';
7
+ dayjs.extend(customParseFormat);
8
+ var VALID_DATE_FORMATS = ['DD/MM/YYYY'];
9
+ var DATE_CACHE = {};
10
+
11
+ var getDate = function getDate(v) {
12
+ return DATE_CACHE[v] ? DATE_CACHE[v] : dayjs(v, VALID_DATE_FORMATS, true);
13
+ };
14
+
15
+ export var getYUPTypeMap = function getYUPTypeMap() {
16
+ var yupTypeMap = {
17
+ file: function file(field) {
18
+ return yup.mixed().transform(function (value) {
19
+ if (!value.length) {
20
+ return null;
21
+ }
22
+
23
+ return value;
24
+ }).nullable().test(FILE_MAX_SIZE_VALIDATOR(field));
25
+ },
26
+ string: yup.string,
27
+ text: yup.string,
28
+ "boolean": yup["boolean"],
29
+ object: function object(field) {
30
+ return getYupObjectShape(field.extra.fields, yupTypeMap);
31
+ },
32
+ array: function array(field) {
33
+ if (_typeof(field.extra.of) === 'object') {
34
+ // else if the field is an object then it is a scalar type
35
+ // eg. number, string etc.
36
+ return yup.array().of(yupTypeMap[field.extra.of.type](field.extra.of)); // .min(field.extra.min)
37
+ // .max(field.extra.max)
38
+ // .length(field.extra.length)
39
+ // .required(field.required);
40
+ } else if (typeof field.extra.of === 'string') {
41
+ return yup.array().of(yupTypeMap[field.extra.of]());
42
+ } else {
43
+ // in any other case it will be string
44
+ return yup.array().of(yup.string());
45
+ }
46
+ },
47
+ "int": function int() {
48
+ return yup.number().typeError('form.error.number').positive('form.error.positive_number');
49
+ },
50
+ email: function email() {
51
+ return yup.string().email('form.error.email');
52
+ },
53
+ afm: function afm() {
54
+ return yup.string().test(AFM_VALIDATOR);
55
+ },
56
+ uuid4: function uuid4() {
57
+ return yup.string().test(UUID4_VALIDATOR);
58
+ },
59
+ iban: function iban(field) {
60
+ return yup.string().test(IBAN_VALIDATOR(field));
61
+ },
62
+ postal_code: function postal_code(field) {
63
+ return yup.string().test(POSTALCODE_VALIDATOR(field));
64
+ },
65
+ mobile_phone: function mobile_phone() {
66
+ return yup.string().test(MOBILE_PHONE_VALIDATOR);
67
+ },
68
+ phone_number: function phone_number(field) {
69
+ return yup.string().test(PHONE_NUMBER_VALIDATOR(field));
70
+ },
71
+ 'choice:multiple': function choiceMultiple() {
72
+ return yup.array().of(yup.string()).nullable();
73
+ },
74
+ 'choice:single': function choiceSingle() {
75
+ return yup.string().nullable();
76
+ },
77
+ date: function date(field) {
78
+ var simpleDate = yup.string().nullable(true).test('date', 'form.error.date.invalid', function (value) {
79
+ if (!value) return true;
80
+ value = getDate(value);
81
+ return value.isValid();
82
+ });
83
+ var params = field.extra || {};
84
+
85
+ if (params.max) {
86
+ var maxDate;
87
+
88
+ if (params.max === 'now') {
89
+ var today = new Date();
90
+ maxDate = new Date(today);
91
+ } else {
92
+ maxDate = getDate(params.max).toDate();
93
+ }
94
+
95
+ var maxNextDate = new Date(maxDate);
96
+ maxNextDate.setDate(maxDate.getDate() + 1);
97
+ simpleDate = simpleDate.test({
98
+ name: 'earlier-than',
99
+ message: {
100
+ key: 'form.error.date.earlier_than',
101
+ context: {
102
+ maxDate: maxNextDate.toLocaleDateString()
103
+ }
104
+ },
105
+ test: function test(value) {
106
+ if (!value) return true;
107
+ value = getDate(value);
108
+ var isValid = +value.toDate() < +maxDate;
109
+ return isValid;
110
+ }
111
+ });
112
+ }
113
+
114
+ if (params.min) {
115
+ var minDate;
116
+
117
+ if (params.min === 'now') {
118
+ var _today = new Date();
119
+
120
+ minDate = new Date(_today);
121
+ } else {
122
+ minDate = getDate(params.min).toDate();
123
+ }
124
+
125
+ var minPreviousDate = new Date(minDate);
126
+ minPreviousDate.setDate(minDate.getDate() - 1);
127
+ simpleDate = simpleDate.test({
128
+ name: 'later-than',
129
+ message: {
130
+ key: 'form.error.date.later_than',
131
+ context: {
132
+ minDate: minPreviousDate.toLocaleDateString()
133
+ }
134
+ },
135
+ test: function test(value) {
136
+ if (!value) return true;
137
+ value = getDate(value);
138
+ var isValid = +value.toDate() > +minDate;
139
+ return isValid;
140
+ }
141
+ });
142
+ }
143
+
144
+ return simpleDate;
145
+ }
146
+ };
147
+ return yupTypeMap;
148
+ };
149
+ // Create a yup validation schema from given fields input
150
+ export function useValidationSchema(fields, validatorRegistry) {
151
+ return useMemo(function () {
152
+ var yupTypeMap = getYUPTypeMap();
153
+ return getYupObjectShape(fields, yupTypeMap, validatorRegistry);
154
+ }, []);
155
+ }
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,35 @@
1
+ export function validateAFM(afm) {
2
+ if (afm.length !== 9) {
3
+ // "afm should be 9 digits"
4
+ return false;
5
+ }
6
+
7
+ if (!/^\d+$/.test(afm)) {
8
+ // "This is not a number"
9
+ return false;
10
+ }
11
+
12
+ if (afm === '0'.repeat(9)) {
13
+ // "This is zero number (000000000)"
14
+ return false;
15
+ }
16
+
17
+ var sum = afm.substring(0, 8).split('').reduce(function (s, v, i) {
18
+ return s + (parseInt(v) << 8 - i);
19
+ }, 0);
20
+ var calc = sum % 11;
21
+ var d9 = parseInt(afm[8]);
22
+ var valid = calc % 10 === d9;
23
+ return valid;
24
+ }
25
+ export var AFM_VALIDATOR = {
26
+ name: 'afm-validator',
27
+ message: 'form.error.afm',
28
+ test: function test(value) {
29
+ if (value) {
30
+ return validateAFM(value);
31
+ }
32
+
33
+ return true;
34
+ }
35
+ };
@@ -0,0 +1,52 @@
1
+ function _createForOfIteratorHelper(o, allowArrayLike) { var it = typeof Symbol !== "undefined" && o[Symbol.iterator] || o["@@iterator"]; if (!it) { if (Array.isArray(o) || (it = _unsupportedIterableToArray(o)) || allowArrayLike && o && typeof o.length === "number") { if (it) o = it; var i = 0; var F = function F() {}; return { s: F, n: function n() { if (i >= o.length) return { done: true }; return { done: false, value: o[i++] }; }, e: function e(_e) { throw _e; }, f: F }; } throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); } var normalCompletion = true, didErr = false, err; return { s: function s() { it = it.call(o); }, n: function n() { var step = it.next(); normalCompletion = step.done; return step; }, e: function e(_e2) { didErr = true; err = _e2; }, f: function f() { try { if (!normalCompletion && it["return"] != null) it["return"](); } finally { if (didErr) throw err; } } }; }
2
+
3
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
4
+
5
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
6
+
7
+ var DEFAULT_FILE_MAX_SIZE = 10000000;
8
+ export var FILE_MAX_SIZE_VALIDATOR = function FILE_MAX_SIZE_VALIDATOR(field) {
9
+ return {
10
+ name: 'file-max-size-validator',
11
+ message: 'form.error.file_size',
12
+ test: function test(value) {
13
+ if (field.extra && field.extra.maxSize && value) {
14
+ var _iterator = _createForOfIteratorHelper(value),
15
+ _step;
16
+
17
+ try {
18
+ for (_iterator.s(); !(_step = _iterator.n()).done;) {
19
+ var file = _step.value;
20
+
21
+ if (file.size >= field.extra.maxSize) {
22
+ return false;
23
+ }
24
+ }
25
+ } catch (err) {
26
+ _iterator.e(err);
27
+ } finally {
28
+ _iterator.f();
29
+ }
30
+ } else if (value) {
31
+ var _iterator2 = _createForOfIteratorHelper(value),
32
+ _step2;
33
+
34
+ try {
35
+ for (_iterator2.s(); !(_step2 = _iterator2.n()).done;) {
36
+ var _file = _step2.value;
37
+
38
+ if (_file.size >= DEFAULT_FILE_MAX_SIZE) {
39
+ return false;
40
+ }
41
+ }
42
+ } catch (err) {
43
+ _iterator2.e(err);
44
+ } finally {
45
+ _iterator2.f();
46
+ }
47
+ }
48
+
49
+ return true;
50
+ }
51
+ };
52
+ };
@@ -0,0 +1,123 @@
1
+ export function validateIban(value, countryCode) {
2
+ value = value.replace(/\s/g, '').replace(/-/g, '');
3
+
4
+ if (value.match(/^[0-9]*$/i)) {
5
+ // this is for greek iban without the country code
6
+ if (value.length !== 25) {
7
+ return false;
8
+ }
9
+ } else {
10
+ value = value.toUpperCase();
11
+
12
+ if (countryCode) {
13
+ if (!value.startsWith(countryCode.toUpperCase())) {
14
+ return false;
15
+ }
16
+
17
+ if (value.length === ibanCountryCodesLengths[countryCode.toUpperCase()]) {
18
+ return true;
19
+ }
20
+ }
21
+
22
+ if (value.length !== 27) {
23
+ // this is for greek iban
24
+ return false;
25
+ }
26
+ }
27
+
28
+ return true;
29
+ }
30
+ export var IBAN_VALIDATOR = function IBAN_VALIDATOR(field) {
31
+ var _field$extra;
32
+
33
+ var countryCode = field === null || field === void 0 ? void 0 : (_field$extra = field.extra) === null || _field$extra === void 0 ? void 0 : _field$extra.country;
34
+ return {
35
+ name: 'iban-validator',
36
+ message: 'form.error.iban',
37
+ test: function test(value) {
38
+ if (!value) {
39
+ return true;
40
+ }
41
+
42
+ return validateIban(value, countryCode);
43
+ }
44
+ };
45
+ };
46
+ var ibanCountryCodesLengths = {
47
+ AD: 24,
48
+ AT: 20,
49
+ AZ: 28,
50
+ BH: 22,
51
+ BY: 28,
52
+ BE: 16,
53
+ BA: 20,
54
+ BR: 29,
55
+ BG: 22,
56
+ CR: 22,
57
+ HR: 21,
58
+ CY: 28,
59
+ CZ: 24,
60
+ DK: 18,
61
+ DO: 28,
62
+ EG: 29,
63
+ SV: 28,
64
+ EE: 20,
65
+ FO: 18,
66
+ FI: 18,
67
+ FR: 27,
68
+ GE: 22,
69
+ DE: 22,
70
+ GI: 23,
71
+ GR: 27,
72
+ GL: 18,
73
+ GT: 28,
74
+ VA: 22,
75
+ HU: 28,
76
+ IS: 26,
77
+ IQ: 23,
78
+ IE: 22,
79
+ IL: 23,
80
+ IT: 27,
81
+ JO: 30,
82
+ KZ: 20,
83
+ XK: 20,
84
+ KW: 30,
85
+ LV: 21,
86
+ LB: 28,
87
+ LI: 21,
88
+ LT: 20,
89
+ LU: 20,
90
+ MT: 31,
91
+ MR: 27,
92
+ MU: 30,
93
+ MD: 24,
94
+ MC: 27,
95
+ ME: 22,
96
+ NL: 18,
97
+ MK: 19,
98
+ NO: 15,
99
+ PK: 24,
100
+ PS: 29,
101
+ PL: 28,
102
+ PT: 25,
103
+ QA: 29,
104
+ RO: 24,
105
+ LC: 32,
106
+ SM: 27,
107
+ ST: 25,
108
+ SA: 24,
109
+ RS: 22,
110
+ SC: 31,
111
+ SK: 24,
112
+ SI: 19,
113
+ ES: 24,
114
+ SE: 24,
115
+ CH: 21,
116
+ TL: 23,
117
+ TN: 24,
118
+ TR: 26,
119
+ UA: 29,
120
+ AE: 23,
121
+ GB: 22,
122
+ VG: 24
123
+ };
@@ -0,0 +1,105 @@
1
+ import _extends from "@babel/runtime/helpers/extends";
2
+ import _slicedToArray from "@babel/runtime/helpers/slicedToArray";
3
+ import * as yup from 'yup';
4
+ export * from '@digigov/form/validators/utils/afm';
5
+ export * from '@digigov/form/validators/utils/file';
6
+ export * from '@digigov/form/validators/utils/iban';
7
+ export * from '@digigov/form/validators/utils/phone';
8
+ export * from '@digigov/form/validators/utils/postal_code';
9
+ export * from '@digigov/form/validators/utils/uuid4';
10
+ export function getYupField(field, yupTypeMap) {
11
+ var yupField = yupTypeMap[field.type] || yupTypeMap.string;
12
+ return yupField(field);
13
+ }
14
+ export function computeShape(fields, yupTypeMap, validatorRegistry) {
15
+ var fieldSchemas = {};
16
+ var objectFields = {};
17
+ fields.forEach(function (field) {
18
+ if (field.key.includes('.')) {
19
+ var _field$key$split = field.key.split(/\.(.+)/),
20
+ _field$key$split2 = _slicedToArray(_field$key$split, 2),
21
+ objectKey = _field$key$split2[0],
22
+ fieldKey = _field$key$split2[1];
23
+
24
+ if (!objectFields[objectKey]) {
25
+ objectFields[objectKey] = [];
26
+ }
27
+
28
+ objectFields[objectKey].push(_extends({}, field, {
29
+ key: fieldKey
30
+ }));
31
+ return;
32
+ }
33
+
34
+ var yupField = getYupField(field, yupTypeMap);
35
+
36
+ if (field.condition) {
37
+ for (var key in field.condition) {
38
+ var then = void 0,
39
+ otherwise = void 0;
40
+
41
+ if (field.condition[key].then) {
42
+ var _field$condition$key$, _field$condition$key$2;
43
+
44
+ if (((_field$condition$key$ = field.condition[key].then) === null || _field$condition$key$ === void 0 ? void 0 : _field$condition$key$.required) === false) {
45
+ then = yupField;
46
+ } else if (((_field$condition$key$2 = field.condition[key].then) === null || _field$condition$key$2 === void 0 ? void 0 : _field$condition$key$2.required) === true) {
47
+ then = yupField.required('form.error.required');
48
+ }
49
+ }
50
+
51
+ if (field.condition[key]["else"]) {
52
+ var _field$condition$key$3, _field$condition$key$4;
53
+
54
+ if (((_field$condition$key$3 = field.condition[key]["else"]) === null || _field$condition$key$3 === void 0 ? void 0 : _field$condition$key$3.required) === false) {
55
+ otherwise = yupField;
56
+ } else if (((_field$condition$key$4 = field.condition[key]["else"]) === null || _field$condition$key$4 === void 0 ? void 0 : _field$condition$key$4.required) === true) {
57
+ otherwise = yupField.required('form.error.required');
58
+ }
59
+ }
60
+
61
+ if (then || otherwise) {
62
+ yupField = yupField.when(key, {
63
+ is: field.condition[key].is,
64
+ then: then,
65
+ otherwise: otherwise
66
+ });
67
+ }
68
+ }
69
+ } else if (field.required) {
70
+ yupField = yupField.required('form.error.required');
71
+ }
72
+
73
+ if (validatorRegistry && field.type && validatorRegistry[field.type]) {
74
+ validatorRegistry[field.type].forEach(function (validator) {
75
+ yupField = yupField.test(validator);
76
+ });
77
+ }
78
+
79
+ if (field.validators) {
80
+ field.validators.forEach(function (validator) {
81
+ yupField = yupField.test(validator);
82
+ });
83
+ }
84
+
85
+ fieldSchemas[field.key] = yupField;
86
+ });
87
+
88
+ for (var objectKey in objectFields) {
89
+ fieldSchemas[objectKey] = getYupObjectShape(objectFields[objectKey], yupTypeMap, validatorRegistry);
90
+ }
91
+
92
+ return fieldSchemas;
93
+ }
94
+ export function getYupObjectShape( // eslint-disable-next-line @typescript-eslint/no-explicit-any
95
+ fields, yupTypeMap, validatorRegistry) {
96
+ if (!fields.current) {
97
+ var fieldSchemas = computeShape(fields, yupTypeMap, validatorRegistry);
98
+ return yup.object().shape(fieldSchemas);
99
+ }
100
+
101
+ return yup.lazy(function () {
102
+ var fieldSchemas = computeShape(Object.values(fields.current), yupTypeMap, validatorRegistry);
103
+ return yup.object().shape(fieldSchemas);
104
+ });
105
+ }
@@ -0,0 +1,122 @@
1
+ import * as gPhoneNumber from 'google-libphonenumber';
2
+ export function validatePhoneNumber(phoneNumber) {
3
+ var countries = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : ['gr'];
4
+ var typeOfPhoneNumber = arguments.length > 2 ? arguments[2] : undefined;
5
+ var phoneUtil = gPhoneNumber.PhoneNumberUtil.getInstance();
6
+
7
+ if (!countries || countries.length === 0) {
8
+ return true;
9
+ }
10
+
11
+ return countries.some(function (country) {
12
+ try {
13
+ var phone = phoneUtil.parse(phoneNumber, country.toUpperCase());
14
+
15
+ if (phoneUtil.isValidNumber(phone)) {
16
+ if (typeOfPhoneNumber) {
17
+ if (matchTypeOfPhoneNumber(phone, typeOfPhoneNumber, phoneUtil)) {
18
+ return true;
19
+ } else {
20
+ return false;
21
+ }
22
+ } else {
23
+ return true;
24
+ }
25
+ }
26
+
27
+ return false;
28
+ } catch (error) {
29
+ console.error(error);
30
+ return false;
31
+ }
32
+ });
33
+ }
34
+ var phoneNumberTypes = {
35
+ 0: 'landline',
36
+ 1: 'mobile',
37
+ 2: 'landline_or_mobile'
38
+ };
39
+ export function matchTypeOfPhoneNumber(phone, type, phoneUtil) {
40
+ try {
41
+ var phoneNumberType = phoneUtil.getNumberType(phone);
42
+ var numberType = phoneNumberTypes[phoneNumberType];
43
+
44
+ if (numberType === 'landline_or_mobile' || numberType === type) {
45
+ return true;
46
+ } else {
47
+ return false;
48
+ }
49
+ } catch (error) {
50
+ console.error(error);
51
+ return false;
52
+ }
53
+ }
54
+
55
+ function validateMobile(value) {
56
+ var phoneUtil = gPhoneNumber.PhoneNumberUtil.getInstance();
57
+ var origValue = value; // probably catch all the cases with a regex instead of gphonenumber
58
+
59
+ if (!value.match(/^((\+|00){0,1}\d{1,3}[- ]?)?\d{10}$/)) {
60
+ return false;
61
+ }
62
+
63
+ try {
64
+ var phone;
65
+
66
+ try {
67
+ phone = phoneUtil.parse(value);
68
+ } catch (err) {
69
+ try {
70
+ value = '+' + value;
71
+ phone = phoneUtil.parse(value);
72
+ } catch (err) {
73
+ value = '+30' + origValue;
74
+ phone = phoneUtil.parse(value);
75
+ }
76
+ }
77
+
78
+ return phoneUtil.isValidNumber(phone);
79
+ } catch (err) {
80
+ console.error(err);
81
+ return false;
82
+ }
83
+ }
84
+
85
+ export var MOBILE_PHONE_VALIDATOR = {
86
+ name: 'mobile-phone-validator',
87
+ message: 'form.error.mobile_phone',
88
+ test: function test(value) {
89
+ if (value) {
90
+ return validateMobile(value);
91
+ }
92
+
93
+ return true;
94
+ }
95
+ };
96
+ export var PHONE_NUMBER_VALIDATOR = function PHONE_NUMBER_VALIDATOR(field) {
97
+ var _field$extra, _field$extra2;
98
+
99
+ var countryCode = field === null || field === void 0 ? void 0 : (_field$extra = field.extra) === null || _field$extra === void 0 ? void 0 : _field$extra.countries;
100
+ var typeOfPhone = field === null || field === void 0 ? void 0 : (_field$extra2 = field.extra) === null || _field$extra2 === void 0 ? void 0 : _field$extra2.phoneType;
101
+ return {
102
+ name: 'phone-number-validator',
103
+ message: function message() {
104
+ if (typeOfPhone === 'mobile') {
105
+ return 'form.error.mobile_phone';
106
+ }
107
+
108
+ if (typeOfPhone === 'landline') {
109
+ return 'form.error.landline';
110
+ } else {
111
+ return 'form.error.phone_number';
112
+ }
113
+ },
114
+ test: function test(value) {
115
+ if (!value) {
116
+ return true;
117
+ }
118
+
119
+ return validatePhoneNumber(value, countryCode, typeOfPhone);
120
+ }
121
+ };
122
+ };