Skip to content

Commit

Permalink
Merge pull request #113 from govuk-one-login/adding-schema-example
Browse files Browse the repository at this point in the history
Adding example using ajv and vocab schema
  • Loading branch information
richa-misra committed Jun 27, 2024
2 parents 8cf697e + 00c4bfe commit 1f7b76e
Show file tree
Hide file tree
Showing 8 changed files with 267 additions and 8 deletions.
1 change: 1 addition & 0 deletions examples/schemas-typescript/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
dist/
54 changes: 54 additions & 0 deletions examples/schemas-typescript/README.md
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.
104 changes: 104 additions & 0 deletions examples/schemas-typescript/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

21 changes: 21 additions & 0 deletions examples/schemas-typescript/package.json
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"
}
}
58 changes: 58 additions & 0 deletions examples/schemas-typescript/src/index.ts
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))
}
21 changes: 21 additions & 0 deletions examples/schemas-typescript/tsconfig.json
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
}
}
14 changes: 7 additions & 7 deletions examples/typescript-nodejs/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/typescript-nodejs/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
},
"license": "ISC",
"dependencies": {
"@govuk-one-login/data-vocab": "^1.4.2"
"@govuk-one-login/data-vocab": "^1.7.2"
},
"devDependencies": {
"typescript": "^5.2.2"
Expand Down

0 comments on commit 1f7b76e

Please sign in to comment.