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

fix: enhance error handling in context validation #219

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 42 additions & 36 deletions packages/untp-playground/src/lib/contextValidation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,43 +31,49 @@ export async function validateContext(credential: Record<string, any>): Promise<

return { valid: true, data: expanded };
} catch (error: any) {
const checkSyntaxErrorResult = checkSyntaxError(error);
if (!checkSyntaxErrorResult.valid) {
return {
valid: false,
error: {
keyword: 'conflictingProperties',
message: checkSyntaxErrorResult.errorMessage as string,
instancePath: '@context',
params: {
conflictingProperty: checkSyntaxErrorResult.term,
}
},
console.log('Expand JSON-LD error:', error);

try {
const checkSyntaxErrorResult = checkSyntaxError(error);
if (!checkSyntaxErrorResult.valid) {
return {
valid: false,
error: {
keyword: 'conflictingProperties',
message: checkSyntaxErrorResult.errorMessage as string,
instancePath: '@context',
params: {
conflictingProperty: checkSyntaxErrorResult.term,
}
},
}
}
}

const checkInvalidContextResult = checkInvalidContext(error);
if (!checkInvalidContextResult.valid) {
return {
valid: false,
error: {
keyword: 'const',
message: checkInvalidContextResult.errorMessage as string,
instancePath: '@context',
},
};
}

const checkInvalidPropertiesResult = await checkInvalidProperties(error, credential);
if (!checkInvalidPropertiesResult.valid) {
return {
valid: false,
error: {
keyword: 'const',
message: checkInvalidPropertiesResult.errorMessage as string,
instancePath: checkInvalidPropertiesResult.invalidValues as string,
},

const checkInvalidContextResult = checkInvalidContext(error);
if (!checkInvalidContextResult.valid) {
return {
valid: false,
error: {
keyword: 'const',
message: checkInvalidContextResult.errorMessage as string,
instancePath: '@context',
},
};
}

const checkInvalidPropertiesResult = await checkInvalidProperties(error, credential);
if (!checkInvalidPropertiesResult.valid) {
return {
valid: false,
error: {
keyword: 'const',
message: checkInvalidPropertiesResult.errorMessage as string,
instancePath: checkInvalidPropertiesResult.invalidValues as string,
},
}
}
} catch (error) {
console.log('Context validation error:', error);
}

return {
Expand Down Expand Up @@ -163,7 +169,7 @@ export function getDroppedProperties(originalObject: Record<string, any>, compac
// If key does not exist in objB, record it
if (!(key in objectB)) {
uniqueKeys.push(currentPath.join('/'));
} else if (typeof objectA[key] === 'object' && objectA[key] !== null) {
} else if (typeof objectA[key] === 'object' && objectA[key] !== null && !Array.isArray(objectA[key])) {
// Recursively search for unique keys
findUniqueKeys(objectA[key], objectB[key], currentPath);
}
Expand Down
Loading