-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #113 from govuk-one-login/adding-schema-example
Adding example using ajv and vocab schema
- Loading branch information
Showing
8 changed files
with
267 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
dist/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
Example Node.js project consuming JSON schemas | ||
================================================== | ||
|
||
This Node.js project consumes the [Digital Identity Vocab](https://github.com/govuk-one-login/data-vocab) TypeScript types. | ||
|
||
In the `package.json`, note the `@govuk-one-login/data-vocab-schemas` dependency: | ||
|
||
```json | ||
"@govuk-one-login/data-vocab-schemas": "1.7.2" | ||
``` | ||
|
||
> **Note** | ||
> See [releases](https://github.com/govuk-one-login/data-vocab/releases) for the latest version. | ||
### Build the project | ||
|
||
##### Prerequisites | ||
|
||
Ensure you have authenticated to the `@govuk-one-login` npm scope in GitHub Packages and selected `write:packages` scope when creating personal token: | ||
|
||
```shell | ||
npm login --scope=@govuk-one-login --auth-type=legacy --registry=https://npm.pkg.github.com | ||
``` | ||
|
||
> See [Authenticating to GitHub Packages](https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-npm-registry#authenticating-to-github-packages) documentation. | ||
> **Note** | ||
> For instructions on using the package from GitHub Actions, see the [main README](https://github.com/govuk-one-login/data-vocab#setting-permissions). | ||
##### Steps | ||
|
||
Install dependencies: | ||
|
||
```shell | ||
npm ci | ||
``` | ||
|
||
Build the project using: | ||
|
||
```shell | ||
npm run build | ||
``` | ||
|
||
This runs the TypeScript transpiler and outputs to the `dist` directory. | ||
|
||
### Run the project | ||
|
||
Once you have built the project, run it with: | ||
|
||
```shell | ||
npm start | ||
``` | ||
|
||
You should see message `Payload complies with the schema` if the payload complies with the provided schema or error object if it doesn't. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"name": "schemas-typescript", | ||
"version": "0.1.0", | ||
"description": "Example project that consumes DI Vocab schemas.", | ||
"main": "dist/index.js", | ||
"scripts": { | ||
"build": "tsc", | ||
"prestart": "npm run build", | ||
"start": "node dist/index.js", | ||
"test": "echo \"Error: no test specified\" && exit 1" | ||
}, | ||
"license": "ISC", | ||
"dependencies": { | ||
"@govuk-one-login/data-vocab-schemas": "1.7.2", | ||
"ajv": "^8.16.0", | ||
"ajv-formats": "^3.0.1" | ||
}, | ||
"devDependencies": { | ||
"typescript": "^5.2.2" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import | ||
IdentityCheckCredentialJWT | ||
from "@govuk-one-login/data-vocab-schemas/src/schemas/IdentityCheckCredentialJWT.json"; | ||
import Ajv from "ajv"; | ||
import addFormats from "ajv-formats" | ||
|
||
const credentials = { | ||
sub: "urn:fdc:gov.uk:2022:954bc117-731b-41cd-86cf-dfb4e7940fce", | ||
aud: "https://passport.core.stubs.account.gov.uk", | ||
nbf: 1690816091, | ||
iss: "https://review-p.build.account.gov.uk", | ||
vc: { | ||
evidence: [{ | ||
validityScore: 0, | ||
strengthScore: 4, | ||
ci: [ | ||
"D02" | ||
], | ||
txn: "5f57a8f2-62b0-4958-9332-06d9f453e5b9", | ||
type: "IdentityCheck" | ||
}], | ||
credentialSubject: { | ||
passport: [{ | ||
expiryDate: "2030-12-12", | ||
icaoIssuerCode: "GBR", | ||
documentNumber: "123456789" | ||
}], | ||
name: [{ | ||
nameParts: [{ | ||
type: "GivenName", | ||
value: "Kenneth" | ||
}, | ||
{ | ||
type: "FamilyName", | ||
value: "Decerqueira" | ||
} | ||
] | ||
}], | ||
birthDate: [{ | ||
value: "1990-01-23" | ||
}] | ||
}, | ||
type: [ | ||
"VerifiableCredential", | ||
"IdentityCheckCredential" | ||
] | ||
} | ||
} | ||
|
||
const ajv = new Ajv({ allErrors: true }); | ||
addFormats(ajv) | ||
const rulesValidator = ajv.addSchema(IdentityCheckCredentialJWT).compile(IdentityCheckCredentialJWT); | ||
if (rulesValidator(credentials)) { | ||
console.log("Payload complies with the schema") | ||
} | ||
else { | ||
console.log("Errors"+ JSON.stringify(rulesValidator.errors)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
{ | ||
"compilerOptions": { | ||
/* Language and Environment */ | ||
"target": "es2016", /* Set the JavaScript language version for emitted JavaScript and include compatible library declarations. */ | ||
|
||
/* Modules */ | ||
"module": "commonjs", /* Specify what module code is generated. */ | ||
"outDir": "./dist", /* Specify an output folder for all emitted files. */ | ||
|
||
/* Interop Constraints */ | ||
"esModuleInterop": true, /* Emit additional JavaScript to ease support for importing CommonJS modules. This enables 'allowSyntheticDefaultImports' for type compatibility. */ | ||
"forceConsistentCasingInFileNames": true, /* Ensure that casing is correct in imports. */ | ||
|
||
/* Type Checking */ | ||
"strict": true, /* Enable all strict type-checking options. */ | ||
|
||
/* Completeness */ | ||
"skipLibCheck": true, /* Skip type checking all .d.ts files. */ | ||
"resolveJsonModule": true | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters