jtcsv 2.1.5 → 2.2.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.
@@ -20,6 +20,9 @@ const {
20
20
  const { Transform, Writable } = require('stream');
21
21
  const { pipeline } = require('stream/promises');
22
22
 
23
+ // Import schema validator from utils
24
+ const { createSchemaValidators } = require('./src/utils/schema-validator');
25
+
23
26
  /**
24
27
  * Creates a transform stream that converts CSV chunks to JSON objects
25
28
  *
@@ -500,90 +503,6 @@ async function createCsvFileToJsonStream(filePath, options = {}) {
500
503
  }, 'FILE_STREAM_ERROR', { function: 'createCsvFileToJsonStream' });
501
504
  }
502
505
 
503
- /**
504
- * Creates schema validators from JSON schema
505
- *
506
- * @private
507
- * @param {Object} schema - JSON schema
508
- * @returns {Object} Validators object
509
- */
510
- function createSchemaValidators(schema) {
511
- const validators = {};
512
-
513
- if (!schema.properties) {
514
- return validators;
515
- }
516
-
517
- for (const [key, definition] of Object.entries(schema.properties)) {
518
- const validator = {
519
- type: definition.type,
520
- required: schema.required && schema.required.includes(key)
521
- };
522
-
523
- // Add format function for dates
524
- if (definition.type === 'string' && definition.format === 'date-time') {
525
- validator.format = (value) => {
526
- if (value instanceof Date) {
527
- return value.toISOString();
528
- }
529
- /* istanbul ignore next */
530
- if (typeof value === 'string') {
531
- // Try to parse as date
532
- const date = new Date(value);
533
- if (!isNaN(date.getTime())) {
534
- return date.toISOString();
535
- }
536
- }
537
- return value;
538
- };
539
- }
540
-
541
- // Add validation function
542
- validator.validate = (value) => {
543
- if (value === null || value === undefined) {
544
- return !validator.required;
545
- }
546
-
547
- // Type validation
548
- if (definition.type === 'string' && typeof value !== 'string') {
549
- return false;
550
- }
551
- if (definition.type === 'number' && typeof value !== 'number') {
552
- return false;
553
- }
554
- if (definition.type === 'integer' && (!Number.isInteger(value) || typeof value !== 'number')) {
555
- return false;
556
- }
557
- if (definition.type === 'boolean' && typeof value !== 'boolean') {
558
- return false;
559
- }
560
-
561
- // Additional constraints
562
- if (definition.minimum !== undefined && value < definition.minimum) {
563
- return false;
564
- }
565
- if (definition.maximum !== undefined && value > definition.maximum) {
566
- return false;
567
- }
568
- if (definition.minLength !== undefined && value.length < definition.minLength) {
569
- return false;
570
- }
571
- if (definition.maxLength !== undefined && value.length > definition.maxLength) {
572
- return false;
573
- }
574
- if (definition.pattern && !new RegExp(definition.pattern).test(value)) {
575
- return false;
576
- }
577
-
578
- return true;
579
- };
580
-
581
- validators[key] = validator;
582
- }
583
-
584
- return validators;
585
- }
586
-
587
506
  /**
588
507
  * Creates a writable stream that collects JSON objects into an array
589
508
  *
@@ -611,12 +530,13 @@ module.exports = {
611
530
  createCsvToJsonStream,
612
531
  streamCsvToJson,
613
532
  createCsvFileToJsonStream,
614
- createJsonCollectorStream,
615
- createSchemaValidators
533
+ createJsonCollectorStream
534
+ // Note: createSchemaValidators is no longer exported from here
535
+ // It should be imported directly from './src/utils/schema-validator'
616
536
  };
617
537
 
618
538
  // For ES6 module compatibility
619
539
  /* istanbul ignore next */
620
540
  if (typeof module !== 'undefined' && module.exports) {
621
541
  module.exports.default = createCsvToJsonStream;
622
- }
542
+ }
@@ -18,6 +18,9 @@ const {
18
18
  const { Transform, Readable, Writable } = require('stream');
19
19
  const { pipeline } = require('stream/promises');
20
20
 
21
+ // Import schema validator from utils
22
+ const { createSchemaValidators } = require('./src/utils/schema-validator');
23
+
21
24
  /**
22
25
  * Creates a transform stream that converts JSON objects to CSV rows
23
26
  *
@@ -291,90 +294,6 @@ function createJsonToCsvStream(options = {}) {
291
294
  }, 'STREAM_CREATION_ERROR', { function: 'createJsonToCsvStream' });
292
295
  }
293
296
 
294
- /**
295
- * Creates schema validators from JSON schema
296
- *
297
- * @private
298
- * @param {Object} schema - JSON schema
299
- * @returns {Object} Validators object
300
- */
301
- function createSchemaValidators(schema) {
302
- const validators = {};
303
-
304
- if (!schema.properties) {
305
- return validators;
306
- }
307
-
308
- for (const [key, definition] of Object.entries(schema.properties)) {
309
- const validator = {
310
- type: definition.type,
311
- required: schema.required && schema.required.includes(key)
312
- };
313
-
314
- // Add format function for dates
315
- if (definition.type === 'string' && definition.format === 'date-time') {
316
- validator.format = (value) => {
317
- if (value instanceof Date) {
318
- return value.toISOString();
319
- }
320
- /* istanbul ignore next */
321
- if (typeof value === 'string') {
322
- // Try to parse as date
323
- const date = new Date(value);
324
- if (!isNaN(date.getTime())) {
325
- return date.toISOString();
326
- }
327
- }
328
- return value;
329
- };
330
- }
331
-
332
- // Add validation function
333
- validator.validate = (value) => {
334
- if (value === null || value === undefined) {
335
- return !validator.required;
336
- }
337
-
338
- // Type validation
339
- if (definition.type === 'string' && typeof value !== 'string') {
340
- return false;
341
- }
342
- if (definition.type === 'number' && typeof value !== 'number') {
343
- return false;
344
- }
345
- if (definition.type === 'integer' && (!Number.isInteger(value) || typeof value !== 'number')) {
346
- return false;
347
- }
348
- if (definition.type === 'boolean' && typeof value !== 'boolean') {
349
- return false;
350
- }
351
-
352
- // Additional constraints
353
- if (definition.minimum !== undefined && value < definition.minimum) {
354
- return false;
355
- }
356
- if (definition.maximum !== undefined && value > definition.maximum) {
357
- return false;
358
- }
359
- if (definition.minLength !== undefined && value.length < definition.minLength) {
360
- return false;
361
- }
362
- if (definition.maxLength !== undefined && value.length > definition.maxLength) {
363
- return false;
364
- }
365
- if (definition.pattern && !new RegExp(definition.pattern).test(value)) {
366
- return false;
367
- }
368
-
369
- return true;
370
- };
371
-
372
- validators[key] = validator;
373
- }
374
-
375
- return validators;
376
- }
377
-
378
297
  /**
379
298
  * Converts a readable stream of JSON objects to CSV and writes to a writable stream
380
299
  *
@@ -533,12 +452,13 @@ module.exports = {
533
452
  streamJsonToCsv,
534
453
  saveJsonStreamAsCsv,
535
454
  createJsonReadableStream,
536
- createCsvCollectorStream,
537
- createSchemaValidators
455
+ createCsvCollectorStream
456
+ // Note: createSchemaValidators is no longer exported from here
457
+ // It should be imported directly from './src/utils/schema-validator'
538
458
  };
539
459
 
540
460
  // For ES6 module compatibility
541
461
  /* istanbul ignore next */
542
462
  if (typeof module !== 'undefined' && module.exports) {
543
463
  module.exports.default = createJsonToCsvStream;
544
- }
464
+ }