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: nested ctype #912

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
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
9 changes: 7 additions & 2 deletions packages/credentials/src/V1/KiltCredentialV1.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,13 @@ it('exports to VC including ctype as schema', async () => {
expect(() => validateStructure(VC)).not.toThrow()
})

it('it verifies valid claim against schema', async () => {
await expect(validateSubject(VC, { cTypes: [cType] })).resolves.not.toThrow()
it('it verifies valid claim against nested schema', async () => {
await expect(
validateSubject(VC, {
cTypes: [cType],
nestedCType: cType
})
).resolves.not.toThrow()
})

it('it detects schema violations', async () => {
Expand Down
24 changes: 20 additions & 4 deletions packages/credentials/src/V1/KiltCredentialV1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,7 @@ const cachingCTypeLoader = newCachingCTypeLoader()
* @param options Options map.
* @param options.cTypes One or more CType definitions to be used for validation. If `loadCTypes` is set to `false`, validation will fail if the definition of the credential's CType is not given.
* @param options.loadCTypes A function to load CType definitions that are not in `cTypes`. Defaults to using the {@link newCachingCTypeLoader | CachingCTypeLoader}. If set to `false` or `undefined`, no additional CTypes will be loaded.
* @param options.nestedCType - If provided, validates using nested CType validation with this CType.
*/
export async function validateSubject(
{
Expand All @@ -345,7 +346,12 @@ export async function validateSubject(
{
cTypes = [],
loadCTypes = cachingCTypeLoader,
}: { cTypes?: ICType[]; loadCTypes?: false | CTypeLoader } = {}
nestedCType = null, // Changed to ICType | null with null default
}: {
cTypes?: ICType[];
loadCTypes?: false | CTypeLoader;
nestedCType?: ICType | null; // Type definition updated
} = {}
): Promise<void> {
// get CType id referenced in credential
const credentialsCTypeId = type.find((str) =>
Expand Down Expand Up @@ -385,6 +391,16 @@ export async function validateSubject(
[key.substring(vocab.length)]: value,
}
}, {})
// validates against CType (also validates CType schema itself)
CType.verifyClaimAgainstSchema(claims, cType)
}

// Check if nestedCType is provided and perform appropriate validation
if (nestedCType) {
await CType.verifyClaimAgainstNestedSchemas(
nestedCType,
cTypes,
claims
)
} else {
// Regular validation
await CType.verifyClaimAgainstSchema(claims, cType)
}
}
Loading