mobiletto-orm-typedef-gen 2.0.9 → 2.1.1

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.
@@ -21,8 +21,10 @@ const _findDecls = (name, fields, decls) => {
21
21
  return decls;
22
22
  };
23
23
  const capitalize = (s) => s.charAt(0).toUpperCase() + s.slice(1);
24
+ const onlyUnique = (value, index, array) => array.indexOf(value) === index;
24
25
  const defaultPrepareContext = (typeDef, ctx) => {
25
- for (const fieldName of Object.keys(ctx.fields)) {
26
+ const fieldNames = ctx.fieldNames;
27
+ for (const fieldName of fieldNames) {
26
28
  /* eslint-disable @typescript-eslint/no-explicit-any */
27
29
  const field = ctx.fields[fieldName];
28
30
  /* eslint-enable @typescript-eslint/no-explicit-any */
@@ -48,6 +50,7 @@ const defaultPrepareContext = (typeDef, ctx) => {
48
50
  errors[field.name] = ["indeterminateArrayValueType"];
49
51
  throw new MobilettoOrmValidationError(errors);
50
52
  }
53
+ field.arrayIndent = " ";
51
54
  }
52
55
  if (field.default) {
53
56
  field.defaultValue = JSON.stringify(field.default);
@@ -63,8 +66,26 @@ const defaultPrepareContext = (typeDef, ctx) => {
63
66
  field.isEnum = true;
64
67
  field.enumValues = JSON.stringify(field.items.map((i) => i.value));
65
68
  }
69
+ if (field.when) {
70
+ field.hasWhen = true;
71
+ field.whenCode = field.when.toString();
72
+ field.whenFieldNames =
73
+ field.whenFields ||
74
+ Array.from(field.whenCode.matchAll(/v\.([A-Za-z\d_.\s+]+)/g))
75
+ .map((matches) => (matches && matches.length > 1 ? matches[1] : null))
76
+ .filter((x) => x != null)
77
+ .map((f) => f === null || f === void 0 ? void 0 : f.replace(/\s+/, ""))
78
+ .map((f) => ((f === null || f === void 0 ? void 0 : f.includes(".")) ? f.substring(0, f.indexOf(".")) : f))
79
+ .filter(onlyUnique);
80
+ field.whenFieldNamesQuoted = field.whenFieldNames
81
+ .filter((f) => f !== fieldName)
82
+ .map((f) => JSON.stringify(f));
83
+ }
66
84
  field.Name = capitalize(field.name);
85
+ field.fullName = (ctx.name !== typeDef.typeName ? ctx.name + "_" : "") + field.name;
67
86
  }
87
+ ctx.fieldNamesArray = JSON.stringify(fieldNames);
88
+ ctx.fieldParamsArray = fieldNames.join(",");
68
89
  ctx.typeName = typeDef.typeName;
69
90
  ctx.curlyOpen = "{";
70
91
  ctx.curlyClose = "}";
@@ -88,6 +109,7 @@ export const generate = (typeDef, templatePath, opts) => {
88
109
  name: decl.name,
89
110
  Name: capitalize(decl.name),
90
111
  fields: decl.fields,
112
+ fieldNames: Object.keys(decl.fields),
91
113
  disclaimer: decls.length > 0 ? null : disclaimer,
92
114
  first,
93
115
  root: decl.root || false,
@@ -1,5 +1,25 @@
1
1
  {{#if disclaimer}}// {{{ disclaimer }}}
2
2
  import * as yup from "yup";{{/if}}
3
3
  export const {{Name}}Schema = yup.object({{{curlyOpen}}}{{#each fields}}
4
- {{name}}: {{#if typeIsObject}}{{ ../Name }}_{{name}}Schema{{else}}{{#if typeIsArray}}yup.array(yup.{{valueType}}),{{else}}yup.{{type}}(){{/if}}{{#if required}}.required('required'){{/if}}{{#if minValue}}.min({{minValue}}, 'minValue'){{/if}}{{#if maxValue}}.max({{maxValue}}, 'maxValue'){{/if}}{{#if min}}.min({{min}}, 'min'){{/if}}{{#if max}}.max({{max}}, 'max'){{/if}}{{#if regex}}.matches({{{regexValue}}}, 'regex'){{/if}}{{#if default}}.default({{{defaultValue}}}){{/if}}{{#if isEnum}}.oneOf({{{enumValues}}}, 'enum'){{/if}}.typeError('invalid'){{/if}},{{/each}}
4
+ {{name}}: {{#if typeIsObject}}{{ ../Name }}_{{name}}Schema{{else}}{{#if typeIsArray}}yup.array().of(yup.{{valueType}}{{else}}yup.{{type}}{{/if}}(){{#if isEnum}}
5
+ {{arrayIndent}}.oneOf({{{enumValues}}}, '{{fullName}}_enum'){{/if}}{{#if minValue}}
6
+ {{arrayIndent}}.min({{minValue}}, '{{fullName}}_minValue'){{/if}}{{#if maxValue}}
7
+ {{arrayIndent}}.max({{maxValue}}, '{{fullName}}_maxValue'){{/if}}{{#if min}}
8
+ {{arrayIndent}}.min({{min}}, '{{fullName}}_min'){{/if}}{{#if max}}
9
+ {{arrayIndent}}.max({{max}}, '{{fullName}}_max'){{/if}}{{#if regex}}
10
+ {{arrayIndent}}.matches({{{regexValue}}}, '{{fullName}}_regex'){{/if}}
11
+ {{arrayIndent}}.typeError('{{fullName}}_invalid'){{#if typeIsArray}})
12
+ .typeError('{{fullName}}_invalid'){{/if}}{{/if}}{{#if required}}
13
+ .required('{{fullName}}_required'){{else}}
14
+ .notRequired(){{/if}}{{#if default}}
15
+ .default({{{defaultValue}}}){{/if}}{{#if hasWhen}}
16
+ .when([{{{whenFieldNamesQuoted}}}], {
17
+ is: ({{whenFieldNames}}) => {
18
+ const w = {{{whenCode}}};
19
+ const v = { {{whenFieldNames}} };
20
+ return w(v);
21
+ },
22
+ then: (schema) => schema.required('{{fullName}}_required'),
23
+ otherwise: (schema) => schema.notRequired(),
24
+ }){{/if}},{{/each}}
5
25
  });
@@ -21,8 +21,10 @@ const _findDecls = (name, fields, decls) => {
21
21
  return decls;
22
22
  };
23
23
  const capitalize = (s) => s.charAt(0).toUpperCase() + s.slice(1);
24
+ const onlyUnique = (value, index, array) => array.indexOf(value) === index;
24
25
  const defaultPrepareContext = (typeDef, ctx) => {
25
- for (const fieldName of Object.keys(ctx.fields)) {
26
+ const fieldNames = ctx.fieldNames;
27
+ for (const fieldName of fieldNames) {
26
28
  /* eslint-disable @typescript-eslint/no-explicit-any */
27
29
  const field = ctx.fields[fieldName];
28
30
  /* eslint-enable @typescript-eslint/no-explicit-any */
@@ -48,6 +50,7 @@ const defaultPrepareContext = (typeDef, ctx) => {
48
50
  errors[field.name] = ["indeterminateArrayValueType"];
49
51
  throw new MobilettoOrmValidationError(errors);
50
52
  }
53
+ field.arrayIndent = " ";
51
54
  }
52
55
  if (field.default) {
53
56
  field.defaultValue = JSON.stringify(field.default);
@@ -63,8 +66,26 @@ const defaultPrepareContext = (typeDef, ctx) => {
63
66
  field.isEnum = true;
64
67
  field.enumValues = JSON.stringify(field.items.map((i) => i.value));
65
68
  }
69
+ if (field.when) {
70
+ field.hasWhen = true;
71
+ field.whenCode = field.when.toString();
72
+ field.whenFieldNames =
73
+ field.whenFields ||
74
+ Array.from(field.whenCode.matchAll(/v\.([A-Za-z\d_.\s+]+)/g))
75
+ .map((matches) => (matches && matches.length > 1 ? matches[1] : null))
76
+ .filter((x) => x != null)
77
+ .map((f) => f === null || f === void 0 ? void 0 : f.replace(/\s+/, ""))
78
+ .map((f) => ((f === null || f === void 0 ? void 0 : f.includes(".")) ? f.substring(0, f.indexOf(".")) : f))
79
+ .filter(onlyUnique);
80
+ field.whenFieldNamesQuoted = field.whenFieldNames
81
+ .filter((f) => f !== fieldName)
82
+ .map((f) => JSON.stringify(f));
83
+ }
66
84
  field.Name = capitalize(field.name);
85
+ field.fullName = (ctx.name !== typeDef.typeName ? ctx.name + "_" : "") + field.name;
67
86
  }
87
+ ctx.fieldNamesArray = JSON.stringify(fieldNames);
88
+ ctx.fieldParamsArray = fieldNames.join(",");
68
89
  ctx.typeName = typeDef.typeName;
69
90
  ctx.curlyOpen = "{";
70
91
  ctx.curlyClose = "}";
@@ -88,6 +109,7 @@ export const generate = (typeDef, templatePath, opts) => {
88
109
  name: decl.name,
89
110
  Name: capitalize(decl.name),
90
111
  fields: decl.fields,
112
+ fieldNames: Object.keys(decl.fields),
91
113
  disclaimer: decls.length > 0 ? null : disclaimer,
92
114
  first,
93
115
  root: decl.root || false,
@@ -1,5 +1,25 @@
1
1
  {{#if disclaimer}}// {{{ disclaimer }}}
2
2
  import * as yup from "yup";{{/if}}
3
3
  export const {{Name}}Schema = yup.object({{{curlyOpen}}}{{#each fields}}
4
- {{name}}: {{#if typeIsObject}}{{ ../Name }}_{{name}}Schema{{else}}{{#if typeIsArray}}yup.array(yup.{{valueType}}),{{else}}yup.{{type}}(){{/if}}{{#if required}}.required('required'){{/if}}{{#if minValue}}.min({{minValue}}, 'minValue'){{/if}}{{#if maxValue}}.max({{maxValue}}, 'maxValue'){{/if}}{{#if min}}.min({{min}}, 'min'){{/if}}{{#if max}}.max({{max}}, 'max'){{/if}}{{#if regex}}.matches({{{regexValue}}}, 'regex'){{/if}}{{#if default}}.default({{{defaultValue}}}){{/if}}{{#if isEnum}}.oneOf({{{enumValues}}}, 'enum'){{/if}}.typeError('invalid'){{/if}},{{/each}}
4
+ {{name}}: {{#if typeIsObject}}{{ ../Name }}_{{name}}Schema{{else}}{{#if typeIsArray}}yup.array().of(yup.{{valueType}}{{else}}yup.{{type}}{{/if}}(){{#if isEnum}}
5
+ {{arrayIndent}}.oneOf({{{enumValues}}}, '{{fullName}}_enum'){{/if}}{{#if minValue}}
6
+ {{arrayIndent}}.min({{minValue}}, '{{fullName}}_minValue'){{/if}}{{#if maxValue}}
7
+ {{arrayIndent}}.max({{maxValue}}, '{{fullName}}_maxValue'){{/if}}{{#if min}}
8
+ {{arrayIndent}}.min({{min}}, '{{fullName}}_min'){{/if}}{{#if max}}
9
+ {{arrayIndent}}.max({{max}}, '{{fullName}}_max'){{/if}}{{#if regex}}
10
+ {{arrayIndent}}.matches({{{regexValue}}}, '{{fullName}}_regex'){{/if}}
11
+ {{arrayIndent}}.typeError('{{fullName}}_invalid'){{#if typeIsArray}})
12
+ .typeError('{{fullName}}_invalid'){{/if}}{{/if}}{{#if required}}
13
+ .required('{{fullName}}_required'){{else}}
14
+ .notRequired(){{/if}}{{#if default}}
15
+ .default({{{defaultValue}}}){{/if}}{{#if hasWhen}}
16
+ .when([{{{whenFieldNamesQuoted}}}], {
17
+ is: ({{whenFieldNames}}) => {
18
+ const w = {{{whenCode}}};
19
+ const v = { {{whenFieldNames}} };
20
+ return w(v);
21
+ },
22
+ then: (schema) => schema.required('{{fullName}}_required'),
23
+ otherwise: (schema) => schema.notRequired(),
24
+ }){{/if}},{{/each}}
5
25
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "mobiletto-orm-typedef-gen",
3
- "version": "2.0.9",
3
+ "version": "2.1.1",
4
4
  "type": "module",
5
5
  "description": "generate various files based on mobiletto-orm-typedef definitions",
6
6
  "keywords": [
@@ -56,6 +56,6 @@
56
56
  },
57
57
  "dependencies": {
58
58
  "handlebars": "^4.7.7",
59
- "mobiletto-orm-typedef": "^2.1.8"
59
+ "mobiletto-orm-typedef": "^2.1.10"
60
60
  }
61
61
  }