Skip to content

Commit

Permalink
Merge pull request #963 from NordicSemiconductor/feat/add_source.json…
Browse files Browse the repository at this point in the history
…_schema

Add schemas for `source.json` and `withdrawn.json` files
  • Loading branch information
datenreisender authored Jan 9, 2025
2 parents 476eb97 + cb5667e commit aa1e453
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 13 deletions.
3 changes: 2 additions & 1 deletion Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ This project does _not_ adhere to
[Semantic Versioning](https://semver.org/spec/v2.0.0.html) but contrary to it
every new version is a new major version.

## 195.0.0 - UNRELEASED
## 195.0.0 - 2025-01-09

### Added

- `virtualDevices` parameter to `DeviceSelector`.
- `onVirtualDeviceSelected` and `onVirtualDeviceDeselected` parameters to
`DeviceSelector`.
- Exported zod schemas for `source.json` and `withdrawn.json` files.

## 194.0.0 - 2024-12-17

Expand Down
14 changes: 8 additions & 6 deletions ipc/MetaFiles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@ import { z } from 'zod';

export type UrlString = string;

export interface SourceJson {
name: string;
apps: UrlString[];
}

export type WithdrawnJson = UrlString[];
export const sourceJsonSchema = z.object({
name: z.string(),
apps: z.array(z.string().url()),
});
export type SourceJson = z.infer<typeof sourceJsonSchema>;

export const withdrawnJsonSchema = z.array(z.string().url());
export type WithdrawnJson = z.infer<typeof withdrawnJsonSchema>;

export type AppVersions = {
[version: string]: AppVersion;
Expand Down
7 changes: 3 additions & 4 deletions ipc/schema/packageJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ const packageJson = z.object({

export type PackageJson = z.infer<typeof packageJson>;

export const parsePackageJson =
parseWithPrettifiedErrorMessage<PackageJson>(packageJson);
export const parsePackageJson = parseWithPrettifiedErrorMessage(packageJson);

// Apps have more required fields in their package.json

Expand Down Expand Up @@ -57,7 +56,7 @@ const packageJsonApp = packageJson.extend({
export type PackageJsonApp = z.infer<typeof packageJsonApp>;

export const parsePackageJsonApp =
parseWithPrettifiedErrorMessage<PackageJsonApp>(packageJsonApp);
parseWithPrettifiedErrorMessage(packageJsonApp);

// In the launcher we want to handle that the whole nrfConnectForDesktop may be missing
// and the html in it can also be undefined, so there we need to use this legacy variant
Expand All @@ -71,4 +70,4 @@ const packageJsonLegacyApp = packageJsonApp.extend({
export type PackageJsonLegacyApp = z.infer<typeof packageJsonLegacyApp>;

export const parsePackageJsonLegacyApp =
parseWithPrettifiedErrorMessage<PackageJsonLegacyApp>(packageJsonLegacyApp);
parseWithPrettifiedErrorMessage(packageJsonLegacyApp);
4 changes: 2 additions & 2 deletions ipc/schema/parseJson.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import { z } from 'zod';
import { fromZodError } from 'zod-validation-error';

export const parseWithPrettifiedErrorMessage =
<Out, T extends z.ZodType<Out> = z.ZodTypeAny>(schema: T) =>
<T extends z.ZodTypeAny>(schema: T) =>
(content: string) => {
const result = schema.safeParse(JSON.parse(content));

if (result.success) {
return result;
return result as z.SafeParseSuccess<z.infer<T>>;
}

return {
Expand Down
2 changes: 2 additions & 0 deletions main/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ export const serialPort = {
};

export {
sourceJsonSchema,
withdrawnJsonSchema,
type AppInfo,
type NrfutilModuleName,
type NrfutilModules,
Expand Down

0 comments on commit aa1e453

Please sign in to comment.