@travetto/schema-faker 7.0.0-rc.0 → 7.0.0-rc.2

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 (2) hide show
  1. package/package.json +2 -2
  2. package/src/faker.ts +37 -37
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/schema-faker",
3
- "version": "7.0.0-rc.0",
3
+ "version": "7.0.0-rc.2",
4
4
  "description": "Data generation for schema-registered objects.",
5
5
  "keywords": [
6
6
  "faker",
@@ -26,7 +26,7 @@
26
26
  },
27
27
  "dependencies": {
28
28
  "@faker-js/faker": "^10.1.0",
29
- "@travetto/schema": "^7.0.0-rc.0"
29
+ "@travetto/schema": "^7.0.0-rc.2"
30
30
  },
31
31
  "travetto": {
32
32
  "displayName": "Schema Faker"
package/src/faker.ts CHANGED
@@ -61,29 +61,29 @@ export class SchemaFaker {
61
61
 
62
62
  /**
63
63
  * Get an array of values
64
- * @param cfg Field configuration
64
+ * @param config Field configuration
65
65
  */
66
- static #array(cfg: SchemaFieldConfig): unknown[] {
67
- const min = cfg.minlength ? cfg.minlength.n : 0;
68
- const max = cfg.maxlength ? cfg.maxlength.n : 10;
66
+ static #array(config: SchemaFieldConfig): unknown[] {
67
+ const min = config.minlength ? config.minlength.n : 0;
68
+ const max = config.maxlength ? config.maxlength.n : 10;
69
69
  const size = faker.number.int({ min, max });
70
70
  const out: unknown[] = [];
71
71
  for (let i = 0; i < size; i++) {
72
- out.push(this.#value(cfg, true));
72
+ out.push(this.#value(config, true));
73
73
  }
74
74
  return out;
75
75
  }
76
76
 
77
77
  /**
78
78
  * Get a new number value
79
- * @param cfg Number config
79
+ * @param config Number config
80
80
  */
81
- static #number(cfg: SchemaFieldConfig): number {
82
- let min = cfg.min && typeof cfg.min.n === 'number' ? cfg.min.n : undefined;
83
- let max = cfg.max && typeof cfg.max.n === 'number' ? cfg.max.n : undefined;
84
- let precision = cfg.precision;
81
+ static #number(config: SchemaFieldConfig): number {
82
+ let min = config.min && typeof config.min.n === 'number' ? config.min.n : undefined;
83
+ let max = config.max && typeof config.max.n === 'number' ? config.max.n : undefined;
84
+ let precision = config.precision;
85
85
 
86
- if (/(price|amt|amount)$/i.test(cfg.name.toString())) {
86
+ if (/(price|amt|amount)$/i.test(config.name)) {
87
87
  precision = [13, 2];
88
88
  }
89
89
 
@@ -102,19 +102,19 @@ export class SchemaFaker {
102
102
 
103
103
  const range = (max - min) * offset;
104
104
 
105
- const val = Math.trunc(Math.random() * range);
105
+ const result = Math.trunc(Math.random() * range);
106
106
 
107
- return (val / offset) + min;
107
+ return (result / offset) + min;
108
108
  }
109
109
 
110
110
  /**
111
111
  * Get a date value
112
- * @param cfg Field config
112
+ * @param config Field config
113
113
  */
114
- static #date(cfg: SchemaFieldConfig): Date {
115
- const name = cfg.name.toString().toLowerCase();
116
- const min = cfg.min && typeof cfg.min.n !== 'number' ? cfg.min.n : undefined;
117
- const max = cfg.max && typeof cfg.max.n !== 'number' ? cfg.max.n : undefined;
114
+ static #date(config: SchemaFieldConfig): Date {
115
+ const name = config.name.toLowerCase();
116
+ const min = config.min && typeof config.min.n !== 'number' ? config.min.n : undefined;
117
+ const max = config.max && typeof config.max.n !== 'number' ? config.max.n : undefined;
118
118
 
119
119
  if (min !== undefined || max !== undefined) {
120
120
  return faker.date.between({ from: min || TimeUtil.fromNow(-50, 'd'), to: max || new Date() });
@@ -130,13 +130,13 @@ export class SchemaFaker {
130
130
 
131
131
  /**
132
132
  * Get a string value
133
- * @param cfg Field config
133
+ * @param config Field config
134
134
  */
135
- static #string(cfg: SchemaFieldConfig): string {
136
- const name = cfg.name.toString().toLowerCase();
135
+ static #string(config: SchemaFieldConfig): string {
136
+ const name = config.name.toLowerCase();
137
137
 
138
- if (cfg.match) {
139
- const mre = cfg.match && cfg.match.re;
138
+ if (config.match) {
139
+ const mre = config.match && config.match.re;
140
140
  for (const [re, fn] of this.#stringToReType) {
141
141
  if (mre === re) {
142
142
  return fn();
@@ -155,23 +155,23 @@ export class SchemaFaker {
155
155
 
156
156
  /**
157
157
  * Get a value for a field config
158
- * @param cfg Field config
158
+ * @param config Field config
159
159
  */
160
- static #value(cfg: SchemaFieldConfig, subArray = false): unknown {
161
- if (!subArray && cfg.array) {
162
- return this.#array(cfg);
163
- } else if (cfg.enum) {
164
- return faker.helpers.arrayElement(cfg.enum.values);
160
+ static #value(config: SchemaFieldConfig, subArray = false): unknown {
161
+ if (!subArray && config.array) {
162
+ return this.#array(config);
163
+ } else if (config.enum) {
164
+ return faker.helpers.arrayElement(config.enum.values);
165
165
  } else {
166
166
 
167
- const typ = cfg.type;
167
+ const typ = config.type;
168
168
 
169
169
  if (typ === Number) {
170
- return this.#number(cfg);
170
+ return this.#number(config);
171
171
  } else if (typ === String) {
172
- return this.#string(cfg);
172
+ return this.#string(config);
173
173
  } else if (typ === Date) {
174
- return this.#date(cfg);
174
+ return this.#date(config);
175
175
  } else if (typ === Boolean) {
176
176
  return faker.datatype.boolean();
177
177
  } else if (SchemaRegistryIndex.has(typ)) {
@@ -186,17 +186,17 @@ export class SchemaFaker {
186
186
  * @param view The view to define specifically
187
187
  */
188
188
  static generate<T>(cls: Class<T>, view?: string): T {
189
- const fields = SchemaRegistryIndex.getFieldMap(cls, view);
189
+ const fields = SchemaRegistryIndex.get(cls).getFields(view);
190
190
  const out: Record<string, unknown> = {};
191
191
 
192
- for (const [f, fieldConfig] of Object.entries(fields)) {
193
- if (f === 'type' || f === 'id') { // Do not set primary fields
192
+ for (const [name, fieldConfig] of Object.entries(fields)) {
193
+ if (name === 'type' || name === 'id') { // Do not set primary fields
194
194
  continue;
195
195
  }
196
196
  if (fieldConfig.required?.active === false && (Math.random() < .5)) {
197
197
  continue;
198
198
  }
199
- out[f] = this.#value(fieldConfig);
199
+ out[name] = this.#value(fieldConfig);
200
200
  }
201
201
 
202
202
  return BindUtil.bindSchema(cls, out, { view });