@travetto/schema-faker 8.0.0-alpha.9 → 8.0.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.
package/README.md CHANGED
@@ -13,7 +13,7 @@ npm install @travetto/schema-faker
13
13
  yarn add @travetto/schema-faker
14
14
  ```
15
15
 
16
- In the course of application development, there is often a need to generate fake data on demand. Given all the information that we have about the schemas provided, translating that into data generation is fairly straightforward. The generation utility is built upon [faker](https://github.com/faker-js/faker), mapping data types, and various field names into specific [faker](https://github.com/faker-js/faker) generation routines.
16
+ In the course of application development, there is often a need to generate fake data on demand. Given all the information that we have about the schemas provided, translating that into data generation is fairly straightforward. The generation utility is built upon [faker](https://github.com/faker-js/faker), mapping data types, and various field names into specific [faker](https://github.com/faker-js/faker) generation routines.
17
17
 
18
18
  By default all types are mapped as-is:
19
19
  * `string`
@@ -41,7 +41,7 @@ static #namesToType = {
41
41
  [/^email(addr(ress)?)?$/, faker.internet.email],
42
42
  [/^(tele)?phone(num|number)?$/, faker.phone.number],
43
43
  [/^((postal|zip)code)|zip$/, faker.location.zipCode],
44
- [/f(irst)?name/, faker.person.firstName],
44
+ [/name|fname|firstName/i, faker.person.firstName],
45
45
  [/l(ast)?name/, faker.person.lastName],
46
46
  [/^ip(add(ress)?)?$/, faker.internet.ip],
47
47
  [/^ip(add(ress)?)?(v?)6$/, faker.internet.ipv6],
@@ -62,9 +62,9 @@ static #namesToType = {
62
62
  ]),
63
63
  date: new Map([
64
64
  [/dob|birth/, (): Date => faker.date.past({ years: 60 })],
65
- [/creat(e|ion)/, this.#between.bind(null, -200, -100)],
66
- [/(update|modif(y|ied))/, this.#between.bind(null, -100, -50)]
67
- ]),
65
+ [/create|creation/, this.#between.bind(null, -200, -100)],
66
+ [/(update|modify|modified)/, this.#between.bind(null, -100, -50)]
67
+ ])
68
68
  };
69
69
  ```
70
70
 
@@ -72,8 +72,8 @@ An example of this would be:
72
72
 
73
73
  **Code: More complex Schema, used with Faker**
