@naturalcycles/nodejs-lib 15.50.0 → 15.50.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.
@@ -220,23 +220,42 @@ export function createAjv(opt) {
220
220
  ajv.addKeyword({
221
221
  keyword: 'email',
222
222
  type: 'string',
223
- modifying: false,
223
+ modifying: true,
224
224
  errors: true,
225
225
  schemaType: 'object',
226
226
  validate: function validate(opt, data, _schema, ctx) {
227
227
  const { checkTLD } = opt;
228
- if (!checkTLD)
229
- return true;
230
- const tld = _substringAfterLast(data, '.');
231
- if (validTLDs.has(tld))
232
- return true;
233
- validate.errors = [
234
- {
235
- instancePath: ctx?.instancePath ?? '',
236
- message: `has an invalid TLD`,
237
- },
238
- ];
239
- return false;
228
+ const cleanData = data.trim();
229
+ // from `ajv-formats`
230
+ const EMAIL_REGEX = /^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/i;
231
+ const result = cleanData.match(EMAIL_REGEX);
232
+ if (!result) {
233
+ ;
234
+ validate.errors = [
235
+ {
236
+ instancePath: ctx?.instancePath ?? '',
237
+ message: `is not a valid email address`,
238
+ },
239
+ ];
240
+ return false;
241
+ }
242
+ if (checkTLD) {
243
+ const tld = _substringAfterLast(cleanData, '.');
244
+ if (!validTLDs.has(tld)) {
245
+ ;
246
+ validate.errors = [
247
+ {
248
+ instancePath: ctx?.instancePath ?? '',
249
+ message: `has an invalid TLD`,
250
+ },
251
+ ];
252
+ return false;
253
+ }
254
+ }
255
+ if (ctx?.parentData && ctx.parentDataProperty) {
256
+ ctx.parentData[ctx.parentDataProperty] = cleanData;
257
+ }
258
+ return true;
240
259
  },
241
260
  });
242
261
  ajv.addKeyword({
@@ -217,9 +217,7 @@ export class JsonSchemaStringBuilder extends JsonSchemaAnyBuilder {
217
217
  email(opt) {
218
218
  const defaultOptions = { checkTLD: true };
219
219
  _objectAssign(this.schema, { email: { ...defaultOptions, ...opt } });
220
- // from `ajv-formats`
221
- const regex = /^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/i;
222
- return this.regex(regex, { msg: 'is not a valid email address' }).trim().toLowerCase();
220
+ return this.trim().toLowerCase();
223
221
  }
224
222
  trim() {
225
223
  _objectAssign(this.schema, { transform: { ...this.schema.transform, trim: true } });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
3
  "type": "module",
4
- "version": "15.50.0",
4
+ "version": "15.50.1",
5
5
  "dependencies": {
6
6
  "@naturalcycles/js-lib": "^15",
7
7
  "@types/js-yaml": "^4",
@@ -73,7 +73,7 @@ export class AjvSchema<IN = unknown, OUT = IN> {
73
73
  let jsonSchema: JsonSchema<IN, OUT>
74
74
 
75
75
  if (AjvSchema.isJsonSchemaBuilder(schema)) {
76
- jsonSchema = schema.build()
76
+ jsonSchema = (schema as JsonSchemaTerminal<IN, OUT, any>).build()
77
77
  AjvSchema.requireValidJsonSchema(jsonSchema)
78
78
  } else {
79
79
  jsonSchema = schema
@@ -244,22 +244,46 @@ export function createAjv(opt?: Options): Ajv {
244
244
  ajv.addKeyword({
245
245
  keyword: 'email',
246
246
  type: 'string',
247
- modifying: false,
247
+ modifying: true,
248
248
  errors: true,
249
249
  schemaType: 'object',
250
250
  validate: function validate(opt: JsonSchemaStringEmailOptions, data: string, _schema, ctx) {
251
251
  const { checkTLD } = opt
252
- if (!checkTLD) return true
252
+ const cleanData = data.trim()
253
253
 
254
- const tld = _substringAfterLast(data, '.')
255
- if (validTLDs.has(tld)) return true
256
- ;(validate as any).errors = [
257
- {
258
- instancePath: ctx?.instancePath ?? '',
259
- message: `has an invalid TLD`,
260
- },
261
- ]
262
- return false
254
+ // from `ajv-formats`
255
+ const EMAIL_REGEX =
256
+ /^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/i
257
+ const result = cleanData.match(EMAIL_REGEX)
258
+
259
+ if (!result) {
260
+ ;(validate as any).errors = [
261
+ {
262
+ instancePath: ctx?.instancePath ?? '',
263
+ message: `is not a valid email address`,
264
+ },
265
+ ]
266
+ return false
267
+ }
268
+
269
+ if (checkTLD) {
270
+ const tld = _substringAfterLast(cleanData, '.')
271
+ if (!validTLDs.has(tld)) {
272
+ ;(validate as any).errors = [
273
+ {
274
+ instancePath: ctx?.instancePath ?? '',
275
+ message: `has an invalid TLD`,
276
+ },
277
+ ]
278
+ return false
279
+ }
280
+ }
281
+
282
+ if (ctx?.parentData && ctx.parentDataProperty) {
283
+ ctx.parentData[ctx.parentDataProperty] = cleanData
284
+ }
285
+
286
+ return true
263
287
  },
264
288
  })
265
289
 
@@ -302,11 +302,7 @@ export class JsonSchemaStringBuilder<
302
302
  email(opt?: Partial<JsonSchemaStringEmailOptions>): this {
303
303
  const defaultOptions: JsonSchemaStringEmailOptions = { checkTLD: true }
304
304
  _objectAssign(this.schema, { email: { ...defaultOptions, ...opt } })
305
-
306
- // from `ajv-formats`
307
- const regex =
308
- /^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$/i
309
- return this.regex(regex, { msg: 'is not a valid email address' }).trim().toLowerCase()
305
+ return this.trim().toLowerCase()
310
306
  }
311
307
 
312
308
  trim(): this {