or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

Files

docs

core-validation.mderror-handling.mdformat-validation.mdindex.mdtype-checking.mdvalidator-creation.mdvalidators.md

core-validation.mddocs/

0

# Core Validation

1

2

Essential validation functionality providing the main entry points for JSON Schema validation. These functions offer the most convenient ways to validate data against schemas.

3

4

## Capabilities

5

6

### Main Validation Function

7

8

The primary validation function that automatically selects the appropriate validator and validates data against a schema.

9

10

```python { .api }

11

def validate(instance, schema, cls=None, *args, **kwargs):

12

"""

13

Validate an instance under the given schema.

14

15

Parameters:

16

- instance: The data to validate

17

- schema: The JSON schema to validate against

18

- cls: Validator class to use (optional, auto-detected from schema)

19

- format_checker: FormatChecker instance for format validation

20

- registry: Schema registry for reference resolution

21

22

Raises:

23

- ValidationError: If the instance is invalid

24

- SchemaError: If the schema itself is invalid

25

"""

26

```

27

28

Usage example:

29

30

```python

31

from jsonschema import validate, ValidationError

32

33

schema = {"type": "string", "minLength": 5}

34

35

try:

36

validate("hello", schema) # Valid

37

validate("hi", schema) # Raises ValidationError

38

except ValidationError as e:

39

print(f"Validation failed: {e.message}")

40

```

41

42

### Validator Selection

43

44

Automatically determine the appropriate validator class for a given schema based on its $schema keyword.

45

46

```python { .api }

47

def validator_for(schema, default=_UNSET):

48

"""

49

Retrieve the validator class appropriate for validating the given schema.

50

51

Parameters:

52

- schema: The schema to analyze

53

- default: Default validator class if $schema not found

54

55

Returns:

56

- Validator class appropriate for the schema

57

58

Raises:

59

- SchemaError: If no appropriate validator can be determined

60

"""

61

```

62

63

Usage example:

64

65

```python

66

from jsonschema import validator_for

67

68

# Schema with explicit $schema

69

schema = {

70

"$schema": "https://json-schema.org/draft/2020-12/schema",

71

"type": "object",

72

"properties": {"name": {"type": "string"}}

73

}

74

75

ValidatorClass = validator_for(schema)

76

validator = ValidatorClass(schema)

77

78

# Use the validator

79

if validator.is_valid({"name": "John"}):

80

print("Valid!")

81

```

82

83

### Quick Validation Check

84

85

Check if data is valid without raising exceptions:

86

87

```python

88

from jsonschema import Draft202012Validator

89

90

validator = Draft202012Validator(schema)

91

is_valid = validator.is_valid(data) # Returns boolean

92

```

93

94

### Detailed Error Information

95

96

Get all validation errors for comprehensive feedback:

97

98

```python

99

from jsonschema import Draft202012Validator

100

101

validator = Draft202012Validator(schema)

102

errors = list(validator.iter_errors(data))

103

104

for error in errors:

105

print(f"Error at {'.'.join(str(p) for p in error.path)}: {error.message}")

106

```