74
74
  ```typescript
75
- import { SchemaFaker } from '@travetto/schema-faker';
76
75
  import { Schema } from '@travetto/schema';
76
+ import { SchemaFaker } from '@travetto/schema-faker';
77
77
 
78
78
  @Schema()
79
79
  class Address {
package/__index__.ts CHANGED
@@ -1 +1 @@
1
- export * from './src/faker.ts';
1
+ export * from './src/faker.ts';
package/package.json CHANGED
@@ -1,38 +1,38 @@
1
1
  {
2
2
  "name": "@travetto/schema-faker",
3
- "version": "8.0.0-alpha.9",
4
- "type": "module",
3
+ "version": "8.0.0",
5
4
  "description": "Data generation for schema-registered objects.",
6
5
  "keywords": [
6
+ "decorators",
7
7
  "faker",
8
8
  "schema",
9
- "typescript",
10
9
  "travetto",
11
- "decorators"
10
+ "typescript"
12
11
  ],
13
12
  "homepage": "https://travetto.io",
14
13
  "license": "MIT",
15
14
  "author": {
16
- "email": "travetto.framework@gmail.com",
17
- "name": "Travetto Framework"
15
+ "name": "Travetto Framework",
16
+ "email": "travetto.framework@gmail.com"
17
+ },
18
+ "repository": {
19
+ "url": "git+https://github.com/travetto/travetto.git",
20
+ "directory": "module/schema-faker"
18
21
  },
19
22
  "files": [
20
23
  "__index__.ts",
21
24
  "src"
22
25
  ],
26
+ "type": "module",
23
27
  "main": "__index__.ts",
24
- "repository": {
25
- "url": "git+https://github.com/travetto/travetto.git",
26
- "directory": "module/schema-faker"
28
+ "publishConfig": {
29
+ "access": "public"
27
30
  },
28
31
  "dependencies": {
29
- "@faker-js/faker": "^10.3.0",
30
- "@travetto/schema": "^8.0.0-alpha.9"
32
+ "@faker-js/faker": "^10.6.0",
33
+ "@travetto/schema": "^8.0.0"
31
34
  },
32
35
  "travetto": {
33
36
  "displayName": "Schema Faker"
34
- },
35
- "publishConfig": {
36
- "access": "public"
37
37
  }
38
38
  }
package/src/faker.ts CHANGED
@@ -1,13 +1,12 @@
1
1
  import { faker } from '@faker-js/faker';
2
2
 
3
- import { castTo, type Class, TimeUtil } from '@travetto/runtime';
4
- import { BindUtil, type SchemaFieldConfig, CommonRegex, SchemaRegistryIndex } from '@travetto/schema';
3
+ import { type Class, castTo, TimeUtil } from '@travetto/runtime';
4
+ import { BindUtil, CommonRegex, type SchemaFieldConfig, SchemaRegistryIndex } from '@travetto/schema';
5
5
 
6
6
  /**
7
7
  * Provide a faker utility for generating content
8
8
  */
9
9
  export class SchemaFaker {
10
-
11
10
  static #stringToReType = new Map<RegExp, () => string>([
12
11
  [CommonRegex.email, faker.internet.email],
13
12
  [CommonRegex.url, faker.internet.url],
@@ -25,7 +24,7 @@ export class SchemaFaker {
25
24
  [/^email(addr(ress)?)?$/, faker.internet.email],
26
25
  [/^(tele)?phone(num|number)?$/, faker.phone.number],
27
26
  [/^((postal|zip)code)|zip$/, faker.location.zipCode],
28
- [/f(irst)?name/, faker.person.firstName],
27
+ [/name|fname|firstName/i, faker.person.firstName],
29
28
  [/l(ast)?name/, faker.person.lastName],
30
29
  [/^ip(add(ress)?)?$/, faker.internet.ip],
31
30
  [/^ip(add(ress)?)?(v?)6$/, faker.internet.ipv6],
@@ -46,9 +45,9 @@ export class SchemaFaker {
46
45
  ]),
47
46
  date: new Map([
48
47
  [/dob|birth/, (): Date => faker.date.past({ years: 60 })],
49
- [/creat(e|ion)/, this.#between.bind(null, -200, -100)],
50
- [/(update|modif(y|ied))/, this.#between.bind(null, -100, -50)]
51
- ]),
48
+ [/create|creation/, this.#between.bind(null, -200, -100)],
49
+ [/(update|modify|modified)/, this.#between.bind(null, -100, -50)]
50
+ ])
52
51
  };
53
52
 
54
53
  static #between(fromDays: number, toDays: number): Date {
@@ -67,7 +66,7 @@ export class SchemaFaker {
67
66
  const max = config.maxlength ? config.maxlength.limit : 10;
68
67
  const size = faker.number.int({ min, max });
69
68
  const out: unknown[] = [];
70
- for (let i = 0; i < size; i++) {
69
+ for (let i = 0; i < size; i += 1) {
71
70
  out.push(this.#value(config, true));
72
71
  }
73
72
  return out;
@@ -89,10 +88,10 @@ export class SchemaFaker {
89
88
  let offset = 1;
90
89
 
91
90
  if (precision !== undefined) {
92
- min ??= -((10 ** precision[0]) - 1);
93
- max ??= ((10 ** precision[0]) - 1);
91
+ min ??= -(10 ** precision[0] - 1);
92
+ max ??= 10 ** precision[0] - 1;
94
93
  if (precision[1] !== undefined) {
95
- offset = (10 ** (precision[1] || 0));
94
+ offset = 10 ** (precision[1] || 0);
96
95
  }
97
96
  }
98
97
 
@@ -101,10 +100,9 @@ export class SchemaFaker {
101
100
 
102
101
  const range = (max - min) * offset;
103
102
  const result = Math.trunc(Math.random() * range);
104
- return (result / offset) + min;
103
+ return result / offset + min;
105
104
  }
106
105
 
107
-
108
106
  /**
109
107
  * Get a new bigint value
110
108
  * @param config BigInt config
@@ -112,7 +110,7 @@ export class SchemaFaker {
112
110
  static #bigInt(config: SchemaFieldConfig): bigint {
113
111
  const min = config.min && typeof config.min.limit === 'bigint' ? config.min.limit : 0n;
114
112
  const max = config.max && typeof config.max.limit === 'bigint' ? config.max.limit : 1000n;
115
- return faker.number.bigInt({ min, max, });
113
+ return faker.number.bigInt({ min, max });
116
114
  }
117
115
 
118
116
  /**
@@ -144,7 +142,7 @@ export class SchemaFaker {
144
142
  const name = config.name.toLowerCase();
145
143
 
146
144
  if (config.match) {
147
- const mre = config.match && config.match.regex;
145
+ const mre = config.match.regex;
148
146
  for (const [regex, fn] of this.#stringToReType) {
149
147
  if (mre === regex) {
150
148
  return fn();
@@ -172,11 +170,16 @@ export class SchemaFaker {
172
170
  return faker.helpers.arrayElement(config.enum.values);
173
171
  } else {
174
172
  switch (config.type) {
175
- case Number: return this.#number(config);
176
- case castTo(BigInt): return this.#bigInt(config);
177
- case String: return this.#string(config);
178
- case Date: return this.#date(config);
179
- case Boolean: return faker.datatype.boolean();
173
+ case Number:
174
+ return this.#number(config);
175
+ case castTo(BigInt):
176
+ return this.#bigInt(config);
177
+ case String:
178
+ return this.#string(config);
179
+ case Date:
180
+ return this.#date(config);
181
+ case Boolean:
182
+ return faker.datatype.boolean();
180
183
  }
181
184
  if (SchemaRegistryIndex.has(config.type)) {
182
185
  return this.generate(config.type);
@@ -194,10 +197,11 @@ export class SchemaFaker {
194
197
  const out: Record<string, unknown> = {};
195
198
 
196
199
  for (const [name, fieldConfig] of Object.entries(fields)) {
197
- if (name === 'type' || name === 'id') { // Do not set primary fields
200
+ if (name === 'type' || name === 'id') {
201
+ // Do not set primary fields
198
202
  continue;
199
203
  }
200
- if (fieldConfig.required?.active === false && (Math.random() < .5)) {
204
+ if (fieldConfig.required?.active === false && Math.random() < 0.5) {
201
205
  continue;
202
206
  }
203
207
  out[name] = this.#value(fieldConfig);
@@ -205,4 +209,4 @@ export class SchemaFaker {
205
209
 
206
210
  return BindUtil.bindSchema(cls, out, { view });
207
211
  }
208
- }
212
+ }