@amritk/generate-parsers 0.7.1 → 0.7.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.
@@ -1 +1 @@
1
- {"version":3,"file":"generate-parser-function.d.ts","sourceRoot":"","sources":["../../src/generators/generate-parser-function.ts"],"names":[],"mappings":"AA4BA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAA;AAMjE;;GAEG;AACH,KAAK,qBAAqB,GAAG;IAC3B;;;;OAIG;IACH,QAAQ,CAAC,aAAa,CAAC,EAAE,OAAO,CAAA;IAChC;;;OAGG;IACH,QAAQ,CAAC,WAAW,CAAC,EAAE,OAAO,CAAA;IAC9B;;;;OAIG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE,OAAO,CAAA;IACzB;;;OAGG;IACH,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAA;CAC7B,CAAA;AAqqCD;;;;;;;;GAQG;AACH,eAAO,MAAM,sBAAsB,WACzB,UAAU,YACR,MAAM,YACN,qBAAqB,KAC9B,MAEF,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,0BAA0B,aAAc,MAAM,KAAG,MAAsC,CAAA;AAEpG;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,sBAAsB,WACzB,UAAU,YACR,MAAM,iBACD,OAAO,sBAErB,MAyDF,CAAA"}
1
+ {"version":3,"file":"generate-parser-function.d.ts","sourceRoot":"","sources":["../../src/generators/generate-parser-function.ts"],"names":[],"mappings":"AA6BA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAA;AAMjE;;GAEG;AACH,KAAK,qBAAqB,GAAG;IAC3B;;;;OAIG;IACH,QAAQ,CAAC,aAAa,CAAC,EAAE,OAAO,CAAA;IAChC;;;OAGG;IACH,QAAQ,CAAC,WAAW,CAAC,EAAE,OAAO,CAAA;IAC9B;;;;OAIG;IACH,QAAQ,CAAC,MAAM,CAAC,EAAE,OAAO,CAAA;IACzB;;;OAGG;IACH,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAA;CAC7B,CAAA;AA4pCD;;;;;;;;GAQG;AACH,eAAO,MAAM,sBAAsB,WACzB,UAAU,YACR,MAAM,YACN,qBAAqB,KAC9B,MAEF,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,0BAA0B,aAAc,MAAM,KAAG,MAAsC,CAAA;AAEpG;;;;;;;;;;;;GAYG;AACH,eAAO,MAAM,sBAAsB,WACzB,UAAU,YACR,MAAM,iBACD,OAAO,sBAErB,MAyDF,CAAA"}
@@ -1,3 +1,4 @@
1
+ import { escapeRegexPattern } from '@amritk/helpers/escape-regex-pattern';
1
2
  import { refToName } from '@amritk/helpers/ref-to-name';
2
3
  import { safeAccessor, safeKey } from '@amritk/helpers/safe-accessor';
3
4
  import { hasAllOf, hasAnyOf, hasConst, hasDefault, hasEnum, hasExamples, hasExclusiveMaximum, hasExclusiveMinimum, hasItems, hasMaxItems, hasMaximum, hasMaxLength, hasMinItems, hasMinimum, hasMinLength, hasMultipleOf, hasOneOf, hasPattern, hasProperties, hasRef, hasType, hasUniqueItems, isObjectSchema, isSchemaObject, } from '@amritk/helpers/schema-guards';
@@ -742,14 +743,6 @@ const generateInlineValueParser = (schema) => {
742
743
  return null;
743
744
  }
744
745
  };
