ajv-validator / ajv-validator/ajv

Type of sibling property of a ref property is unknown when validated via JTDSchemaType

Abierto
#2,167 4 comentarios 0 reacciones 0 asignados Ver en GitHub
bug report typescript
Lenguaje dominante
TypeScript
Estrellas
14.8k
Forks
1k
Métricas de merge de PR
Sin PR fusionados en 30 d

Descripción

**What version of Ajv are you using? Does the issue happen if you use the latest version?**

8.11.2 (latest as of this writing)

**Ajv options object**

```ts
import Ajv, { JTDSchemaType } from "ajv/dist/jtd";
const ajv = new Ajv();
```

**JTD Schema**

```ts
type MyType = {
referenced: number;
stringProp: string;
};

const schema: JTDSchemaType = {
definitions: {
num: { type: "int32" }
},
properties: {
referenced: { ref: "num" },
stringProp: { type: "string" }
}
} as const;

```

**Sample data**

```ts
const data = {} as any;
```

**Your code**

```ts
const validate = ajv.compile(schema);

if (validate(data)) {
const revealType: never = data;
const stringProp: string = data.stringProp;
console.log(stringProp);
}
```

https://replit.com/@ento/IntrepidIndolentOutput#index.ts

^`strictNullChecks` is enabled in `tsconfig.json`

```json
"strictNullChecks": true,
```

**Validation result, data AFTER validation, error messages**

TypeScript's typecheck errors:

```
// const revealType
Type '{ referenced: number; stringProp: unknown; } & {}' is not assignable to type 'never'.

// const stringProp
Type 'unknown' is not assignable to type 'string'.
```

**What results did you expect?**

The type of `stringProp` to be `string` after being narrowed by `validate(data)`

**Are you going to resolve the issue?**

My current workaround is to hold the definition in a const and use it to compose the schema object. (The actual code is a bit more complex than this.)

```ts
const numSchema = { type: "int32" } as const;

const schema: JTDSchemaType = {
properties: {
referenced: numSchema,
stringProp: { type: "string" }
}
} as const;
```

Guía de contribución

Abrir la guía de contribución

Evaluación

Este issue todavía no se ha evaluado.

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.