Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

urnSerialize Fails Unexpectedly #2503

Open
pyramid-johng opened this issue Oct 30, 2024 · 1 comment
Open

urnSerialize Fails Unexpectedly #2503

pyramid-johng opened this issue Oct 30, 2024 · 1 comment

Comments

@pyramid-johng
Copy link

What version of Ajv are you using? Does the issue happen if you use the latest version?
Using version 8.12.0 up to 8.16.0 works, latest version fails.

Uncaught (in promise) TypeError: Cannot read properties of undefined (reading 'toLowerCase')
at Object.urnSerialize [as serialize] (ajv_dist_2020.js?v=7ea982fe:3411:37)
at Object.serialize (ajv_dist_2020.js?v=7ea982fe:3592:67)
at _getFullPath (ajv_dist_2020.js?v=7ea982fe:2215:35)
at getFullPath (ajv_dist_2020.js?v=7ea982fe:2211:14)
at Ajv2020.getSchemaRefs (ajv_dist_2020.js?v=7ea982fe:2236:26)
at Ajv2020._addSchema (ajv_dist_2020.js?v=7ea982fe:4195:51)
at Ajv2020.addSchema (ajv_dist_2020.js?v=7ea982fe:3993:34)
at Ajv2020.addInitialSchemas (ajv_dist_2020.js?v=7ea982fe:4253:16)
at new Ajv (ajv_dist_2020.js?v=7ea982fe:3887:27)
at new Ajv2020 (ajv_dist_2020.js?v=7ea982fe:6903:9)

That is this line of code:

function urnSerialize(urnComponents, options) {
      const scheme = options.scheme || urnComponents.scheme || "urn";
      const nid = urnComponents.nid.toLowerCase();

Not sure what nid is, but it's undefined and the code does not validate, hence the unexpected exception. Previous versions did not call toLowerCase.

Ajv options object

const options = {
				allErrors: true,
				verbose: true,
				allowUnionTypes: true,
				schemas: {
					"/schemas/iw/rule/column/adorner": sub_columnRuleAdorner,
					"/schemas/iw/rule/column/class": sub_columnRuleClass,
					"/schemas/iw/source/command": sub_command,
					"/schemas/iw/column/computed": sub_computedColumn,
					"/schemas/iw/column/data": sub_dataColumn,
					"/schemas/iw/dateFormat": sub_dateFormat,
					"/schemas/iw/component/grid": sub_grid,
					"/schemas/iw/grid/group": sub_gridGroup,
					"/schemas/iw/component/grid/detail": sub_detailGrid,
					"/schemas/iw/source/entitySet/entityFilter": sub_entityFilter,
					"/schemas/iw/source/entitySet": sub_entitySet,
					"/schemas/iw/grid/expand": sub_expand,
					"/schemas/iw/grid/search": sub_gridSearch,
					"/schemas/iw/format": sub_format,
					"/schemas/iw/source/overlay": sub_overlay,
					"/schemas/iw/command/builtin": sub_builtinCommand,
					"/schemas/iw/command/page": sub_pageCommand,
					"/schemas/iw/command/row": sub_rowCommand,
					"/schemas/iw/rule/row/class": sub_rowRuleClass,
				},
			};

JSON Schema

{
	"$schema": "https://json-schema.org/draft/2020-12/schema",
	"$id": "urn:pyramidsolutions.com/schemas/iw/rule/column/adorner",
	"type": "object",
	"description": "Adorner rule",
	"properties": {
		"name": {
			"type": "string",
			"description": "Name of rule used in diagnostics"
		},
		"type": {
			"type": "string",
			"description": "Type of rule 'adorner'",
			"const": "adorner"
		},
		"enabled": {
			"type": "boolean",
			"description": "Evaluate rule if set to true"
		},
		"field": {
			"type": "string",
			"description": "Field name to evaluate"
		},
		"position": {
			"type": "string",
			"description": "Adorner position",
			"enum": ["before", "after"]
		},
		"function": {
			"type": "string",
			"description": "Javascript must return formatting object(s) to trigger"
		},
		"template": {
			"type": ["number", "integer", "boolean", "string", "array", "object"],
			"description": "Data passed to the script function"
		}
	},
	"required": ["name", "type", "enabled", "field", "position", "function"]
}

Not sure if it is relevant. All JSON schemas use similar techniques for $schema and $id.

Sample data

Crashes in constructor before any methods can be called.

Your code

const ajv = new Ajv2020(options);

Crashes in constructor.

Validation result, data AFTER validation, error messages


Crashes before any methods can be called.

What results did you expect?
Expect it to work the same as previous version. Expect you to check values for null or undefined before calling methods on them.

Are you going to resolve the issue?
No.

@pyramid-johng
Copy link
Author

pyramid-johng commented Oct 30, 2024

After some reading, went in and added the nid to the urn values; according to spec this field is required, but earlier versions of the library tolerated it being omitted. This tolerance should be preserved to avoid unexpected issues for users upgrading the version of this package, perhaps add an option setting to opt-in to the stricter behavior.

In any event there should be checks in place for null and undefined values; this is just good hygiene.

If the nid field is really required by the implementation, a specific actionable error message should be provided, not one from an indirect "crash". This would save time on troubleshooting by users.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

1 participant