745
- /**
746
- * Escapes special regex characters in a pattern string for use in a regex literal.
747
- * This escapes backslashes and forward slashes for safe inclusion in /pattern/ syntax.
748
- */
749
- const escapeRegexPattern = (pattern) => {
750
- // Escape backslashes first, then forward slashes
751
- return pattern.replace(/\\/g, '\\\\').replace(/\//g, '\\/');
752
- };
753
746
  /**
754
747
  * Generates a parser for schemas with patternProperties.
755
748
  * Handles both patternProperties and specification-extensions (x- prefix).
@@ -1 +1 @@
1
- {"version":3,"file":"generate-schema-checks.d.ts","sourceRoot":"","sources":["../../src/generators/generate-schema-checks.ts"],"names":[],"mappings":"AAqBA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAA;AAIjE;;GAEG;AACH,eAAO,MAAM,oBAAoB,aAAc,MAAM,UAAU,UAAU,KAAG,MAAM,EAkGjF,CAAA"}
1
+ {"version":3,"file":"generate-schema-checks.d.ts","sourceRoot":"","sources":["../../src/generators/generate-schema-checks.ts"],"names":[],"mappings":"AAsBA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAA;AAIjE;;GAEG;AACH,eAAO,MAAM,oBAAoB,aAAc,MAAM,UAAU,UAAU,KAAG,MAAM,EAkGjF,CAAA"}
@@ -1,3 +1,4 @@
1
+ import { escapeRegexPattern } from '@amritk/helpers/escape-regex-pattern';
1
2
  import { getMjstInstanceOf, getMjstPrimitive } from '@amritk/helpers/mjst-extension';
2
3
  import { hasAdditionalProperties, hasEnum, hasExclusiveMaximum, hasExclusiveMinimum, hasMaxItems, hasMaximum, hasMaxLength, hasMaxProperties, hasMinItems, hasMinimum, hasMinLength, hasMinProperties, hasMultipleOf, hasPattern, hasProperties, hasRequired, hasType, hasUniqueItems, } from '@amritk/helpers/schema-guards';
3
4
  import { generateEnumCheck } from './generate-enum-check.js';
@@ -23,7 +24,7 @@ export const generateSchemaChecks = (accessor, schema) => {
23
24
  case 'string': {
24
25
  checks.push(`typeof ${accessor} === "string"`);
25
26
  if (hasPattern(schema)) {
26
- checks.push(`/${schema.pattern.replace(/\//g, '\\/')}/.test(${accessor})`);
27
+ checks.push(`/${escapeRegexPattern(schema.pattern)}/.test(${accessor})`);
27
28
  }
28
29
  if (hasMinLength(schema)) {
29
30
  checks.push(`${accessor}.length >= ${schema.minLength}`);
@@ -1 +1 @@
1
- {"version":3,"file":"generate-strict-assertion.d.ts","sourceRoot":"","sources":["../../src/generators/generate-strict-assertion.ts"],"names":[],"mappings":"AAkBA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAA;AAsLjE;;;;;;;;;;GAUG;AACH,eAAO,MAAM,6BAA6B,WAAY,UAAU,YAAY,MAAM,KAAG,MAAM,EAe1F,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,6BAA6B,WAAY,UAAU,YAAY,MAAM,KAAG,MAAM,GAAG,IAgB7F,CAAA"}
1
+ {"version":3,"file":"generate-strict-assertion.d.ts","sourceRoot":"","sources":["../../src/generators/generate-strict-assertion.ts"],"names":[],"mappings":"AAmBA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAA;AA+KjE;;;;;;;;;;GAUG;AACH,eAAO,MAAM,6BAA6B,WAAY,UAAU,YAAY,MAAM,KAAG,MAAM,EAe1F,CAAA;AAED;;;GAGG;AACH,eAAO,MAAM,6BAA6B,WAAY,UAAU,YAAY,MAAM,KAAG,MAAM,GAAG,IAgB7F,CAAA"}
@@ -1,3 +1,4 @@
1
+ import { escapeRegexPattern } from '@amritk/helpers/escape-regex-pattern';
1
2
  import { getMjstInstanceOf, getMjstPrimitive } from '@amritk/helpers/mjst-extension';
2
3
  import { safeAccessor } from '@amritk/helpers/safe-accessor';
3
4
  import { hasEnum, hasExclusiveMaximum, hasExclusiveMinimum, hasMaximum, hasMaxLength, hasMinimum, hasMinLength, hasMultipleOf, hasPattern, hasProperties, hasRef, hasRequired, hasType, isSchemaObject, } from '@amritk/helpers/schema-guards';
@@ -27,12 +28,6 @@ const wrongTypeCondition = (accessor, type) => {
27
28
  * `integer` collapses to `number` since both are validated via `typeof === "number"`.
28
29
  */
29
30
  const typeLabel = (type) => (type === 'integer' ? 'number' : type);
30
- /**
31
- * Escapes special regex characters in a pattern string for use in a regex literal.
32
- */
33
- const escapeRegexPattern = (pattern) => {
34
- return pattern.replace(/\\/g, '\\\\').replace(/\//g, '\\/');
35
- };
36
31
  /**
37
32
  * Generates strict-mode constraint checks for a typed property
38
33
  * (pattern, length, min/max, multipleOf).
@@ -1 +1 @@
1
- {"version":3,"file":"generate-validation-expression.d.ts","sourceRoot":"","sources":["../../src/generators/generate-validation-expression.ts"],"names":[],"mappings":"AA6BA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAA;AAgDjE;;;;GAIG;AACH,eAAO,MAAM,4BAA4B,QAClC,MAAM,UACH,UAAU,gBACJ,MAAM,cACR,OAAO,eACN,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,gBACtB,GAAG,CAAC,MAAM,CAAC,qBACN,MAAM,sBACL,OAAO,KAC1B,MA0OF,CAAA"}
1
+ {"version":3,"file":"generate-validation-expression.d.ts","sourceRoot":"","sources":["../../src/generators/generate-validation-expression.ts"],"names":[],"mappings":"AA8BA,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,iCAAiC,CAAA;AAgDjE;;;;GAIG;AACH,eAAO,MAAM,4BAA4B,QAClC,MAAM,UACH,UAAU,gBACJ,MAAM,cACR,OAAO,eACN,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,gBACtB,GAAG,CAAC,MAAM,CAAC,qBACN,MAAM,sBACL,OAAO,KAC1B,MA0OF,CAAA"}
@@ -1,3 +1,4 @@
1
+ import { escapeRegexPattern } from '@amritk/helpers/escape-regex-pattern';
1
2
  import { getMjstInstanceOf, getMjstPrimitive } from '@amritk/helpers/mjst-extension';
2
3
  import { resolveRef } from '@amritk/helpers/resolve-ref';
3
4
  import { safeAccessor } from '@amritk/helpers/safe-accessor';
@@ -153,7 +154,7 @@ export const generateValidationExpression = (key, schema, defaultValue, isRequir
153
154
  case 'string': {
154
155
  checks.push(`typeof ${accessor} === "string"`);
155
156
  if (hasPattern(schema)) {
156
- checks.push(`/${schema.pattern.replace(/\//g, '\\/')}/.test(${accessor})`);
157
+ checks.push(`/${escapeRegexPattern(schema.pattern)}/.test(${accessor})`);
157
158
  }
158
159
  if (hasMinLength(schema)) {
159
160
  checks.push(`${accessor}.length >= ${schema.minLength}`);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@amritk/generate-parsers",
3
- "version": "0.7.1",
3
+ "version": "0.7.2",
4
4
  "description": "Generate TypeScript parsers and type definitions from JSON Schemas.",
5
5
  "module": "./dist/index.js",
6
6
  "type": "module",
@@ -49,7 +49,7 @@
49
49
  },
50
50
  "dependencies": {
51
51
  "json-schema-typed": "^8.0.1",
52
- "@amritk/helpers": "0.7.1",
52
+ "@amritk/helpers": "0.8.0",
53
53
  "@amritk/generate-markdown": "0.2.4"
54
54
  }
55
55
  }
@@ -1,3 +1,4 @@
1
+ import { escapeRegexPattern } from '@amritk/helpers/escape-regex-pattern'
1
2
  import { refToName } from '@amritk/helpers/ref-to-name'
2
3
  import { safeAccessor, safeKey } from '@amritk/helpers/safe-accessor'
3
4
  import {
@@ -947,15 +948,6 @@ const generateInlineValueParser = (schema: JSONSchema): string | null => {
947
948
  }
948
949
  }
949
950
 
950
- /**
951
- * Escapes special regex characters in a pattern string for use in a regex literal.
952
- * This escapes backslashes and forward slashes for safe inclusion in /pattern/ syntax.
953
- */
954
- const escapeRegexPattern = (pattern: string): string => {
955
- // Escape backslashes first, then forward slashes
956
- return pattern.replace(/\\/g, '\\\\').replace(/\//g, '\\/')
957
- }
958
-
959
951
  /**
960
952
  * Generates a parser for schemas with patternProperties.
961
953
  * Handles both patternProperties and specification-extensions (x- prefix).
@@ -1,3 +1,4 @@
1
+ import { escapeRegexPattern } from '@amritk/helpers/escape-regex-pattern'
1
2
  import { getMjstInstanceOf, getMjstPrimitive } from '@amritk/helpers/mjst-extension'
2
3
  import {
3
4
  hasAdditionalProperties,
@@ -49,7 +50,7 @@ export const generateSchemaChecks = (accessor: string, schema: JSONSchema): stri
49
50
  case 'string': {
50
51
  checks.push(`typeof ${accessor} === "string"`)
51
52
  if (hasPattern(schema)) {
52
- checks.push(`/${schema.pattern.replace(/\//g, '\\/')}/.test(${accessor})`)
53
+ checks.push(`/${escapeRegexPattern(schema.pattern)}/.test(${accessor})`)
53
54
  }
54
55
  if (hasMinLength(schema)) {
55
56
  checks.push(`${accessor}.length >= ${schema.minLength}`)
@@ -1,3 +1,4 @@
1
+ import { escapeRegexPattern } from '@amritk/helpers/escape-regex-pattern'
1
2
  import { getMjstInstanceOf, getMjstPrimitive } from '@amritk/helpers/mjst-extension'
2
3
  import { safeAccessor } from '@amritk/helpers/safe-accessor'
3
4
  import {
@@ -46,13 +47,6 @@ const wrongTypeCondition = (accessor: string, type: string): string | null => {
46
47
  */
47
48
  const typeLabel = (type: string): string => (type === 'integer' ? 'number' : type)
48
49
 
49
- /**
50
- * Escapes special regex characters in a pattern string for use in a regex literal.
51
- */
52
- const escapeRegexPattern = (pattern: string): string => {
53
- return pattern.replace(/\\/g, '\\\\').replace(/\//g, '\\/')
54
- }
55
-
56
50
  /**
57
51
  * Generates strict-mode constraint checks for a typed property
58
52
  * (pattern, length, min/max, multipleOf).
@@ -1,3 +1,4 @@
1
+ import { escapeRegexPattern } from '@amritk/helpers/escape-regex-pattern'
1
2
  import { getMjstInstanceOf, getMjstPrimitive } from '@amritk/helpers/mjst-extension'
2
3
  import { resolveRef } from '@amritk/helpers/resolve-ref'
3
4
  import { safeAccessor } from '@amritk/helpers/safe-accessor'
@@ -208,7 +209,7 @@ export const generateValidationExpression = (
208
209
  case 'string': {
209
210
  checks.push(`typeof ${accessor} === "string"`)
210
211
  if (hasPattern(schema)) {
211
- checks.push(`/${schema.pattern.replace(/\//g, '\\/')}/.test(${accessor})`)
212
+ checks.push(`/${escapeRegexPattern(schema.pattern)}/.test(${accessor})`)
212
213
  }
213
214
  if (hasMinLength(schema)) {
214
215
  checks.push(`${accessor}.length >= ${schema.minLength}`)