|
| 1 | +--- |
| 2 | +title: "A schema for a document" |
| 3 | +description: "The validators of Brazilian Utils inside a Zod, Valibot or ArkType schema, or as a Standard Schema of their own." |
| 4 | +keywords: ["zod CPF", "valibot CPF", "arktype CPF", "Standard Schema", "toStandardSchema", "validate CNPJ schema"] |
| 5 | +--- |
| 6 | + |
| 7 | +A validator goes straight into a schema: pick the document and the schema library. Nothing runs here, a schema is the same code everywhere. |
| 8 | + |
| 9 | +Each of these builds the document on its own first, so it can be reused wherever a schema needs it, and composes it into a form afterwards. They all speak [Standard Schema](https://standardschema.dev), which is how a schema reaches a form library, and `toStandardSchema` gives one validator that same interface with no schema library at all. |
| 10 | + |
| 11 | + |
| 12 | +<div class="example" data-name="Zod"> |
| 13 | + |
| 14 | +`refine` takes the validator as it is: |
| 15 | + |
| 16 | +<div class="variant" data-variant="CPF"> |
| 17 | + |
| 18 | +<div class="file" data-file="cpf-zod.ts"> |
| 19 | + |
| 20 | +[cpf-zod.ts](../snippets/document-field/generated/cpf/schema/cpf-zod.ts ':include :type=code ts') |
| 21 | + |
| 22 | +</div> |
| 23 | + |
| 24 | +</div> |
| 25 | + |
| 26 | +<div class="variant" data-variant="CNPJ"> |
| 27 | + |
| 28 | +<div class="file" data-file="cnpj-zod.ts"> |
| 29 | + |
| 30 | +[cnpj-zod.ts](../snippets/document-field/generated/cnpj/schema/cnpj-zod.ts ':include :type=code ts') |
| 31 | + |
| 32 | +</div> |
| 33 | + |
| 34 | +</div> |
| 35 | + |
| 36 | +<div class="variant" data-variant="CEP"> |
| 37 | + |
| 38 | +<div class="file" data-file="cep-zod.ts"> |
| 39 | + |
| 40 | +[cep-zod.ts](../snippets/document-field/generated/cep/schema/cep-zod.ts ':include :type=code ts') |
| 41 | + |
| 42 | +</div> |
| 43 | + |
| 44 | +</div> |
| 45 | + |
| 46 | +<div class="variant" data-variant="Phone"> |
| 47 | + |
| 48 | +<div class="file" data-file="phone-zod.ts"> |
| 49 | + |
| 50 | +[phone-zod.ts](../snippets/document-field/generated/phone/schema/phone-zod.ts ':include :type=code ts') |
| 51 | + |
| 52 | +</div> |
| 53 | + |
| 54 | +</div> |
| 55 | + |
| 56 | +</div> |
| 57 | + |
| 58 | +<div class="example" data-name="Valibot"> |
| 59 | + |
| 60 | +`check` takes the validator inside a pipe: |
| 61 | + |
| 62 | +<div class="variant" data-variant="CPF"> |
| 63 | + |
| 64 | +<div class="file" data-file="cpf-valibot.ts"> |
| 65 | + |
| 66 | +[cpf-valibot.ts](../snippets/document-field/generated/cpf/schema/cpf-valibot.ts ':include :type=code ts') |
| 67 | + |
| 68 | +</div> |
| 69 | + |
| 70 | +</div> |
| 71 | + |
| 72 | +<div class="variant" data-variant="CNPJ"> |
| 73 | + |
| 74 | +<div class="file" data-file="cnpj-valibot.ts"> |
| 75 | + |
| 76 | +[cnpj-valibot.ts](../snippets/document-field/generated/cnpj/schema/cnpj-valibot.ts ':include :type=code ts') |
| 77 | + |
| 78 | +</div> |
| 79 | + |
| 80 | +</div> |
| 81 | + |
| 82 | +<div class="variant" data-variant="CEP"> |
| 83 | + |
| 84 | +<div class="file" data-file="cep-valibot.ts"> |
| 85 | + |
| 86 | +[cep-valibot.ts](../snippets/document-field/generated/cep/schema/cep-valibot.ts ':include :type=code ts') |
| 87 | + |
| 88 | +</div> |
| 89 | + |
| 90 | +</div> |
| 91 | + |
| 92 | +<div class="variant" data-variant="Phone"> |
| 93 | + |
| 94 | +<div class="file" data-file="phone-valibot.ts"> |
| 95 | + |
| 96 | +[phone-valibot.ts](../snippets/document-field/generated/phone/schema/phone-valibot.ts ':include :type=code ts') |
| 97 | + |
| 98 | +</div> |
| 99 | + |
| 100 | +</div> |
| 101 | + |
| 102 | +</div> |
| 103 | + |
| 104 | +<div class="example" data-name="ArkType"> |
| 105 | + |
| 106 | +`narrow` takes it, and says what the value must be when it says no: |
| 107 | + |
| 108 | +<div class="variant" data-variant="CPF"> |
| 109 | + |
| 110 | +<div class="file" data-file="cpf-arktype.ts"> |
| 111 | + |
| 112 | +[cpf-arktype.ts](../snippets/document-field/generated/cpf/schema/cpf-arktype.ts ':include :type=code ts') |
| 113 | + |
| 114 | +</div> |
| 115 | + |
| 116 | +</div> |
| 117 | + |
| 118 | +<div class="variant" data-variant="CNPJ"> |
| 119 | + |
| 120 | +<div class="file" data-file="cnpj-arktype.ts"> |
| 121 | + |
| 122 | +[cnpj-arktype.ts](../snippets/document-field/generated/cnpj/schema/cnpj-arktype.ts ':include :type=code ts') |
| 123 | + |
| 124 | +</div> |
| 125 | + |
| 126 | +</div> |
| 127 | + |
| 128 | +<div class="variant" data-variant="CEP"> |
| 129 | + |
| 130 | +<div class="file" data-file="cep-arktype.ts"> |
| 131 | + |
| 132 | +[cep-arktype.ts](../snippets/document-field/generated/cep/schema/cep-arktype.ts ':include :type=code ts') |
| 133 | + |
| 134 | +</div> |
| 135 | + |
| 136 | +</div> |
| 137 | + |
| 138 | +<div class="variant" data-variant="Phone"> |
| 139 | + |
| 140 | +<div class="file" data-file="phone-arktype.ts"> |
| 141 | + |
| 142 | +[phone-arktype.ts](../snippets/document-field/generated/phone/schema/phone-arktype.ts ':include :type=code ts') |
| 143 | + |
| 144 | +</div> |
| 145 | + |
| 146 | +</div> |
| 147 | + |
| 148 | +</div> |
| 149 | + |
| 150 | +<div class="example" data-name="Standard Schema"> |
| 151 | + |
| 152 | +No schema library at all: `toStandardSchema` gives the validator the interface every form library speaks. |
| 153 | + |
| 154 | +<div class="variant" data-variant="CPF"> |
| 155 | + |
| 156 | +<div class="file" data-file="cpf-standard.ts"> |
| 157 | + |
| 158 | +[cpf-standard.ts](../snippets/document-field/generated/cpf/schema/cpf-standard.ts ':include :type=code ts') |
| 159 | + |
| 160 | +</div> |
| 161 | + |
| 162 | +</div> |
| 163 | + |
| 164 | +<div class="variant" data-variant="CNPJ"> |
| 165 | + |
| 166 | +<div class="file" data-file="cnpj-standard.ts"> |
| 167 | + |
| 168 | +[cnpj-standard.ts](../snippets/document-field/generated/cnpj/schema/cnpj-standard.ts ':include :type=code ts') |
| 169 | + |
| 170 | +</div> |
| 171 | + |
| 172 | +</div> |
| 173 | + |
| 174 | +<div class="variant" data-variant="CEP"> |
| 175 | + |
| 176 | +<div class="file" data-file="cep-standard.ts"> |
| 177 | + |
| 178 | +[cep-standard.ts](../snippets/document-field/generated/cep/schema/cep-standard.ts ':include :type=code ts') |
| 179 | + |
| 180 | +</div> |
| 181 | + |
| 182 | +</div> |
| 183 | + |
| 184 | +<div class="variant" data-variant="Phone"> |
| 185 | + |
| 186 | +<div class="file" data-file="phone-standard.ts"> |
| 187 | + |
| 188 | +[phone-standard.ts](../snippets/document-field/generated/phone/schema/phone-standard.ts ':include :type=code ts') |
| 189 | + |
| 190 | +</div> |
| 191 | + |
| 192 | +</div> |
| 193 | + |
| 194 | +</div> |
| 195 | + |
| 196 | +The [utilities reference](../utilities.md#tostandardschema) documents `toStandardSchema` and the types of the specification. |
0 commit comments