@scalar/json-schema-validator 0.1.1 → 0.1.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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @scalar/json-schema-validator
2
2
 
3
+ ## 0.1.2
4
+
5
+ ### Patch Changes
6
+
7
+ - [#10202](https://github.com/scalar/scalar/pull/10202): Explain invalid URI references and OpenAPI component names, and return parser errors for malformed reference escapes.
8
+
3
9
  ## 0.1.1
4
10
 
5
11
  ## 0.1.0
@@ -28,6 +28,8 @@ export type SpecificationValidatorConfig<TVersion extends string, TOptions exten
28
28
  /** The document's version is not supported. */
29
29
  versionNotSupported: string;
30
30
  };
31
+ /** Adds specification-specific guidance to schema errors before returning or throwing them. */
32
+ transformErrors?: (errors: ErrorObject[], specification: AnyObject) => ErrorObject[];
31
33
  /**
32
34
  * Adjusts the document before schema validation without touching the caller's
33
35
  * copy (AsyncAPI, for example, pins `asyncapi` to an exact version). The
@@ -1 +1 @@
1
- {"version":3,"file":"create-specification-validator.d.ts","sourceRoot":"","sources":["../src/create-specification-validator.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,SAAS,EAAE,MAAM,qBAAqB,CAAA;AAGpD,OAAO,KAAK,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,SAAS,CAAA;AAG7D,KAAK,YAAY,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;AAE3C,qDAAqD;AACrD,MAAM,MAAM,6BAA6B,GAAG;IAC1C;;;;OAIG;IACH,YAAY,CAAC,EAAE,OAAO,CAAA;CACvB,CAAA;AAED;;;GAGG;AACH,MAAM,MAAM,4BAA4B,CAAC,QAAQ,SAAS,MAAM,EAAE,QAAQ,SAAS,6BAA6B,IAAI;IAClH,kDAAkD;IAClD,OAAO,EAAE,MAAM,CAAC,QAAQ,EAAE,YAAY,CAAC,CAAA;IACvC,gEAAgE;IAChE,aAAa,EAA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false, errors: [{ message: config.errors.versionNotSupported }] };
57
57
  }
58
58
  const documentToValidate = config.prepareDocument?.(specification, version) ?? specification;
59
- const result = getValidator(version)(documentToValidate, options);
59
+ const result = getValidator(version)(documentToValidate);
60
60
  if (!result.valid) {
61
- return { valid: false, version, errors: result.errors };
61
+ const errors = config.transformErrors?.(result.errors, documentToValidate) ?? result.errors;
62
+ if (options?.throwOnError) {
63
+ throw new Error(errors[0]?.message ?? 'Validation failed');
64
+ }
65
+ return { valid: false, version, errors };
62
66
  }
63
67
  const semanticErrors = config.postValidate?.(specification, version, options) ?? [];
64
68
  if (semanticErrors.length > 0) {
@@ -1 +1 @@
1
- {"version":3,"file":"prettify-ajv-errors.d.ts","sourceRoot":"","sources":["../src/prettify-ajv-errors.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH;;;;;GAKG;AACH,MAAM,MAAM,QAAQ,GAAG;IACrB,OAAO,EAAE,MAAM,CAAA;IACf,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,yDAAyD;IACzD,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;CAC7B,CAAA;AAED,qDAAqD;AACrD,MAAM,MAAM,WAAW,GAAG;IACxB,OAAO,EAAE,MAAM,CAAA;IACf,IAAI,CAAC,EAAE,MAAM,CAAA;CACd,CAAA;AAyXD;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,WAAW,EAAE,CAKtF"}
1
+ {"version":3,"file":"prettify-ajv-errors.d.ts","sourceRoot":"","sources":["../src/prettify-ajv-errors.ts"],"names":[],"mappings":"AAAA;;;;;;;GAOG;AAEH;;;;;GAKG;AACH,MAAM,MAAM,QAAQ,GAAG;IACrB,OAAO,EAAE,MAAM,CAAA;IACf,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,yDAAyD;IACzD,QAAQ,CAAC,EAAE,MAAM,CAAA;IACjB,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB,MAAM,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,GAAG,CAAC,CAAA;CAC7B,CAAA;AAED,qDAAqD;AACrD,MAAM,MAAM,WAAW,GAAG;IACxB,OAAO,EAAE,MAAM,CAAA;IACf,IAAI,CAAC,EAAE,MAAM,CAAA;CACd,CAAA;AAiYD;;GAEG;AACH,wBAAgB,iBAAiB,CAAC,QAAQ,EAAE,OAAO,EAAE,MAAM,EAAE,QAAQ,EAAE,GAAG,WAAW,EAAE,CAKtF"}
@@ -109,20 +109,26 @@ function formatFormatError(error, document) {
109
109
  }
110
110
  return { message: `${error.keyword} ${error.message}`, path };
111
111
  }
112
- function uriReferenceMessage(document, path) {
112
+ const uriReferenceMessage = (document, path) => {
113
113
  const refValue = extractRefValue(document, path);
114
114
  if (refValue && /[^\x00-\x7F]/.test(refValue)) {
115
- return `$ref "${refValue}" contains non-ASCII characters`;
115
+ return `$ref "${refValue}" contains non-ASCII characters. Percent-encode them using UTF-8 (RFC 3986).`;
116
+ }
117
+ if (refValue && /%(?![0-9a-fA-F]{2})/.test(refValue)) {
118
+ return `$ref "${refValue}" contains invalid percent-encoding. Each "%" must be followed by two hexadecimal digits; encode a literal "%" as "%25".`;
119
+ }
120
+ if (refValue && /\s/.test(refValue)) {
121
+ return `$ref "${refValue}" contains whitespace. Remove it or percent-encode it (for example, use "%20" for a space).`;
116
122
  }
117
123
  if (refValue) {
118
124
  return `$ref "${refValue}" is not a valid URI reference`;
119
125
  }
120
126
  return '$ref is not a valid URI reference';
121
- }
127
+ };
122
128
  /** Decodes the `~1` and `~0` escapes of a single JSON Pointer segment. */
123
129
  const unescapePointerSegment = (segment) => segment.replace(/~1/g, '/').replace(/~0/g, '~');
124
130
  /** Splits a JSON Pointer into its decoded segments. */
125
- const pointerSegments = (path) => path.split('/').filter(Boolean).map(unescapePointerSegment);
131
+ const pointerSegments = (path) => path.split('/').slice(1).map(unescapePointerSegment);
126
132
  /**
127
133
  * Walks the document along a list of JSON Pointer segments.
128
134
  *
package/package.json CHANGED
@@ -17,7 +17,7 @@
17
17
  "validator",
18
18
  "typescript"
19
19
  ],
20
- "version": "0.1.1",
20
+ "version": "0.1.2",
21
21
  "engines": {
22
22
  "node": ">=22"
23
23
  },
@@ -42,8 +42,8 @@
42
42
  "ajv-draft-04": "^1.0.0",
43
43
  "ajv-formats": "^3.0.1",
44
44
  "yaml": "^2.9.0",
45
- "@scalar/types": "0.19.0",
46
- "@scalar/helpers": "0.11.3"
45
+ "@scalar/helpers": "0.12.0",
46
+ "@scalar/types": "0.20.0"
47
47
  },
48
48
  "devDependencies": {
49
49
  "@types/node": "^24.1.0",