@travetto/schema-faker 7.0.0-rc.1 → 7.0.0-rc.3

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 +47 -47
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@travetto/schema-faker",
3
- "version": "7.0.0-rc.1",
3
+ "version": "7.0.0-rc.3",
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.1"
29
+ "@travetto/schema": "^7.0.0-rc.3"
30
30
  },
31
31
  "travetto": {
32
32
  "displayName": "Schema Faker"
package/src/faker.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  import { faker } from '@faker-js/faker';
2
2
 
3
3
  import { Class, TimeUtil } from '@travetto/runtime';
4
- import { BindUtil, SchemaFieldConfig, CommonRegExp, SchemaRegistryIndex } from '@travetto/schema';
4
+ import { BindUtil, SchemaFieldConfig, CommonRegex, SchemaRegistryIndex } from '@travetto/schema';
5
5
 
6
6
 
7
7
  /**
@@ -10,10 +10,10 @@ import { BindUtil, SchemaFieldConfig, CommonRegExp, SchemaRegistryIndex } from '
10
10
  export class SchemaFaker {
11
11
 
12
12
  static #stringToReType = new Map<RegExp, () => string>([
13
- [CommonRegExp.email, faker.internet.email],
14
- [CommonRegExp.url, faker.internet.url],
15
- [CommonRegExp.telephone, faker.phone.number],
16
- [CommonRegExp.postalCode, faker.location.zipCode]
13
+ [CommonRegex.email, faker.internet.email],
14
+ [CommonRegex.url, faker.internet.url],
15
+ [CommonRegex.telephone, faker.phone.number],
16
+ [CommonRegex.postalCode, faker.location.zipCode]
17
17
  ]);
18
18
 
19
19
  /**
@@ -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.limit : 0;
68
+ const max = config.maxlength ? config.maxlength.limit : 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.limit === 'number' ? config.min.limit : undefined;
83
+ let max = config.max && typeof config.max.limit === 'number' ? config.max.limit : 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,25 +102,25 @@ 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.limit !== 'number' ? config.min.limit : undefined;
117
+ const max = config.max && typeof config.max.limit !== 'number' ? config.max.limit : 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() });
121
121
  } else {
122
- for (const [re, fn] of this.#namesToType.date) {
123
- if (re.test(name)) {
122
+ for (const [regex, fn] of this.#namesToType.date) {
123
+ if (regex.test(name)) {
124
124
  return fn();
125
125
  }
126
126
  }
@@ -130,22 +130,22 @@ 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;
140
- for (const [re, fn] of this.#stringToReType) {
141
- if (mre === re) {
138
+ if (config.match) {
139
+ const mre = config.match && config.match.regex;
140
+ for (const [regex, fn] of this.#stringToReType) {
141
+ if (mre === regex) {
142
142
  return fn();
143
143
  }
144
144
  }
145
145
  }
146
146
 
147
- for (const [re, fn] of this.#namesToType.string) {
148
- if (re.test(name)) {
147
+ for (const [regex, fn] of this.#namesToType.string) {
148
+ if (regex.test(name)) {
149
149
  return fn();
150
150
  }
151
151
  }
@@ -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)) {
@@ -189,14 +189,14 @@ export class SchemaFaker {
189
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 });